Struts2注解配置Action

最近在看Struts2的showcase,本打算先找个简单的看看,就选择了person,要么说没有中彩票的运气,第一个例子我就看不明白了,person的index.jsp里是这样调用action的
<html>
<head>
    <title>Acme Corp</title>
</head>

<body>
<ul>
    <li><a href="newPerson!input.action">Create</a> a new person</li>
    <li><a href="listPeople.action">List</a> all people</li>
</ul>
</body>
</html>
但是在struts-person.xml里却没有这个action的声明,只是声明了一个包,也没有默认的action类,后来”百度“了一下才知道,这个是Struts2的新特性,使用注解配置Action。

  使用注解配置Action第一个要做的就是在web.xml中配置FilterDispatcher,为FilterDispatcher设置actionPackages参数,指定包含了Action类的包的列表,多个包之间用(,)分割,这些包和他们的子包都会被扫描。扫描的对象是包中所有实现了Action接口,或者以"Action"结尾的类。顺便说一下类名的匹配方法,把类名的第一个字母小写,如果以Action结尾的,去掉Action。比如说一个类叫 DeletePersonAction 那么他的请求应该是deletePersion.action。
    <filter>
        <filter-name>struts-prepare</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class>
        <init-param>
            <param-name>actionPackages</param-name>
            <param-value>org.apache.struts2.showcase.person</param-value>
        </init-param>
    </filter>
  这个配置的意思就是会扫描org.apache.struts2.showcase.person包里所有实现了Action接口,或者以"Action"结尾的类,所以"newPerson!input.action"这个请求对应的类应该是NewPersonAction的input方法。

  我们再看看NewPersonAction.java这个文件
@ParentPackage("person")
public class NewPersonAction extends ActionSupport {

    private static final long serialVersionUID = 200410824352645515L;

    PersonManager personManager;
    Person person;

    public void setPersonManager(PersonManager personManager) {
        this.personManager = personManager;
    }

    public String execute() {
        personManager.createPerson(person);

        return SUCCESS;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}
  所谓的零配置就是把相关的注解写到Action类里,比如ParentPackage,Namespace,Result,Results
  例如:
@Namespace("/person")     //指定名称空间
@ParentPackage("person") //指定继承的包,struts-default无需指定
@Result("/success.jsp") //指定success,因为success是默认的,所以不用指定name
@Results({
    @Result("name="input", value="/input.jsp"),
    @Result("name="error", value="/error.jsp", type=ServletDispatcherResult.class)
    })
public class NewPersonAction extends ActionSupport {

    。。。。
}

  但是NewPersonAction.java里并没有指定result,那它是怎么转移的呢?这个是codebehind插件的作用。在struts.xml里有这样的设定,也就是说person这个包应用了codebehind的功能
<constant name="struts.codebehind.defaultPackage" value="person" />

 

 

 

按照以上参考资料,我将以前用xml配置action的测试项目修改如下:

 

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
        
        <init-param>          <!-- 添加使用注解必须的参数-->
        <param-name>actionPackages</param-name>
        <param-value>com.testman.action</param-value>  
        </init-param>
   
    </filter>

 

 

 

    在struts.xml中注释掉以前的配置信息

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- <constant name="struts.codebehind.defaultPackage" value="action" /> -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.serve.static.browserCache" value="false"/>
   <!-- <constant name="struts.action.extension" value="do"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true" /> -->
     <!-- 默认的视图主题 -->
    <constant name="struts.ui.theme" value="simple" />
    <!--下面表示action对象是spring负责创建的 -->
    <constant name="struts.objectFactory" value="spring" />
    
 	<!-- <package name="fundbalance" namespace="/fundbalance" extends="struts-default">
 		<global-results>
 			<result name="message">/WEB-INF/page/fundbalance/message.jsp</result>
 		</global-results> -->
		<!-- <action name="action_*" class="fundBalanceList" method="{1}">  -->         <!-- "fundBalanceList"对应的是spring容器中bean的名称 -->
		<!--	<result name="list">/WEB-INF/page/fundbalance/fundbalancelist.jsp</result>
			<result name="add">/WEB-INF/page/fundbalance/addfundbalance.jsp</result>
			<result name="update">/WEB-INF/page/fundbalance/updatefundbalance.jsp</result>
			<result name="query">/WEB-INF/page/fundbalance/fundbalancelist.jsp</result>
		</action> 
		
    </package> -->

</struts>

 

    action修改如下

 

package com.testman.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.opensymphony.xwork2.ActionSupport;
import com.testman.bean.FundBalance;
import com.testman.service.FundBalanceService;

@Namespace("/fundbalance")
@Results({
	@Result(name="list",value="/WEB-INF/page/fundbalance/fundbalancelist.jsp"),
	@Result(name="add",value="/WEB-INF/page/fundbalance/addfundbalance.jsp"),
	@Result(name="addUI",value="/WEB-INF/page/fundbalance/addfundbalance.jsp"),
	@Result(name="update",value="/WEB-INF/page/fundbalance/updatefundbalance.jsp"),
	@Result(name="updateUI",value="/WEB-INF/page/fundbalance/updatefundbalance.jsp"),
	@Result(name="query",value="/WEB-INF/page/fundbalance/fundbalancelist.jsp"),
	@Result(name="message",value="/WEB-INF/page/fundbalance/message.jsp")
})
//@Result("/index.jsp")
public class FundBalanceAction extends ActionSupport{
	@Resource FundBalanceService fundBalanceService;
	private String message;
	private List<FundBalance> fundbalances;
	private FundBalance fundbalance;
	private String delkeys;
	

	public FundBalance getFundbalance() {
		return fundbalance;
	}
	public void setFundbalance(FundBalance fundbalance) {
		this.fundbalance = fundbalance;
	}
	/**
	 */
	public String list(){
		this.fundbalances = fundBalanceService.getAllFundBalance();
		return "list";
	}
	/**
	 */
	public String addUI(){
		return "add";
	}
	
	public String updateUI(){
		fundbalance = fundBalanceService.queryFundBalanceById(this.fundbalance).get(0);
		return "update";
	}
	/**
	 */
	public String add(){
		this.fundBalanceService.save(this.fundbalance);
		this.message="数据保存成功!";
		return "message";
	}
	/**
	 */
	public String update(){
		this.fundBalanceService.update(this.fundbalance);
		this.message="数据更新成功!";
		return "message";
	}
	
	public String query(){
		this.fundbalances = fundBalanceService.queryFundBalanceByYearMonthItem(this.fundbalance);
		return "query";
	}
	
	public String del(){
		if(this.delkeys.contains(",")){
		
		String[] keys = this.delkeys.split(",");
	    for(String key : keys){
	    if(!"".equals(key)){
		this.fundBalanceService.delete(key);
		 }
	    }
	    }else{
	    this.fundBalanceService.delete(delkeys);	
	    }
		this.message="数据删除成功!";
		return "message";
	}

	
	public List<FundBalance> getFundbalances() {
		return fundbalances;
	}
	public void setFundbalances(List<FundBalance> fundbalances) {
		this.fundbalances = fundbalances;
	}
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public String getDelkeys() {
		return delkeys;
	}
	public void setDelkeys(String delkeys) {
		this.delkeys = delkeys;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	
}
 最后,页面上再把action的指向修改成 形如 f undBalance!add.action 即可。

你可能感兴趣的:(apache,spring,jsp,Web,struts)