一 编写Action
package org.crazyit.app.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
public class LoginAction implements Action
{
private String username;
private String password;
// username的setter和getter方法
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
// password的setter和getter方法
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
public String execute() throws Exception
{
ActionContext ctx = ActionContext.getContext();
// 通过ActionContext访问application范围的属性值
Integer counter = (Integer)ctx.getApplication()
.get("counter");
if (counter == null)
{
counter = 1;
}
else
{
counter = counter + 1;
}
// 通过ActionContext设置application范围的属性
ctx.getApplication().put("counter" , counter);
// 通过ActionContext设置session范围的属性
ctx.getSession().put("user" , getUsername());
if (getUsername().equals("crazyit.org")
&& getPassword().equals("leegang") )
{
// 通过ActionContext设置request范围的属性
ctx.put("tip" , "服务器提示:您已经成功的登录");
return SUCCESS;
}
// 通过ActionContext设置request范围的属性
ctx.put("tip" , "服务器提示:登录失败");
return ERROR;
}
}
二 配置文件
<?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="conststruts.devMode" value="true"/>
<!-- Struts2的所有Action都需位于package下 -->
<package name="lee" extends="struts-default">
<!-- 定义名为login的Action,其实现类为LoginAction类 -->
<action name="login" class="org.crazyit.app.action.LoginAction">
<!-- 处理结果返回error,对应/WEB-INF/content/error.jsp视图资源 -->
<result name="error">/WEB-INF/content/error.jsp</result>
<!-- 处理结果返回success,对应/WEB-INF/content/welcome.jsp视图资源 -->
<result>/WEB-INF/content/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>
<td>密 码:<input type="text" name="password"/></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="" %>
<!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>
本站访问次数为:${applicationScope.counter}<br/>
${sessionScope.user},您已经登录!<br/>
${requestScope.tip}
</body>
</html>
3 error.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>
本站访问次数为:${applicationScope.counter}<br/>
${sessionScope.user},您不能登录!<br/>
${requestScope.tip}
</body>
</html>
四 测试