struts 框架是一个MVC设计模式,通过与JSP配合使用完成Java Web项目中关于前台的页面交互和逻辑处理,下面是在Eclipse中整合struts的步骤:
1. 下载:
(1) Eclipse:https://www.eclipse.org/downloads/
(2) Struts:http://struts.apache.org/download.cgi#struts2316
2. 新建Dynamic Web Project,命名 “HelloStruts” ,将Struts压缩包解压后的 “\struts-2.3.16\lib” 中的其中几个文件拷贝到Web工程的 “WEB-INF”目录下,截图如下:
3. 在 “WebContent” 下 新建三个jsp页面,分别命名为“login.jsp”、“success.jsp”、“error.jsp”,代码如下:
(1) login.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>login</title> 9 </head> 10 11 <body> 12 <form action="login.action" method="post"> 13 <label>username:</label><input type="text" name="username" /> 14 <label>password:</label><input type="password" name="password" /> 15 <input type="submit" value="登录" /> 16 </form> 17 </body> 18 </html>
(2)success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib prefix="S" uri="/struts-tags" %> 4 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 9 <title>success page</title> 10 </head> 11 12 <body> 13 Hello,${sessionScope.username},login success! 14 </body> 15 </html>
(3)error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>error page</title> 8 </head> 9 <body> 10 Sorry,login error! 11 </body> 12 </html>
4. 新建struts过滤器,在项目的 “WEB-INF” 目录下新建“web.xml”,代码如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>Login</display-name> 4 5 <filter> 6 <filter-name>struts2</filter-name> 7 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 8 </filter> 9 10 <filter-mapping> 11 <filter-name>struts2</filter-name> 12 <url-pattern>/*</url-pattern> 13 </filter-mapping> 14 15 <welcome-file-list> 16 <welcome-file>index.html</welcome-file> 17 <welcome-file>index.htm</welcome-file> 18 <welcome-file>index.jsp</welcome-file> 19 <welcome-file>default.html</welcome-file> 20 <welcome-file>default.htm</welcome-file> 21 <welcome-file>default.jsp</welcome-file> 22 </welcome-file-list> 23 </web-app>
5. 在项目的src中新建login类,package为“com.struts”,代码如下:
1 package com.struts; 2 3 import javax.servlet.http.HttpServletRequest; 4 import org.apache.struts2.ServletActionContext; 5 import com.opensymphony.xwork2.ActionContext; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class login extends ActionSupport { 9 /* 10 * 默认版本序列号 11 */ 12 private static final long serialVersionUID = 1L; 13 private String username; 14 private String password; 15 16 public String getUsername() { 17 return username; 18 } 19 public void setUsername(String username) { 20 this.username = username; 21 } 22 public String getPassword() { 23 return password; 24 } 25 public void setPassword(String password) { 26 this.password = password; 27 } 28 29 public String execute() throws Exception { 30 if (this.getUsername().equals("admin") && this.getPassword().equals("admin")) { 31 ActionContext.getContext().getSession().put("username", this.getUsername()); 32 return "success"; 33 } else { 34 return "error"; 35 } 36 } 37 }
6. 在“com/struts”目录下新建“struts.xml”,代码如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 <struts> 6 <package name="login" extends="struts-default"> 7 <action name="login" class="com.struts.login"> 8 <result name="success">/success.jsp</result> 9 <result name="error">/error.jsp</result> 10 </action> 11 </package> 12 </struts>
7.
Tomcat下测试,需要实现将Tomcat插件整合到Eclipse,并做相关的配置。然后在浏览器中输入:http://localhost:8080/HelloStruts/login.jsp,输入“admin”,“admin”,则跳转到success
page,否则跳转到error page,效果图如下:
8. 搭建完毕。
eclipse整合Struts框架,布布扣,bubuko.com
原文:http://www.cnblogs.com/hypolee/p/3580984.html