在Eclipse下实践Struts(不用Struts IDE) 二

本文介绍了一个简单的Struts2应用实例,包括web.xml配置、index.jsp页面、success.jsp反馈页面及对应的HelloAction处理类。该示例展示了如何通过Struts2框架处理用户输入并返回结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

改了一下,只贴代码了:
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>utest</display-name>
<filter>
<display-name>struts2</display-name>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>你好,世界</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="hello.action" method="post">
<fieldset><legend>Struts2入门实例</legend>
<p><label>请输入你想说的话:</label>
<input type="text" name="helloWorld" value="buddy" /></p>
<p><input type="submit" value="提交" /></p>
</fieldset>
</form>
</body>
</html>


success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功啦</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<s:property value="helloWorld" /> <br>
</body>
</html>


struts.xml

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />

<package name="default" extends="struts-default">
<action name="hello"
class="com.yeepal.test.HelloAction">
<result name="success">success.jsp</result>
<result name="input">index.jsp</result>
</action>

</package>
</struts>


HelloAction.java

package com.yeepal.test;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
private static final long serialVersionUID = 1L;

private String helloWorld;

public String getHelloWorld() {
return helloWorld;
}

public void setHelloWorld(String helloWorld) {
this.helloWorld = helloWorld;
}

public String execute() throws Exception {
return SUCCESS;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值