搭建一個簡單的Struts2框架

 1  建立一個web項目。css

 2 導入必要的JAR文件。html

      放在WEB-INF下lib包裏。java

      

3 添加web.xml配置,添加啓動配置。web

     

 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。apache

  1. Struts2直接使用Action來封裝HTTP請求參數,所以Action類應該包含與請求相對應的屬性,併爲該屬性提供對應的setter和getter方法。
  2. 爲Action類裏增長一個execute方法,由於Struts2框架默認會執行這個方法。這個方法自己並不作業務邏輯處理,而是調用其餘業務邏輯組件完成這部分工做。
  3. Action類返回一個標準的字符串,該字符串是一個邏輯視圖名,該視圖名對應實際的物理視圖。

咱們來寫個用戶登陸驗證,提供用戶名和密碼兩個屬性。若是正確返回success不然返回error。app

  

 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。jsp

   在struts2-core-2.3.16.jar中有strust-defalut.xml.咱們須要繼承它。post

 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>

 

 總體:

 

相關文章
相關標籤/搜索