最近学习传智播客的巴巴运动网教程,在集成Struts与Spring时,遇到很多麻烦,千万要沉住气,不要浮躁,开始的时候,一集成struts2就出现各种各样的问题,怎么都解决不了,看了很多帖子,但大多数都是水贴。在这里很想说,千万别用百度查,一定要用google,特别是技术上的问题,看看外国朋友怎么解决问题的。在此记录,希望对以后遇到同样问题的人有所帮助。教程中集成的是Struts1,我选择的是Struts2。3。两者还是有所不同的。比如,默认情况下,Struts1的action使用单例模式,容易引起线程安全问题;Struts2使用的是原型模式,不存在线程安全问题;框架使用起来很方便,前提是你得配置好。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
ProductTypeAction.java
package com.web.action.product; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.springframework.stereotype.Controller; import com.bean.product.ProductType; import com.opensymphony.xwork2.ActionSupport; import com.service.product.ProductTypeService; @Controller public class ProductTypeAction extends ActionSupport { private static final long serialVersionUID = -566112034741430939L; @Resource(name="productTypeServiceBean") private ProductTypeService productTypeService; private ProductType productType; @Override public String execute() throws Exception { productType = productTypeService.find(ProductType.class, 3); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("producttype", productType); return SUCCESS; } }
这里应该注意两点,如果不采用@Resource方式注入bean的话,请参考注释中的代码,重写构造一个bean。使用@Controller注解,Spring将扫描指定的路径获得需要的资源,这里注意如果@Controller使用默认配置,那么struts.xml文件中action的class属性应该与这里的ProductTypeAction.java名字一致。如果这里不使用注解的方式,可以参考注释中的代码,相应的在配置文件struts.xml
中也不需要那么复杂,具体请自己思考减少配置代码。
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="type" class="productTypeAction" > <result>/test.jsp</result> </action> </package> </struts>
test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${producttype.name} </body> </html>
其余的代码,请参考http://blog.csdn.net/zsm653983/article/details/8161163
巴巴运动网学习笔记三之产品分类实体对象基本属性的JPA映射,用泛型技术对产品分类的业务管理Bean抽象,单元测试产品分类的业务管理Bean.,重载业务管理Bean的删除方法,
出现的各种问题,可以到网上找,上一篇我也写了一些自己遇到的问题和解决方法。可能各种问题大家遇到不一样,但不要慌。