[중 얼, 교장] Spring + Struts 2 + Jquery Jquery Ajax 요청 action json 데이터 인 스 턴 스 획득

1. 새 항목 에 해당 하 는 jar 패키지 가 져 오기
2. 웹. 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. sturts 2 설정 파일, src 디 렉 터 리 아래 struts 2. 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  、
	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);}
})

좋은 웹페이지 즐겨찾기