1、环境:structs2.0,MyEclipse 6.0,tomcat 6.0
2、步骤:
(1) 在MyEclipse下建立web project,把structs2.0中的包:commons-logging-1.0.4.jar、freemarker-3.2.8.jar、ognl-2.6.11.jar,struts-core-2.0.11.1.jar、xwork-2.0.4.jar导入进来
(2) 配置web.xml文件
<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>
(3)创建两个jsp文件,login.jsp和result.jsp,主要配置如下:
login.jsp
<body>
<form action="login1.action",method="post">
username <input name="username",type="text"/><br>
password <input name="password" type="password"/><br>
<input type="submit" value="submit"/>
</form>
</body>
result.jsp
<body>
username ${requestScope.username}<br>
password ${requestScope.password}
</body>
(4)创建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="login1" class="com.test.action.LoginAction">
<result name="success">/result.jsp</result>
</action>
</package>
</struts>
(5)编写action类,代码如下:
package com.test.action;
public class LoginAction{
public String username;
public 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";
}
}
(6)启动tomcat服务器,输入http://localhost:8080/MyStructs2/login.jsp ,输入数据进行测试!