【叨、校长】Spring+Struts2+Jquery Jquery Ajax请求action获得json数据实例
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" 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" value="/WEB-INF/classes/dataSource.properties" /></bean><!-- 配置c3p0数据源 --><bean id="dataSource" 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对象、public List<String> list;public void setJokeService(IJokeService jokeService) {this.jokeService = jokeService;}//action的主体方法public String findJokeList(){System.out.print("测试数据");list=jokeService.findJokeList();return SUCCESS;}/** * @return the list */public List<String> getList() {return list;}/** * @param list the list to set */public void setList(List<String> list) {this.list = list;}/** * @return the jokeService */public IJokeService getJokeService() {return jokeService;}}
?6、前端请求action、这里使用Jquery的ajax(这个方便实用)
$.ajax({url:"/MasterWork/ajax/jokeAction.action",type:"get",success:function(data,textStatus,jq){ /*data、就是ajax的返回值、一个json对象*/ },error:function(data,textStatus,jq){alert(2);}})?