Struts 2框架:基礎

Struts 2是一種MVC框架。html

Struts 2框架中的處理大概分爲如下幾個步驟:java

  1. 客戶端初始化一個指向Servlet容器的請求;web

  2. 這個請求通過一系列的過濾器(Filter)後,核心控制器FilterDispatcher根據請求詢問ActionMapper來決定這個請求是否須要調用某個Action。若是ActionMapper決定須要調用某個ActionFilterDispatcher把請求的處理交給ActionProxyActionProxy經過Configuration Manager詢問框架的配置文件,找到須要調用的Action類。數據庫

  3. ActionProxy建立一個ActionInvocation的實例。ActionInvocation實例使用命令模式調用,回調Actionexecute方法,該方法先獲取用戶請求參數,而後執行某種數據庫操做,既能夠是將數據保存到數據庫,也能夠從數據庫中檢索信息。由於Action只是一個控制器,它會調用業務邏輯組件來處理用戶的請求。在調用Action的過程先後,將涉及到相關攔截器的調用。apache

  4. Action執行完畢後,ActionInvocation會根據 struts.xml 中的配置找到對應的返回結果並將其輸出到瀏覽器中,能夠是HTML頁面、圖像,也能夠是文檔。此時支持的視圖技術很是多。segmentfault

實現一個Struts 2應用,只須要配置 struts.xml 文件,編寫Action代碼和相關視圖資源文件。瀏覽器

請輸入圖片描述


struts 2配置文件:

配置文件是用戶視圖(View)和業務邏輯模塊(Model)Action之間的橋樑。app

  • 配置Action的 struts.xml 文件,
  • 配置Struts 2全局屬性的 struts.properties 文件

一個實例:

配置好開發環境,安裝好Struts(將所須要的jar包放入相應位置)。框架

web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>struts 2.0 應用項目</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <!-- filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class-->
    <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>

  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

設計動做控制類

package com.struts2.app;

public class UserLoginAction {
    //下面是Action用於封裝用戶請求參數的兩個屬性
    private String name;
    private String pass;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String password) {
        this.pass = password;
    }

    //處理用戶請求的execute方法
    public String execute() throws Exception {
        if("admin".equals(getName()) && "12345".equals(getPass())) {
            return "success";
        }
        else if(getName()==null || getPass()==null) {
            return "input";
        }
        else return "error";
    }
}

配置Action類: Struts.xml

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<!--include file="struts-default.xml" /-->

    <!-- Struts 2 的Action必須放在指定的包空間下定義 -->
    <package name="struts" extends="struts-default">

        <action name="UserLogin" class="com.struts2.app.UserLoginAction">
            <result name="error">/error.jsp</result>
            <result name="success">/welcome.jsp</result>
            <result name="input">/index.jsp</result>
        </action>

    </package>

</struts>

設計視圖:JSP視圖

  • index.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK" %>

<html>
<head>
    <title>Struts 2 Demo</title>
</head>
<body>
    <form action="UserLogin.action" method="post">
        <table align="center">
            <caption><h4>用戶登陸信息</h4></caption>
            <tr>
                <td>用戶名:<input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>密 碼:<input type="password" name="pass" /></td>
            </tr>
            <tr align="center">
                <td colspan="2"><input type="submit" value="登 錄" /><input type="reset" value="清 除" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

  • welcome.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK" %>
<%@taglib prefix="s" uri="/struts-tags"%>

<html>
<head>
    <title>登陸成功頁面</title>
</head>
<body>
    您已成功登陸本系統!
    <li><a href="<s:url action="UserLogin" />">從新登陸</a></li>
    <br>
</body>
</html>

  • error.jsp
<%@page language="java" import="java.util.*" pageEncoding="GBK" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<% String path=request.getContextPath(); String basePath=request.getScheme()+"://"+request.getServerName()+
    ":"+request.getServerPort()+path+"/"; %>

<html>
<head>
    <base href="<%=basePath%>">
</head>
<body>
    您輸入的用戶名或密碼錯誤,請從新輸入
    <br><li><a href="<s:url action="UserLogin" />">Sign on</a></li>
</body>
</html>

實例過程總結

login.jsp (頁面內跳轉地址,struts攔截) ===> struts.xml (指定並調用Action類,以及不一樣結果下返回的視圖界面)===>UserLoginAction.java (execute方法,返回值)===>struts.xml (得到返回值,調用視圖)jsp

能夠看出,整個過程和servlet十分類似。只不過servlet使用web.xml映射java文件和路徑,這裏是用struts.xml(固然前提是在web.xml中有struts配置)。另外,servlet是直接用java文件返回其視圖的;而這裏經過三個jsp視圖文件處理不一樣狀況下的視圖。這樣,即將視圖層與控制層分開。

相關文章
相關標籤/搜索