关于使用Gradle整合Springmvc构建JavaWeb项目的那点事

关于项目创建

如果你不知道如何创建一个通过Gradle构建的基本web项目,那么本文无法帮助你,你先去把项目创建好,此项目仅作为练习用,我将再次以插件形式集成shiro,mybatis,oauth等等。

    1. 如果你看到这里,我就假设你对gradke已经有了初步的了解,已经创建好了一个基本的web项目了,如下图你已经有了一个基本的项目结构,我使用的是Intellij Idea15.

      项目结构图:

    1. build.gradle
group 'com.51tutechan'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'

sourceCompatibility = 1.7

repositories {
    mavenCentral()
}

dependencies {
    compile(
            'commons-logging:commons-logging:1.2',
            'org.springframework:spring-aop:4.2.4.RELEASE',
            'org.springframework:spring-beans:4.2.4.RELEASE',
            'org.springframework:spring-context:4.2.4.RELEASE',
            'org.springframework:spring-core:4.2.4.RELEASE',
            'org.springframework:spring-expression:4.2.4.RELEASE',
            'org.springframework:spring-web:4.2.4.RELEASE',
            'org.springframework:spring-webmvc:4.2.4.RELEASE'
    )
    runtime("jstl:jstl:1.2")
}

task copyJars(type: Copy) {
    from configurations.runtime
    into 'lib' // 目标位置
}
  • 3.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"
         version="3.0">

    <!--START 设置字符编码过滤器-->
    <filter>
        <description>字符集过滤器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集编码</description>
            <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>
    <!--END 设置字符编码过滤器-->

    <!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>/</welcome-file>
    </welcome-file-list>
    <session-config>
        <session-timeout>20</session-timeout>
    </session-config>
</web-app>
  • 4.spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="com.tutechan"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />

    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/view/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
  • 5.Controller
package com.tutechan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * Created by benny on 2016/1/16.
 */
@Controller
@RequestMapping("/test")
public class HelloController {

    @RequestMapping(value = "/hello/{0}", method = RequestMethod.GET)
    public String helloWorld(@PathVariable("id") Integer id) {
        System.out.println("get" + id);
        return "hello";
    }
}
  • 6.SpringMVC运行流程图

借用sunnier的图

最近工作比较忙,陆续添加

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值