日期:2014-05-16 浏览次数:20392 次
1、新建项目导入相应的jar包
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!-- struts过滤器 --> <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> <!-- spring配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- applicationContext.xml、spring 容器配置文件、applicationContext-*.xml(srping bean配置文件)、分开写便于管理--> <param-value>/WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/applicationContext-*.xml</param-value> </context-param> <!-- spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
?3、配置sturts2配置文件、位于src目录下面struts2.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 将struts的action委托给spring管理 --> <constant name="struts.objectFactory" value="spring"></constant> <!-- action包配置、使用json的包、需要struts2-json-plugin-2.3.4.jar包,版本可以换 --> <package name="default" namespace="/ajax" extends="json-default"> <!-- 配置action、由于使用spring管理action,所有class的值为spring的bean --> <action name="jokeAction" class="jokeAction_Spring" method="findJokeList"> <result type="json"> <!-- list是我们需要返回数据,acrion会自动封装--> <param name="root">list</param> </result> </action> </package> </struts>
?4、配置spring的容器applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <!-- 使用setter注入方式、自动装载 --> <!--使用本地jdbc配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/classes/dataSource.properties" /> </bean> <!-- 配置c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.pass}"></property> </bean> <!--不同类型的bean配置文件、分开写、便于管理 --> <import resource="applicationContext-action.xml"/> <import resource="applicationContext-service.xml"/> <import resource="applicationContext-dao.xml"/> </beans>
?5、测试action类
import java.util.List; import net.sf.json.JSONArray; import com.master.service.IJokeService; import com.opensymphony.xwork2.ActionSupport; public class JokeAction extends ActionSupport{ //业务逻辑类、setter注入 public IJokeService jokeService; //需要返回的json对象、 pub