(十五)Struts2.x多业务处理

        所有的开发之中,每一个Action都要同时处理多个操请求,所以来讲在Struts2.x里面依然处理多业务的操作.那么下面为了更好的观察出问题,重新建立新的Action以及vo类.

范例:定义一个新闻的VO类

package cn.zwb.vo;

public class News {
	private Integer nid;
	private String title;
	private String content;
	public Integer getNid() {
		return nid;
	}
	public void setNid(Integer nid) {
		this.nid = nid;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	@Override
	public String toString() {
		return "News [nid=" + nid + ", title=" + title + ", content=" + content + "]";
	}
	
}    

        随后建立一个NewsAction,这个Action要求可以进行多业务的处理,如果写多业务处理的话就不要使用execute()方法

范例:定义NewsAction

package cn.zwb.action;

import com.opensymphony.xwork2.ActionSupport;

import cn.zwb.vo.News;

public class NewsAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private News news=new News();
	public News getNews() {
		return news;
	}
	public String insert(){  //增加操作但是不跳转
		System.out.println("***********[增加新闻]"+this.news);
		return null;
	}
	public void update(){
		System.out.println("***********[修改新闻]"+this.news);
	}
	public void list(){
		System.out.println("***********[新闻列表]"+this.news);
	}
	public void delete(){
		System.out.println("***********[删除新闻]"+this.news);
	}
}

        随后进行struts.xml文件的配置操作.

范例:定义struts.xml文件

<action name="NewsActon_*" class="cn.zwb.action.NewsAction" method="{1}">
		
	</action>

        如果现在要只执行增加操作,则路径使用:NewsAction_insert.action,如果是修改操作使用NewsAction_update.action,依次类推.

        可以发现在Struts2.x之中,每一次执行的时候是几上都会重新生成新的Action对象,所以来讲里面的内容不会从上一次的内容延用到本次请求的情况.

        如果说此时你的项目开发是基于配置文件完成的,这样的配置完全可用,而且可以发现在整个Action是否要返回结果都由用户自己来决定.可是在现在的开发之中(Struts2.x比Struts1.x最 优秀的地方在于它支持Annotation配置)都会使用Annotation来完成,一旦使用了Annotation,那么无法配置method参数.如果现在无法配置method参数,那么我们就需要换另外一种方式访问.

<action name="NewsAction" class="cn.zwb.action.NewsAction" >
	</action>
        此时可以利用"!"完成,例如:要访问增加操作"NewsAction!insert.action",修改:"NewsAction!update.action"

总结

        可以发现在Struts2.x中的多业务处理是现在为止遇见的最简单,而且要求最不固定的实现形式,例如:你的方法可以根据你自己的需要选择是否抛出异常,是否返回跳转


        

猜你喜欢

转载自blog.csdn.net/qq1019648709/article/details/80568995
今日推荐