struts2配置,struts2

來源:互聯網
上載者:User

struts2配置,struts2
一、先匯入jar包

先解壓個空白的項目,從裡面複製jar包

二、導完jar包後,需要配置struts.xml檔案和web.xml檔案

1.匯入web.xml檔案,改配置.

相當於加了一個struts過濾器

<?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">    <display-name>Struts Blank</display-name>    <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>    <welcome-file-list>        <welcome-file>index.html</welcome-file>    </welcome-file-list>  </web-app>

 

 2.加入struts.xml檔案到src目錄下

struts.xml檔案配置, 主要配置的是action

<?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="default" namespace="/" extends="struts-default">           <!-- 對應著IndexAction裡面的execute方法 -->        <action name="index" class="com.itnba.maya.controller.IndexAction">            <!-- execute方法return SUCCESS時不用寫name -->            <result>                index.jsp <!--跳轉的頁面  -->            </result>                        <result name="error">                index_error.jsp            </result>                        <result name="haha">                index_haha.jsp            </result>        </action>                <!-- 直接跳轉不用處理資料時 不需要寫class -->        <action name="home">            <result>                home.jsp            </result>        </action>    </package>   </struts>

3.建一個類,繼承com.opensymphony.xwork2.ActionSupport    

在該類中,重寫execute()方法  

成員變數用來相互傳值,產生get set

package com.itnba.maya.controller;import com.opensymphony.xwork2.ActionSupport;public class IndexAction extends ActionSupport {    private String msg;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    @Override    public String execute() throws Exception {        msg+="haha";        return SUCCESS;    }    }

這樣就簡單的配置完成了,實際運用還要根據情況修改

 

聯繫我們

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