一、下载struts2.0
http://struts.apache.org/download.cgi#struts212
二、编辑web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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>HelloWorld</display-name> <!-- 定义Struts 2 的FilterDispatcher 的Filter --> <filter> <!-- 定义核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 定义核心Filter实现类 --> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <!-- FilterDispatcher 用来初始化 Struts2 并且处理所有的Web请求 --> <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> </web-app>
三、创建一个简单的用户登录界面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>简单的用户登录</title> </head> <body> <form action="Login.action" methos="post"> 用户名:<input type="text" name="name" /> <br> 密 码:<input type="password" name="password" /> <br> <input type="submit" name="tj" value="提交" /> </form> </body> </html>
四、创建实现控制类
package tutorial; public class Login { private String name; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String execute() { if ("abc".equals(this.name) && "123".equals(this.password)) return "success"; else return "false"; } }
五、配置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"></include> <package name="strutsqs" extends="struts-default"> <action name="Login" class="tutorial.Login"> <result name="success" type="redirect">success.jsp</result> <result name="false" type="redirect">faile.jsp</result> </action> </package> </struts>
六、创建两个结果页面
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>简单的用户登录</title> </head> <body> 登录成功 </body> </html>
faile.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>简单的用户登录</title> </head> <body> <form action="Login.action" methos="post"> 用户名:<input type="text" name="name" /> <br> 密 码:<input type="passwordr" name="password" /> <br> <input type="submit" name="tj" value="提交" /> </form> </body> </html>