Spring整合Struts2
1:struts2-spring-plugin Spring整合Struts2所必须的jar包
2:org.springframework.expression.PropertyAccessor缺少org.springframework.expression jar包
3:web工程导入jar包不能用 build path的user library的方式
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SSH_Test</display-name> <!-- spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- stutst2核心拦截器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置spring文件路径 --> <!-- <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext-*.xml</param-value> </context-param> --> </web-app>
aplicationContext.xml(WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean name="getListAction" class="com.lee.action.GetListAction"> <property name="iocList"> <bean class="com.lee.service.impl.GetListServiceImpl" /> </property> </bean> </beans>
struts.xml
<?xml version="1.0" encoding="GBK"?> <!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.objectFactory" value="spring"></constant> <constant name="struts.custom.i18n.resources" value="mess"></constant> <constant name="struts.i18n.encoding" value="GBK"></constant> <package name="lee" extends="struts-default"> <action name="getList" class="getListAction"> <result>/index.jsp</result> </action> </package> </struts>
action
package com.lee.action; import java.util.List; import com.lee.service.GetListService; import com.opensymphony.xwork2.ActionSupport; public class GetListAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<String> list; private GetListService iocList; public String execute() throws Exception { this.setList(iocList.getList()); return super.execute(); } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public GetListService getIocList() { return iocList; } public void setIocList(GetListService iocList) { this.iocList = iocList; } }
service
package com.lee.service; import java.util.List; public interface GetListService { public List<String> getList(); }
serviceImpl
package com.lee.service.impl; import java.util.ArrayList; import java.util.List; import com.lee.service.GetListService; public class GetListServiceImpl implements GetListService { @Override public List<String> getList() { List<String> list = new ArrayList<String>(); list.add("strtuts2"); list.add("spring"); list.add("hibernate"); return list; } }