Struts2基於註解的Action配置

來源:互聯網
上載者:User

標籤:const   turn   java   r.js   mic   time   最好   建立   方法   

使用註解來配置Action的最大好處就是可以實現零配置,但是事務都是有利有弊的,使用方便,維護起來就沒那麼方便了。

 

要使用註解方式,我們必須添加一個額外包:struts2-convention-plugin-2.x.x.jar。

 

雖說是零配置的,但struts.xml還是少不了的,配置如下:

 

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

<!DOCTYPE struts PUBLIC 

    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" 

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

   

<struts>   

    <!-- 請求參數的編碼方式--> 

    <constant name="struts.i18n.encoding" value="UTF-8"/> 

    <!-- 指定被struts2處理的請求尾碼類型。多個用逗號隔開--> 

    <constant name="struts.action.extension" value="action,do,htm"/> 

    <!-- 當struts.xml改動後,是否重新載入。預設值為false(生產環境下使用),開發階段最好開啟  --> 

    <constant name="struts.configuration.xml.reload" value="true"/> 

    <!-- 是否使用struts的開發模式。開發模式會有更多的調試資訊。預設值為false(生產環境下使用),開發階段最好開啟  --> 

    <constant name="struts.devMode" value="false"/>   

    <!-- 設定瀏覽器是否緩衝靜態內容。預設值為true(生產環境下使用),開發階段最好關閉  --> 

    <constant name="struts.serve.static.browserCache" value="false" /> 

    <!-- 指定由spring負責action對象的建立    

    <constant name="struts.objectFactory" value="spring" /> 

    --> 

    <!-- 是否開啟動態方法引動過程--> 

    <constant name="struts.enable.DynamicMethodInvocation" value="false"/> 

</struts> 

action類的註解:

 

 

package com.tjcyjd.web.action;   

   

import org.apache.struts2.convention.annotation.Action;   

import org.apache.struts2.convention.annotation.ExceptionMapping;   

import org.apache.struts2.convention.annotation.ExceptionMappings;   

import org.apache.struts2.convention.annotation.Namespace;   

import org.apache.struts2.convention.annotation.ParentPackage;   

import org.apache.struts2.convention.annotation.Result;   

import org.apache.struts2.convention.annotation.Results;   

   

import com.opensymphony.xwork2.ActionSupport;   

   

/** 

 * Struts2基於註解的Action配置

 *  

 */   

@ParentPackage("struts-default") 

@Namespace("/annotation_test") 

@Results( { @Result(name = "success", location = "/main.jsp"), 

        @Result(name = "error", location = "/error.jsp") }) 

@ExceptionMappings( { @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") }) 

public class LoginAction extends ActionSupport { 

    private static final long serialVersionUID = 2730268055700929183L;  

    private String loginName;   

    private String password;   

   

    @Action("login") //或者寫成  @Action(value = "login")  

    public String login() throws Exception {   

   

        if ("yjd".equals(loginName) && "yjd".equals(password)) {   

            return SUCCESS;   

        } else {   

            return ERROR;   

        }   

    }   

    @Action(value = "add", results = { @Result(name = "success", location = "/index.jsp") })   

    public String add() throws Exception {   

        return SUCCESS;   

    }   

    public String getLoginName() {   

        return loginName;   

    }   

    public void setLoginName(String loginName) {   

        this.loginName = loginName;   

    }   

    public String getPassword() {   

        return password;   

    }   

    public void setPassword(String password) {   

        this.password= password;   

    }   

} 

這樣就完成了一個基於註解的action配置。

 

總結常用的註解如下:

 

Namespace:指定命名空間。

ParentPackage:指定父包。

Result:提供了Action結果的映射。(一個結果的映射)

Results:“Result”註解列表

ResultPath:指定結果頁面的基路徑。

Action:指定Action的訪問URL。

Actions:“Action”註解列表。

ExceptionMapping:指定異常映射。(映射一個聲明異常)

ExceptionMappings:一級聲明異常的數組。

InterceptorRef:攔截器引用。

InterceptorRefs:攔截器引用組。

Struts2基於註解的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.