1.新建webapp:
struts_login
2.导入struts2的jar包
(不过做介绍)
3.配置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>
4.新建一个jsp页面:
<body> <form action="login.action" method="post"> Username:<input type="text" name="username"><br> Password:<input type="password" name="password"><br> <input type="submit" value="submit"> </form> </body>
5.新建一个Action类
package com.zchen.struts.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() {
return "success";
}
}
6.配置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.zchen.struts.action.LoginAction">
<result>/result.jsp</result>
</action>
</package>
</struts>
7.新建一个result页面
<body> username:${requestScope.username }<br> password:${requestScope.password }<br> </body>