(三十四)利用MyEclipse整合spring与Struts2.x

    1.为项目添加Struts2.x的开发支持;

        添加支持的时候一定要选择好相应的开发包,因为需要整合Spring问题。


2.Spring如果要想在WEB中使用,那么必须为其添加一个监听器;

            名称:org.springframework.web.context.ContextLoaderListener

Myelipse已经自动为我们添加了相应的监听器支持


        ●需要将Spring的核心配置文件:applicationContext.xml文件设置到web环境之中


        以上代码所表示的含义是:将路径的信息设置为application属性范围

3.建立struts.properties文件,这个文件用于设置对象工厂;

定义struts.properties;

struts.i18n.encoding=UTF-8
struts.custom.i18n.resoures=Message,Pages

struts.objectFactory=spring

        WebWork最初设计的时候这一点上就比Srruts1.x优秀.

4.定义一个根包,修改struts.xml文件

范例:定义NewsAction.java程序

package cn.zwb.action;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.stereotype.Repository;

import com.opensymphony.xwork2.ActionSupport;

import cn.zwb.pojo.Newss;
import cn.zwb.service.INewsService;
@Repository
@ParentPackage("root")  //继承父接口
@Namespace("/pages/news")
@Action(value="NewsAction")
@SuppressWarnings("serial")
public class NewsAction extends ActionSupport{
	@Resource
	private INewsService newsService;
	private Newss news =new Newss();
	public Newss getNews() {
		return news;
	}
	public void insert(){
		System.out.println("[新闻数据增加]数据:" +this.news);
		try {
			System.out.println("[新闻数据增加]业务调用:"+this.newsService.insert(this.news));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void update(){
		System.out.println("[新闻数据修改]数据:" +this.news);
		try {
			System.out.println("[新闻数据修改]业务调用:"+this.newsService.update(this.news));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void delete(){
		String ids=ServletActionContext.getRequest().getParameter("ids");
		System.out.println("[新闻数据修改]数据:" +this.news);
		try {
			Set<Integer> set=new HashSet<Integer>();
			String result[]=ids.split(",");
			for (String string : result) {
				set.add(Integer.parseInt(string));
			}
			System.out.println("[新闻数据删除]业务调用:"+this.newsService.delete(set));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void get(){
		System.out.println("[新闻数据取得]数据:" +this.news);
		try {
			System.out.println("[新闻数据取得]业务调用:"+this.newsService.get(this.news.getNid()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void list(){
		System.out.println("[新闻数据查询]数据list:" +this.news);
		try {
			System.out.println(this.newsService.list());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void listSplit(){
		System.out.println("[新闻数据查询]数据list:" +this.news);
		try {
			System.out.println(this.newsService.list("title","",1,5));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

随后对数据进行测试.所有的测试都通过地址传参的方式处理

        ●增加数据:localhost:8080/SSHProject/pages/news/NewsAction!insert.action?news.title=世界你好呀&news.content=今天天气不错

        ●查询全部数据:http://localhost:8080/SSHProject/pages/news/NewsAction!list.action

        ●修改数据localhost:8080/SSHProject/pages/news/NewsAction!update.action?news.title=ssh修改!!!!!&news.content=今天天气不错&news.nid=2

            但是在修改的时候会出现以下错误信息,

java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
            造成这个问题的主要原因是是因为hiberante与spring开发包里面都配置有一个antlr的开发包,建议保留最新版本


       删除操作: localhost:8080/SSHProject/pages/news/NewsAction!delete.action?news.nid=2,4,5


猜你喜欢

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