2、新建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
3、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>
4、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;
{
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";
}
}
<%@ 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>
6、在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>
7、http://localhost:8080/struts2/Login.jsp
输入用户名、密码提交 成功跳转