json-struts是一個實現了任意java對象自動轉換JSON 參數自動匹配和賦值的j2ee框架。其項目源碼放在svn://www.svn999.com/opopop880.json_struts,用戶名anyoneuser 密碼anyoneuser ;主要是運用於移動開發後臺。提交的前端提交的請求不區分GET還是POST,框架能夠完成自動賦值給Java Bean參數,支持屬性模型和域模型;能夠與Spring集成;支持自定義訪問URL;所有的結果統一轉換到JSON返回,支持自定義轉換格式。
json-struts必須依賴項目reflectASM和fastJSON。
最新jar包下載地址 json-struts_2.0.jar
新建J2EE WEB工程web 下載jar包拷貝到WEB_INF下Lib文件夾,修改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>web</display-name> <!-- 核心過濾器 --> <filter> <filter-name>dispatcher</filter-name> <filter-class>org.jsonstruts.filter.DispatcherFilter</filter-class> <init-param> <param-name>isEnableSpring</param-name> <param-value>false</param-value> </init-param> </filter> <filter-mapping> <filter-name>dispatcher</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
核心過濾器DispatcherFilter中間增加了頻繁請求過濾請求,能夠阻止用戶頻繁刷新頁面或者使用壓力測試工具攻擊!新建java類User
package test; public class User { public String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
新建訪問類Index
package test; import org.jsonstruts.annotation.WebUrl; @WebUrl("/index") public class Index{ private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Object execute() { User user = new User(); user.setName(name); user.setAge(age); return user; } }
然後啓動項目,查看啓動日誌,查看訪問URL映射
十二月 11, 2014 2:43:13 下午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\soft\eclipse-jee-kepler-SR2-win32\eclipse\jre\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:/soft/eclipse-jee-kepler-SR2-win32/eclipse/jre/bin/client;D:/soft/eclipse-jee-kepler-SR2-win32/eclipse/jre/bin;D:/soft/eclipse-jee-kepler-SR2-win32/eclipse/jre/lib/i386;E:\oracle\product\10.2.0\client_3\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\MySQL\MySQL Server 5.2\bin;C:\Program Files\WPS Office\9.1.0.4866\office6;D:\soft\eclipse-jee-kepler-SR2-win32\eclipse;;. 十二月 11, 2014 2:43:14 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:web' did not find a matching property. 十二月 11, 2014 2:43:14 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["http-nio-8080"] 十二月 11, 2014 2:43:15 下午 org.apache.tomcat.util.net.NioSelectorPool getSharedSelector 信息: Using a shared selector for servlet write/read 十二月 11, 2014 2:43:15 下午 org.apache.coyote.AbstractProtocol init 信息: Initializing ProtocolHandler ["ajp-bio-8009"] 十二月 11, 2014 2:43:15 下午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 1757 ms 十二月 11, 2014 2:43:15 下午 org.apache.catalina.core.StandardService startInternal 信息: Starting service Catalina 十二月 11, 2014 2:43:15 下午 org.apache.catalina.core.StandardEngine startInternal 信息: Starting Servlet Engine: Apache Tomcat/7.0.53 十二月 11, 2014 2:43:18 下午 org.apache.catalina.core.ApplicationContext log 信息: No Spring WebApplicationInitializer types detected on classpath 十二月 11, 2014 2:43:19 下午 org.jsonstruts.filter.DispatcherFilter init 信息: Init DispatcherFilter... 十二月 11, 2014 2:43:19 下午 org.jsonstruts.filter.Dispatcher doProcess 信息: Mapping url '/index' to handle 'test.Index' 十二月 11, 2014 2:43:19 下午 org.jsonstruts.filter.Dispatcher doProcess 信息: Mapping url '/index2' to handle 'test.Index2' 十二月 11, 2014 2:43:19 下午 org.jsonstruts.filter.DispatcherFilter init 信息: 'isEnableSpring' not be setted to 'true',Spring module shutdown... 十二月 11, 2014 2:43:19 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["http-nio-8080"] 十二月 11, 2014 2:43:19 下午 org.apache.coyote.AbstractProtocol start 信息: Starting ProtocolHandler ["ajp-bio-8009"] 十二月 11, 2014 2:43:19 下午 org.apache.catalina.startup.Catalina start 信息: Server startup in 4016 ms
訪問http://localhost:8080/web/Index?name=zhangsan&age=1
輸出結果{"age":1,"name":"zhangsan"}