json-struts使用教程(1)-初识json-struts

 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"}

 
  

猜你喜欢

转载自blog.csdn.net/opopop880/article/details/41863259
今日推荐