Struts2應用的開發流程,struts2應用流程

來源:互聯網
上載者:User

Struts2應用的開發流程,struts2應用流程

Struts2的開發流程

為了能夠在eclipse中使用Struts2在進行開發時,需要根據需要匯入一些有關的jar包;

在官網下載相關的壓縮包,這裡下載了兩個:struts-2.3.30-all.zip和struts-2.3.30-docs.zip,解壓到同一個檔案夾中。

在eclipse中建立了新的Dymamic Web Project以後,為該項目添加Struts2支援:

1.複製Struts2解壓目錄下struts2-blank.war解壓後WEB-INF\lib目錄下的所有jar包到web項目中的WebContent\WEB-INF\lib下;

2.把這些複製的jar包匯入項目中去。

開始使用Struts2開發:

1.在web.xml中定義攔截器攔截使用者請求:

  如果eclipse中產生的Dynamic Web Project中沒有自動產生web.xml檔案,可以從Tomcat相關的檔案夾中找到一個預設的web.xml檔案,把此檔案複製到項目的WEB-INF目錄下(不是WEB-INF\lib下)。

  也可以在struts2-blank.war解壓後目錄的WEB-INF下直接複製一個web.xml。

  修改此web.xml檔案,如下所示:

<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">    <!-- 定義核心攔截器 -->
<filter> <filter-name>struts2</filter-name> <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></web-app>

2.定義包含表單資料的JSP頁面:

  寫一個提交請求的JSP頁面,此JSP頁面將會將請求提交給相關的Action類來處理。

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:form action="test_1">    <s:textfield name="username" key="username"/>    <s:submit key="login" value="login"/></s:form></body></html>

3.定義處理使用者請求的Action類:

  寫一個與以上JSP關聯的Action類來處理該JSP提交的請求。在eclipse中直接放在在src目錄下即可

package testAction;import com.opensymphony.xwork2.ActionSupport;public class Test_1Action extends ActionSupport{    //定義封裝請求參數的成員變數    private String username;    //setter、getter方法    public void setUsername(String username){        this.username = username;    }    public String getUsername(){        return this.username;    }    //定義處理請求的execute方法    public String execute()throws Exception{        if(getUsername()!=null){            return SUCCESS;        }else{            return ERROR;        }    }}

4.編寫物理視圖資源檔:

  為了簡單,我們把物理視圖資源檔直接放在項目的webContent目錄下

  error.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>error page</title></head><body>    錯誤頁面!username為空白!</body></html>

  welcome.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>welcome page</title></head><body>    登陸成功!</body></html>

5.配置處理使用者請求的Action類並配置Action的處理結果和物理視圖資源的映射關係::

  使得JSP頁面的請求能夠準確找到與此JSP頁面相關的Action類。

  配置Action需要在項目的src目錄下添加struts.xml檔案,此檔案的預設檔案也可以在struts2-blank.war解壓後目錄的WEB-INF\classes下找到,把其修改為如下內容:

<?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>    <constant name="struts.enable.DynamicMethodInvocation" value="false" />    <constant name="struts.devMode" value="true" />        <package name="test" extends="struts-default">        <!--             定義了名為test_1的Action;            處理請求的Action類為testAction.Test_1Action.java         -->        <action name="test_1" class="testAction.Test_1Action">            <!-- 定義邏輯視圖與物理視圖之間的映射關係 -->            <result name="error">error.jsp</result>            <result name="success">welcome.jsp</result>        </action>    </package></struts>

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.