Springmvc 整合 jetbrick 实例

应用环境: 
<jetbrick.version>1.2.8</jetbrick.version>

<antlr4-runtime.version>4.2.2</antlr4-runtime.version>

<spring.mybatis.common.version>2.0</spring.mybatis.common.version>

项目结构:
Springmvc 整合 jetbrick 实例

实例展示:

1.引如pom依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.spring.mybatis</groupId>
  <artifactId>jetbrick-mvc</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>jetbrick-mvc Maven Webapp</name>
  <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <slf4j.version>1.7.7</slf4j.version>
        <junit.version>4.11</junit.version>
        <http.components.version>4.1</http.components.version>
        <commons.logging.version>1.1.3</commons.logging.version>
        <commons.codec.version>1.7</commons.codec.version>
        <jetbrick.version>1.2.8</jetbrick.version>
        <antlr4-runtime.version>4.2.2</antlr4-runtime.version>
        <servlet.version>2.4</servlet.version>
    </properties>
    <dependencies>
        
        <dependency>
            <groupId>jetbrick-template</groupId>
            <artifactId>jetbrick-template</artifactId>
            <version>${jetbrick.version}</version>
        </dependency>

        <dependency>
            <groupId>com.spring.mybatis</groupId>
            <artifactId>spring-mybatis-data-common</artifactId>
            <version>2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-servlet_3.0_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>${antlr4-runtime.version}</version>
        </dependency>
        
        <dependency>
            <groupId>servlet-api</groupId>
            <artifactId>servlet-api</artifactId>
            <version>${servlet.version}</version>
        </dependency>
    </dependencies>
  <build>
    <finalName>jetbrick-mvc</finalName>
  </build>
</project>

2.添加jetbrick的监听与配置文件到web.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">

    <!--Spring ApplicationContext context listener -->  
    <listener>     
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
    </listener>
    
    <!-- config Spring context -->
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
         <!-- if we set "/WEB-INF/xx", here should be "/WEB-INF/applicationContext.xml" -->
        <param-value>classpath*:context/spring-context.xml</param-value>  
    </context-param>  
    
 
    <!-- Spring refresh Introspector -->  
    <listener>  
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
    </listener>  
  
      
      <!-- set jetbrick listener -->
    <listener>
      <listener-class>jetbrick.template.web.JetWebEngineLoader</listener-class>
    </listener>
      
      <!-- set load jetbrick-template properties -->
    <context-param>
      <param-name>jetbrick-template-config-location</param-name>
      <param-value>classpath:template/jetbrick-template.properties</param-value>
    </context-param>
    
    <!-- Spring log4j listener -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
     
    <!-- set load log4j properties-->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:property/log4j.properties</param-value>
    </context-param>
     
       <!-- character Encoding filter -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    
    <!-- Spring Modelv view Controller -->
    <servlet>
        <servlet-name>spring-controller</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:context/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-controller</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!--set session timeout -->
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
         
</web-app>

3.配置jetbrick的全局属性

import.packages = com.mvc.cmrn.dao, com.mvc.cmrn.model
#import inner define function,once import use anywhere 
import.functions = jetbrick.template.runtime.JetFunctions
#set autoscan is true
import.autoscan = true
#set autoscan package
import.autoscan.packages = com.mvc.cmrn
#set reloadable is true
template.reloadable = true
input.encoding= UTF-8
output.encoding=UTF-8
template.reloadable = true
#template.path  +  spring prefix +   VIEW + spring suffix (or?? template.suffix)
template.path=/WEB-INF/jetx/
compile.debug = true
template.loader = jetbrick.template.web.WebResourceLoader
#more config read :http://subchen.github.io/jetbrick-template/config.html

4.定义jetbrick为应用试图

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

     <!-- set annotation support is true -->  
    <mvc:annotation-driven />  
    
    <!-- autoscan @Component , @Controller , @Service , @Repository ... annotation -->
    <context:component-scan base-package="com.mvc.cmrn.controller" />  
   
    <!-- load static resources -->
    <mvc:resources mapping="/scripts/**" location="/scripts/"/> 
    <mvc:resources mapping="/styles/**"  location="/styles/" />
    
    
    <!-- define jdp for spring Model view
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />     
        <property name="prefix" value="/WEB-INF/jetx/" />
        <property name="suffix" value=".jsp" />     
    </bean>
    -->
 
    <!-- define view with jetbrick template -->
    <bean class="jetbrick.template.web.springmvc.JetTemplateViewResolver">
        <!-- 如果spring 的suffix 没有配置,会读取 template.suffix 这个不是叠加.是 覆盖(spring 的优先) -->
         <property name="suffix" value=".baidu" />
         <property name="contentType" value="text/html; charset=UTF-8" />
         <property name="order" value="9999" />
    </bean>
    
</beans>

5.springmvc操作

package com.mvc.cmrn.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.cmrn.model.Member;
import com.mvc.cmrn.service.IMemberService;
import com.spring.mybatis.data.common.controller.BaseMultiController;


@Controller
@RequestMapping("/member")
public class MemberController extends BaseMultiController{

    @Autowired
    private IMemberService memberService;
    
    @RequestMapping(value={"/{id:\\d+}/query.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView query(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("id") Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        Member member = this.memberService.query(id);
        map.put("member", member);
        String viewPath = "front/detail";
        return toView(viewPath, map);
    }
    
    @RequestMapping(value={"/lists.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView lists(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Member> lists = this.memberService.list();
        map.put("item", lists);
        String viewPath = "front/list";
        return toView(viewPath, map);
    }

    @RequestMapping(value={"/{id:\\d+}/delete.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView delete(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("id") Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        int result = this.memberService.delete(id);
        if(result>0){
            map.put("message", "Delete OK");
        }else{
            map.put("message", "Delete Error");
        }
        List<Member> lists = this.memberService.list();
        map.put("item", lists);
        String viewPath = "front/list";
        return toView(viewPath, map);
    }
    
    
    public IMemberService getMemberService() {
        return memberService;
    }

    public void setMemberService(IMemberService memberService) {
        this.memberService = memberService;
    }
    
    
}
package com.mvc.cmrn.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.mvc.cmrn.model.Member;
import com.mvc.cmrn.service.IMemberService;
import com.spring.mybatis.data.common.controller.BaseMultiController;


@Controller
@RequestMapping("/member")
public class MemberController extends BaseMultiController{

    @Autowired
    private IMemberService memberService;
    
    @RequestMapping(value={"/{id:\\d+}/query.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView query(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("id") Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        Member member = this.memberService.query(id);
        map.put("member", member);
        String viewPath = "front/detail";
        return toView(viewPath, map);
    }
    
    @RequestMapping(value={"/lists.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView lists(HttpServletRequest request,HttpServletResponse response){
        Map<String,Object> map = new HashMap<String, Object>();
        List<Member> lists = this.memberService.list();
        map.put("item", lists);
        String viewPath = "front/list";
        return toView(viewPath, map);
    }

    @RequestMapping(value={"/{id:\\d+}/delete.baidu"},method={RequestMethod.GET,RequestMethod.POST})
    public ModelAndView delete(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("id") Long id){
        Map<String,Object> map = new HashMap<String, Object>();
        int result = this.memberService.delete(id);
        if(result>0){
            map.put("message", "Delete OK");
        }else{
            map.put("message", "Delete Error");
        }
        List<Member> lists = this.memberService.list();
        map.put("item", lists);
        String viewPath = "front/list";
        return toView(viewPath, map);
    }
    
    
    public IMemberService getMemberService() {
        return memberService;
    }

    public void setMemberService(IMemberService memberService) {
        this.memberService = memberService;
    }
    
    
}

6.获取应用路径

在全局属性配置文件jetbrick-template.properties中引入的jetbrick模板中自定义的函数.通过函数webroot()获取路径.

<table border="1" width="600">
  <tr>
        <td>ID</td>
        <td>姓名</td>
        <td>邮箱</td>
        <td>查看</td>
        <td>删除</td>
  </tr>
  #for(Member member: item)
  <tr>
        <td>${user.id}</td>
        <td>${user.name}</td>
        <td>${user.email}</td>
        <td><a href="${webroot()}/member/${user.id}/query.baidu">查看</a></td>
        <td><a href="${webroot()}/member/${user.id}/delete.baidu">删除</a></td>
  </tr>
  #end
</table>

语法说明: http://subchen.github.io/jetbrick-template/syntax.html
实例下载: http://files.cnblogs.com/dennisit/springmvc-jetbrick-demo.7z
转载请注明出处:[http://www.cnblogs.com/dennisit/p/3812456.html]

Springmvc 整合 jetbrick 实例,布布扣,bubuko.com

Springmvc 整合 jetbrick 实例

上一篇:原生javascript兼容性问题


下一篇:Java_NIO基础分析-1