配置Struts2的開發環境

來源:互聯網
上載者:User

配置Struts2的開發環境

面試題:

Struts2不是在Struts1基礎上而來的,是在WebWork2基礎發展而來的。儘管Struts2和Struts名字上差別不是很大,但在代碼編寫風格上幾乎是不一樣的。

搭建Struts2開發環境:

1.      需要的jar包

2.      編寫設定檔

3.      在web.xml中加入Struts2 MVC架構啟動配置

需要的jar包:

 

struts2案例

必要的準備

   struts2的開發環境。

      第一步:引入jar檔案

   第二步:建立struts2的設定檔  如下

<?xmlversion="1.0" encoding="UTF-8"?>

   <!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTDStruts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

</struts>

     第三步:添加啟動 struts2所依賴的filter過濾器在web.xml檔案添加

 

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

 

 

第一步:配置xml檔案

Struts2應用的設定檔:

         Struts2預設的設定檔為struts.xml,該檔案存放在WEB-INF/classes下,該檔案的配置模板為:

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

   "-//Apache Software Foundation//DTD Struts Configuration2.0//EN"

   "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="test" extends="struts-default" namespace="/cs">

       <action name="hello" class="cn.csdn.hr.action.HelloAction">

           <result name="login"type="dispatcher">../index.jsp</result>

       </action>

    </package>

</struts>

第二步:建立action

package cn.csdn.hr.action;

 

public class HelloAction {

    /*方法 第一必須返回String字串*/

    public String execute(){

       System.out.println("-----action執行-----");

       return "login";

    }

 

}

第三步:分析

http://localhost:8080/day11/cs/hello.action

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.