Struts2 配置
1.首先建立一个Web Project
2.将struts提供的jar包,放到lib目录下(添加必须要使用的,额外的不加,防止出现问题)
必须使用的jar包:
- commons-fileupload- .jar(处理文件上传的)
- commons-io- .jar(上面的依赖于这个)
- commons-logging- .jar(日志处理)
- ognl- .jar(表达式语言)
- struts2-core- .jar()
- xwork-core- .jar(struts的内核)
- freemark- .jar
- javassist- .GA.jar
- commons-lang3- .jar
3.新建jsp页面,在进行web.xml配置,使struts融入进去web里面,必须进行配置
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPreparAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
4.在src目录下建立struts.xml,配置struts的xml(打开你安装的struts2里面的apps内的任何一个包,找到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="structs2" extends="struts-default"> <action name="login" class="com.liule.LoginAction"> <result name="success">/result.jsp</result> </action> </package> </struts>
5.在src目录下建立一个包com.liule.struts2,再建立个.class文件(LoginAction.java)
package com.liule; 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.将应用部署到服务器上,配置服务器的server.xml
<Context path="/struts2" docBase="D:MyEclipse Professionalstruts2WebRoot" reloadable="true"/>
7.login.jsp
<body> <form action="login"> <input type="text" name="username"><br> <input type="password" name="password"><br> <input type="submit" value="submit"> </form> </body>
8.result.jsp
<body> <%=request.getParameter("username") %> <%=request.getParameter("password") %> </body>