搭建一个简单的Struts2框架

 1  创建一个web项目。

 2 导入必要的JAR文件。

      放在WEB-INF下lib包里。

      

3 添加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" 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>StrutsDemo</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12     <!-- struts2.1.3之后的版本,可以在该过滤器之前之间定义一定的过滤器-->
13     <!-- 定义struts2 的核心控制器,用于生成ActionMapper ,拦截所有的Action请求-->
14     <filter>
15         <filter-name>struts2</filter-name>
16        <filter-class>
17             org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
18        </filter-class>
19     </filter>
20     <filter-mapping>
21         <filter-name>struts2</filter-name>
22         <url-pattern>/*</url-pattern>
23     </filter-mapping> 
24   
25 </web-app>

  4  编写Action。

  1. Struts2直接使用Action来封装HTTP请求参数,因此Action类应该包含与请求相对应的属性,并为该属性提供对应的setter和getter方法。
  2. 为Action类里增加一个execute方法,因为Struts2框架默认会执行这个方法。这个方法本身并不做业务逻辑处理,而是调用其他业务逻辑组件完成这部分工作。
  3. Action类返回一个标准的字符串,该字符串是一个逻辑视图名,该视图名对应实际的物理视图。

我们来写个用户登录验证,提供用户名和密码两个属性。如果正确返回success否则返回error。

  

 1 package com.cy.action;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class LoginAction extends ActionSupport {
 6 
 7     private static final long serialVersionUID = 1L;
 8 
 9     private String userName;
10     private String password;
11 
12     public String execute() {
13 
14         if (userName.equals("hellokitty") && password.equals("123")) {
15 
16             return SUCCESS;
17         } else {
18             return ERROR;
19         }
20 
21     }
22 
23     public String getUserName() {
24         return userName;
25     }
26 
27     public void setUserName(String userName) {
28         this.userName = userName;
29     }
30 
31     public String getPassword() {
32         return password;
33     }
34 
35     public void setPassword(String password) {
36         this.password = password;
37     }
38 
39 }

Action有一下特点:

  • Struts2框架中Action是一个POJO,没有被代码污染。
  • Struts2中的Action的execute方法不依赖于servlet API,改善了Struts1中耦合过于紧密,极大方便了单元测试。
  • Struts2的Action无须用ActionForm封装请求参数。

5  添加框架核心配置文件struts.xml文件:在WEB-INF/classes文件夹下创建struts.xml。

   在struts2-core-2.3.16.jar中有strust-defalut.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 
 6 <struts>
 7     <package name="default" extends="struts-default">
 8         <action name="login" class="com.cy.action.LoginAction">
 9             <result name="success">/jsp/success.jsp</result> 
10              <result name="error">/jsp/error.jsp</result>      
11         </action>
12     </package>
13 
14 
15 </struts>
  1. 在action标签中定义了name和class。name属性对应的是用户URL请求中的action名,class属性是处理请求的实现类(注意:要包含完整路径)。
  2. result标签定义逻辑视图和物理视图之间的映射,在我们的Action中,如果返回的字符串是"success”则由对应的success.jsp页面进行处理;如果返回的字符串是"error”则由error.jsp页面进行处理。

6 编写界面 

 6.1 login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>My JSP 'Login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <form action="login" method="post">
27               用户名:<input type="text" name="userName"><br/>
28&nbsp;&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"/><br/>
29             <input type="submit" value="提交"/> 
30             <input type="reset" value="重置"/>                  
31     </form>
32   </body>
33 </html>

 6.2 success.jsp

   

 <body>
   <h1>登录成功!</h1>
  </body>

 6.3 error.jsp

 <body>
   <h1>登录失败!</h1>
  </body>

 整体:

 

  

原文地址:https://www.cnblogs.com/hellokitty1/p/5078185.html