一 action
package org.crazyit.app.action;
import com.opensymphony.xwork2.*;
import java.util.Map;
public class LoginAction
extends ActionSupport
{
// 用于封装请求参数的username成员变量
private String username;
// username的setter和getter方法
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
}
二 配置文件
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<package name="lee" extends="struts-default">
<action name="login" class="org.crazyit.app.action.LoginAction">
<!-- 指定结果的类型为redirect,
这意味着系统该Action将重定向到welcome.jsp页面-->
<result type="redirect">/welcome.jsp</result>
</action>
<action name="*">
<result>/WEB-INF/content/{1}.jsp</result>
</action>
</package>
</struts>
三 视图
1 loginForm.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body>
<form action="login.action" method="post">
<table align="center">
<caption><h3>用户登录</h3></caption>
<tr>
<td>用户名:<input type="text" name="username"/></td>
</tr>
<tr align="center">
<td><input type="submit" value="登录"/><input type="reset" value="重填" /></td>
</tr>
</table>
</form>
</body>
</html>
2 welcome.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>欢迎</title>
</head>
<body>
不显示属性
<s:property value="username"/>
</body>
</html>
四 测试