Code Monkey home page Code Monkey logo

jrouter-servlet's Introduction

jrouter-servlet 是一个基于jrouter的Servlet组件;提供快速的路由定位、方法调用、参数绑定等功能。其核心设计目标即小而精、代码少、轻量级、易扩展、Restful。

● require jdk 1.8+

changelog

Maven:

<dependency>
    <groupId>net.jrouter</groupId>
    <artifactId>jrouter-servlet</artifactId>
    <version>1.8.7</version>
</dependency>

JavaConfig:

import javax.servlet.DispatcherType;
import net.jrouter.ActionFactory;
import net.jrouter.http.servlet.ObjectHandlerActionFactory;
import net.jrouter.http.servlet.filter.SpringBeanJRouterFilter;
import net.jrouter.spring.SpringObjectFactory;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
...
    @Bean
    ActionFactory<String> actionFactory(ApplicationContext applicationContext) {
        ObjectHandlerActionFactory.Properties properties = new ObjectHandlerActionFactory.Properties();
        properties.setActionPathCaseSensitive(false);
        properties.setExtension(".");
        properties.setDefaultResultType("fastjson");
        properties.setDefaultInterceptorStack(net.jrouter.home.interceptor.DefaultInterceptorStack.DEFAULT);
        properties.setObjectFactory(new SpringObjectFactory(applicationContext));
        ObjectHandlerActionFactory actionFactory = new ObjectHandlerActionFactory(properties);

        actionFactory.addInterceptors(net.jrouter.home.interceptor.TimerInterceptor.class);
        actionFactory.addInterceptors(net.jrouter.home.interceptor.ExceptionInterceptor.class);

        actionFactory.addInterceptorStacks(net.jrouter.home.interceptor.DefaultInterceptorStack.class);

        actionFactory.addResultTypes(net.jrouter.http.servlet.result.ServletResult.class);
        actionFactory.addResultTypes(...);

        actionFactory.addActions(net.jrouter.home.action.HomeAction.class);
        ...
        return actionFactory;
    }

    @Bean
    public FilterRegistrationBean actionFactoryFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        SpringBeanJRouterFilter actionFactoryFilter = new SpringBeanJRouterFilter();
        actionFactoryFilter.setEncoding("UTF-8");
        actionFactoryFilter.setFactoryName("jrouter_factory");
        actionFactoryFilter.setLogNotFoundException(false);
        actionFactoryFilter.setTrimRequestParameter(true);
        actionFactoryFilter.setUseThreadLocal(true);
        registration.addUrlPatterns("*.jj");
        registration.setFilter(actionFactoryFilter);
        registration.setDispatcherTypes(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE);
        return registration;
    }

Web Filter配置:

Sample web.xml of project jrouter-home

<filter>
    <filter-name>JRouter-Filter</filter-name>
    <filter-class>net.jrouter.http.servlet.filter.SpringBeanJRouterFilter</filter-class>
    <init-param>
        <description>ActionFactory's bean name</description>
        <param-name>beanName</param-name>
        <param-value>servletActionFactory</param-value>
    </init-param>
    <init-param>
        <description>Character encoding (optional)</description>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <description>Trim HttpServletRequest#getParameter(String) (optional) (default:false)</description>
        <param-name>trimRequestParameter</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <description>ActionFactory's name in ServletContext (optional)</description>
        <param-name>factoryName</param-name>
        <param-value>servletActionFactory</param-value>
    </init-param>
    <init-param>
        <description>log NotFoundException (default:true)</description>
        <param-name>logNotFoundException</param-name>
        <param-value>false</param-value>
    </init-param>
</filter>

Springframework Integration:

Sample spring.xml

<!-- JRouter ActionFactory -->
<bean id="servletActionFactory" class="net.jrouter.http.servlet.spring.ObjectHandlerActionFactoryBean">
    <!-- deprecated since 1.7.5 <property name="defaultObjectResultType" value="freemarker" />-->
    <property name="actionFactoryProperties">
        <util:properties location="classpath:jrouterActionFactory.properties" />
    </property>
    <property name="interceptors">
        <list>
            <value>net.jrouter.home.interceptor.TimerInterceptor</value>
            <value>net.jrouter.home.interceptor.ExceptionInterceptor</value>
        </list>
    </property>
    <property name="interceptorStacks">
        <list>
            <value>net.jrouter.home.interceptor.DefaultInterceptorStack</value>
        </list>
    </property>
    <property name="resultTypes">
        <list>
            <value>net.jrouter.http.servlet.result.ServletResult</value>
            <ref bean="freemarkerResult" />
        </list>
    </property>
    <!-- scan classes properties -->
    <property name="componentClassScanProperties">
        <list>
            <value>
                package = net.jrouter
                includeExpression = net.jrouter.home.**.*Action
            </value>
        </list>
    </property>
</bean>

jrouter-servlet's People

Contributors

tripluo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.