下面我们来做一个struts2的登陆的例子,如果您还不了解struts2的开发步骤的,请先参阅作者的另一篇文章:http://blog.csdn.net/lenotang/archive/2008/08/07/2782813.aspx
1.使用领域对象来接收用户输入
Step1:在web.xml上配置struts2的过滤器
<?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>strut2(1)</display-name>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<filter>
<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>
</web-app>
Step2:编写login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user.username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="user.password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><input type="reset" value="重填"></td>
</tr>
</table>
</form>
</body>
</html>
Step3:编写User类
package org.leno.struts2.model;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Step4:编写LoginAction
package org.leno.struts2.action;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
public class LoginAction1 implements Action {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword()))
return SUCCESS;
else
return ERROR;
}
}
Step5:编写success.jsp和error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1><s:property value="user.username" />,欢迎您登录老许的博客(http://www.blog.csdn.net/lenotang)</h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
用户名或密码错误,请重新<a href="login.jsp">登录</a>!
</body>
</html>
Step6:在struts.xml中配置LoginAction1
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="login"
class="org.leno.struts2.action.LoginAction1">
<result>/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
2.使用ModelDriven action简化对领域对象的操作
package org.leno.struts2.action;
import org.leno.struts2.model.User;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction2 implements Action, ModelDriven<User> {
private User user = new User();
public String execute() throws Exception {
if ("leno".equals(user.getUsername())
&& "123".equals(user.getPassword()))
return SUCCESS;
else
return ERROR;
}
public User getModel() {
return user;
}
}
修改login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登录"></td>
<td><input type="reset" value="重填"></td>
</tr>
</table>
</form>
</body>
</html>
修改success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1><s:property value="username" />,欢迎您登录老许的博客(http://www.blog.csdn.net/lenotang)</h1>
</body>
</html>
3.直接使用Action的属性来接收用户的输入
一些很简单的应用,不需要使用模型对象,我们可以删除User类,直接使用action的属性来接收用户的输入。
package org.leno.struts2.action;
import com.opensymphony.xwork2.Action;
public class LoginAction3 implements Action {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute() throws Exception {
if ("leno".equals(username) && "123".equals(password))
return SUCCESS;
else
return ERROR;
}
}
最后别忘了导入struts2的jar包和在web.xml上配置struts2的过滤器。