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

一. spring JPA struts 整合开发(1) - spring集成JPA
二. spring JPA struts 整合开发(2) - spring集成struts



1. 在web.xml中加入struts配置和spring实例化配置

  <!-- 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>


2. 拷贝struts-config.xml到/WEB-INF目录下:

<?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"/>


3. com.john.web.action.PersonListAction类:

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");
	}
}


4. 创建result.jsp

<body>
	${message}
</body>


5. 测试

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

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

参看: spring hibernate struts 整合开发(6) - 额外功能

整理自:传智播客spring教程

猜你喜欢

转载自czj4451.iteye.com/blog/1535749