1、打开MyEclipse,添加WebProject,名称为testStruts2
2、配置Tomcat环境。
1) 在窗口——首选项——MyEclipse——Servers下找到Tomcat6.x
2) 选择Enable、浏览找到本机Tomcat6文件位置
3) 找到Tomcat——conf——server.xml文件、在</Host>结束标签前添加
<Context path="/testStruts2" docBase="E:softwareMyEclipseWorkSpaces estStruts2WebRoot" reloadable="true" />
3、引入jar包
4、配置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"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
5、在src路径下建立struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="testStruts2" extends="struts-default" namespace=""> <action name="login" class="com.test.action.LoginAction"> <result name="success">/result.jsp</result> </action> </package> </struts>
6、在src路径下建立类文件
package com.test.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { private static final long serialVersionUID = -74906200993380354L; 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"; } }
7、在WebRoot路径下新建login.jsp、result.jsp进行测试
/** * login.jsp */ <form action="login" method="post" > username:<input type="text" name="username"><br> password:<input type="text" name="password"> <input type="submit" value="submit"> </form> /** * result.jsp */ username:${requestScope.username }<br> password:${requestScope.password }