SSH框架搭建过程---之Struts整合Spring框架(4)

来源:互联网 发布:淘宝企业店铺转让过程 编辑:程序博客网 时间:2024/05/23 00:08

Struts整合Spring框架

1.  导入整合用的jar

2.  配置web.xml

a)  指定Spring配置文件位置

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/ApplicationContext.xml</param-value>    </context-param>


b)  配置监听器

<listener>                    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>                </listener>


3.  编写修改ApplicationContext.xml(bean.xml),该文件位于WEB-INF目录中。

添加其代码如下:

…<bean id="demoAction"class="cn.edu.bucea.action.DemoAction">            <property name="demoService"ref="demoService" /></bean>…


4.  修改struts.xml文件,tihuanStruts的控制器类以完成整合。

修改后的代码如下:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts><constantname="struts.objectFactory"               value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>             <package name="demo" namespace="/demo"extends="struts-default">       <action name="Demo" class="demoAction">           <result name="success">/index.jsp</result>           <result name="input">/index.jsp</result>       </action>    </package></struts>


 

5.  接下来,依次逐级实现依赖注入。

例:在DemoAction.java中

private DemoService demoService;public void setDemoService(DemoService demoService) {       this.demoService = demoService;} @Override    public String execute(){       System.out.println(name + "\t" + password);       Demo demo = new Demo(0, name, password);       try {           demoService.createDemo(demo);       } catch (DemoExceptione) {           addActionError(e.getMessage());           return INPUT;       }       return SUCCESS;}


6.  启动服务器,在Demo.jsp中进行测试。

Demo.jsp<s:form action="Demo" method="post"namespace="/demo" theme="simple">        姓名:<s:textfield name="name" /><s:fielderror name="name"/><br /><br />        密码:<s:textfield name="password" /><s:fielderror name="password"/>      <s:submit value="提交"/></s:form>


 

7.  查看数据库表。

==========================================================

下一篇:SSH框架搭建过程---之Spring-Hibernate整合

0 0
原创粉丝点击