spring+jpa+strut1集成

a.完成spring+jpa的集成并测试成功

b.导入strut1所用的jar包

c.在web.xml添加spring的启动配置代码:

View Code
1 <context-param>
2 <param-name>contextConfigLocation</param-name>
3 <param-value>classpath:beans.xml</param-value>
4 </context-param>
5 <listener>
6 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
7 </listener>

d.在web.xml中添加strut1的启动配置代码:

View Code
 1 <servlet>
2 <servlet-name>struts</servlet-name>
3 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
4 <init-param>
5 <param-name>config</param-name>
6 <param-value>/WEB-INF/struts-config.xml</param-value>
7 </init-param>
8 <load-on-startup>0</load-on-startup>
9 </servlet>
10 <servlet-mapping>
11 <servlet-name>struts</servlet-name>
12 <url-pattern>*.do</url-pattern>
13 </servlet-mapping>

e.在web.xml中添加解决strut1乱码的配置代码:

View Code
 1 <filter>
2 <filter-name>encoding</filter-name>
3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
4 <init-param>
5 <param-name>encoding</param-name>
6 <param-value>UTF-8</param-value>
7 </init-param>
8 </filter>
9 <filter-mapping>
10 <filter-name>encoding</filter-name>
11 <url-pattern>/*</url-pattern>
12 </filter-mapping>

f.在web.xml中添加解决EntityManager已关闭异常的配置代码:

View Code
1  <filter>
2 <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
3 <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
4 </filter>
5 <filter-mapping>
6 <filter-name>Spring OpenEntityManagerInViewFilter</filter-name>
7 <url-pattern>/*</url-pattern>
8 </filter-mapping>

g.新建如下的配置文件/WEB-INF/struts-config.xml:

View Code
 1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
4 "http://struts.apache.org/dtds/struts-config_1_3.dtd">
5 <struts-config>
6 <form-beans>
7 <form-bean name="personForm" type="cnblogs.xiaoqiu.formbean.PersonForm"></form-bean>
8 </form-beans>
9 <action-mappings>
10 <action path="/person/list" validate="false">
11 <forward name="list" path="/WEB-INF/page/list.jsp"></forward>
12 </action>
13 <action path="/person/save" validate="false" parameter="method" scope="request" name="personForm">
14 <forward name="message" path="/WEB-INF/page/message.jsp"></forward>
15 </action>
16 </action-mappings>
17 <controller>
18 <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
19 </controller>
20 </struts-config>

h.搭建一个测试例子(通过save.jsp向数据库中保存一个Person对象):

jsp:

View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 </head>
7 <body>
8 <html:form action="/person/save" method="post">
9 name:<html:text property="name"/>
10 <input type="submit" value="提交"/>
11 <input type="hidden" name="method" value="add"/>
12 </html:form>
13 </body>
14 </html>

action:

View Code
 1 @Component("/person/save")
2 public class PersonManager extends DispatchAction {
3 @Resource PersonService personService;
4 public ActionForward add(ActionMapping mapping, ActionForm form,
5 HttpServletRequest request, HttpServletResponse response)
6 throws Exception {
7 PersonForm personForm =(PersonForm)form;
8 personService.save(new Person(personForm.getName()));
9 request.setAttribute("message", "保存成功");
10 return mapping.findForward("message");
11 }
12 }

struts-config中的配置:

View Code
1 <action path="/person/save" validate="false" parameter="method" scope="request" name="personForm">
2 <forward name="message" path="/WEB-INF/page/message.jsp"></forward>
3 </action>




posted @ 2012-03-25 12:43  xiao秋  阅读(780)  评论(0编辑  收藏  举报