spring JPA struts 整合开发(2) - spring集成struts

[url="http://czj4451.iteye.com/blog/1535731"]一. spring JPA struts 整合开发(1) - spring集成JPA[/url]
二. spring JPA struts 整合开发(2) - spring集成struts


[size=medium]1. 在web.xml中加入struts配置和spring实例化配置[/size]


<!-- Specify the config file of spring, would search the web root folder by default, -->
<!-- It's practical to use classpath provided by spring to search from source folder -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>

<!-- Instantiate the spring container -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Register struts as a servlet -->
<servlet>
<servlet-name>struts</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>struts</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>


[size=medium]2. 拷贝struts-config.xml到/WEB-INF目录下:[/size]


<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">

<struts-config>
<action-mappings>
<!-- Let the action be managed by spring, thus the type property is removed -->
<action path="/person/list">
<forward name="list" path="/WEB-INF/pages/person_list.jsp"/>
</action>
</action-mappings>
<controller>
<!-- A process that will fetch the instance of the specific action from spring by value of the path -->
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
</struts-config>


在beans.xml中,加入:

<!-- Let spring manage the action -->
<bean name="/person/list" class="com.john.web.action.PersonListAction"/>


[size=medium]3. com.john.web.action.PersonListAction类:[/size]


public class PersonListAction extends Action {
// Now that the action is instantiated by spring, which will inject the property with instance
@Resource PersonService personService;

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setAttribute("list", personService.getPersons());
return mapping.findForward("list");
}
}


[size=medium]4. 创建result.jsp[/size]


<body>
${message}
</body>


[size=medium]5. 测试[/size]

在web server上运行该项目,往数据库插入几条数据。
输入http://localhost:[port name]/[project name]/person/list.do,查看效果

[size=medium]6. struts1.3的中文乱码问题以及因session关闭导致的延迟加载例外问题[/size]

参看:[url="http://czj4451.iteye.com/blog/1535279"]spring hibernate struts 整合开发(6) - 额外功能[/url]

整理自:传智播客spring教程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值