Spring 利用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.jcn.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.jcn.pojo.Newss;
import cn.jcn.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()
 造成这个问题的主要原因是是因为hiberante与spring开发包里面都配置有一个antlr的开发包,建议保留最新版本。

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

此时就完成了SSH的整合操作形式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值