<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <context-param> <param-name>resteasy.servlet.mapping.prefix</param-name> <param-value>/rest</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <listener> <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class> </listener> <listener> <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>RestEasy</servlet-name> <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestEasy</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> <!-- 编码 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="lmx.phone" /> </beans>
package lmx.phone.service; import javax.ws.rs.GET; import javax.ws.rs.Path; import lmx.phone.util.ExtJSResponse; import org.springframework.stereotype.Service; @Service @Path("esper") public class EsperService { @Path("phone") @GET public ExtJSResponse phoneService() { System.out.println("rest works"); return null; } }
package lmx.phone.util; import java.util.HashMap; /** * used to integrates with ExtJS 4.x data stores.<br> * In ExtJS, the data store use following format to verify the result: * <p/> * <pre> * "{"success":false,"data":"","message":"VERBOSE ERROR"}" * </pre> * * @author MiXian */ public class ExtJSResponse extends HashMap<String, Object> { /** * */ private static final long serialVersionUID = -2791356338016228077L; public static ExtJSResponse successRes() { return new ExtJSResponse(true); } public static ExtJSResponse successRes4Find(Object data,Integer total) { ExtJSResponse res = new ExtJSResponse(true); res.setData(data); res.put("total",total); return res; } public static ExtJSResponse successResWithData(Object data) { ExtJSResponse res = new ExtJSResponse(true); res.setData(data); return res; } public static ExtJSResponse errorRes(String error) { ExtJSResponse res = new ExtJSResponse(false); res.setErrorMsg(error); return res; } public ExtJSResponse() { } public ExtJSResponse(boolean success) { super(); setSuccess(success); } public boolean isSuccess() { return (Boolean) get("success"); } public void setSuccess(boolean success) { put("success", success); } public void setErrorMsg(String errorMsg) { put("error", errorMsg); } public String getErrorMsg() { return (String) get("error"); } public void setData(Object data) { put("data", data); } public Object getData() { return get("data"); } }
关于前台和后台之间的交互:
1、extjs发送ajax请求,以“/rest/*”的形式。
2、web.xml捕捉到请求之后,将请求发给DispatherServlet
3、DispatherServlet将请求发给配置的package
4、package扫描所有的“@service”类,寻找匹配的方法来处理请求,并返回给前端一个json数据串。json数据串通过ExtjsResonse来生成一个基本的内容。
5、前端根据json数据串来改变前端的内容