MVC 框架 fw4jREST

GPL
Java Groovy
跨平台
2015-03-31
独特程序

【年终提升】2019尾声,来 OSC·年终盛典收割技术干货,get新技能!>>>

fw4jREST 是以 REST 风格设计的 MVC 框架,可以使用 Java 和 Groovy 写 Controller,使用 Java 注解声明Controller 层。

fw4jREST 是基于 fw4j 编写的,去掉了springmvc的框架注解命名方式,增加使用Groovy语言编写Controller的支持,去掉配置文件的编写,框架启动后会扫描项目下所有目录。

fw4jREST 包括的注解有:

  • Fw4jController

  • GET

  • POST

  • PUT

  • DELETE

代码示例:

java:


@Fw4jController("/admin")
public class AdminController {
	
	@GET("/index")
	public String index(OutParam out){
		JSHelper js = new JSHelper("nav/nav.js");
		String a = js.getMethod("nav").invoke();
		System.out.println(a);
		out.put("nav", a);
		return "request:index";
	}
}

groovy:

@Fw4jController("/websitetype")
class WebsiteTypeController{
	
	WebsiteTypeService websiteTypeService = null
	
	@Fw4jSpringIoc("WebsiteTypeService")
	void setWebsiteService(WebsiteTypeService websiteTypeService){
		this.websiteTypeService = websiteTypeService
	}
	
	
	
	@POST("/add")
	String add(InParam inParam){
		WebsiteType wst = new WebsiteType()
		wst.name = inParam['name']
		websiteTypeService.save(wst)
		"redirect:/WebRoot/websitetype/show"
	}
	
	@GET("/del")
	String del(InParam inParam){
		def id = Integer.parseInt(inParam['id'])
		websiteTypeService.del(id)
		"redirect:/WebRoot/websitetype/show"
	}
	
	@PUT("/total")
	String totalAdd(InParam inParam){
		def id = inParam['id']
		WebsiteType wst = this.websiteTypeService.get(id)
		wst.websiteTotalNum ++
		this.websiteTypeService.save(wst)
		"redirect:/WebRoot/websitetype/show"
	}
}
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" 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>fw4j</display-name>
  <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>
  <servlet-mapping>
   <servlet-name>fw4j</servlet-name>
   <url-pattern>/</url-pattern>
  </servlet-mapping>
  <servlet>
 	 <servlet-name>fw4j</servlet-name>
 	 <servlet-class>com.cn.fw4j.mvc.servlet.DispatcherServlet</servlet-class>
 	 <load-on-startup>1</load-on-startup>
  </servlet>
 <servlet>
 	<servlet-name>static</servlet-name>
 	<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
 </servlet> 
  <servlet-mapping>
  	<servlet-name>static</servlet-name>
  	<url-pattern>/static/*</url-pattern>
  </servlet-mapping>
 <servlet-mapping>
  	<servlet-name>static</servlet-name>
  	<url-pattern>*.html</url-pattern>
  </servlet-mapping>	    
	    
</web-app>




的码云指数为
超过 的项目
加载中

评论(0)

暂无评论

暂无资讯

暂无问答

暂无博客

返回顶部
顶部