一、
http://www.struts.apche.com 下載 Full Distribution: struts-2.0.14-all.zip
二、新建webProject:struts2
導入struts2須要的jar(下載的包中):commons-logging-1.0.4.jar、ognl-2.6.11.jar、freemarker-2.3.8.jar、struts2-core-2.0.14.jar、xwork-2.0.7.jar
三、Login.jsp以下:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<form action="login.action" method="post">
用戶名:
<input type="text" name="username" />
<br>
密碼:
<input type="password" name="password" />
<br>
<input type="submit" value="提交" />
<br>
</form>
</center>
</body>
</html>
四、web.xml中修改以下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="
http://java.sun.com/xml/ns/javaee"
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<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>
5.LoginAction以下:
package com.test.action;
public class LoginAction
{
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
{
return "success";
}
}
五、result.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
isELIgnored="false"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<br>******************************<br>
用戶名:${requestScope.username}<br>
密碼:${requestScope.password }<br>
*********************************<br>
用戶名:<%=(String)request.getAttribute("username") %><br>
密碼:<%=(String)request.getAttribute("password") %><br>
**********************************************<br>
用戶名:${username}<br>
密碼:${password }<br>
**********************************************<br>
</center>
</body>
</html>
六、在src下建struts.xml,以下:
<?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="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success" type="dispatcher">/result.jsp</result>
</action>
</package>
</struts>
七、
http://localhost:8080/struts2/Login.jsp 輸入用戶名、密碼提交 成功跳轉