莫大人

springMvc 简单搭建

1.pom.xml 依赖引入

2.配置web.xml

3.配置 springMvc.xml

4.配置 logback.xml

5.编写 controller 测试

 

1.pom.xml 依赖引入

<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.yun</groupId>
    <artifactId>springMvc</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>kong Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <properties>
        <spring.version>4.2.5.RELEASE</spring.version>
        <druid.version>1.0.5</druid.version>
        <mysql.version>5.1.40</mysql.version>
        <slf4j.version>1.7.7</slf4j.version>
        <logback.version>1.1.2</logback.version>

<servlet.version>4.0.0</servlet.version>
<fastjson.version>1.2.37</fastjson.version>

        <freemarker.version>2.3.23</freemarker.version>
        <mybatis.version>3.3.1</mybatis.version>
        <mybatis-spring.version>1.2.5</mybatis-spring.version>
        <mysql.connector.version>5.1.30</mysql.connector.version>
    </properties>

    <dependencies>
        <!-- springmvc start -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- springmvc end -->
        
        
        <!-- logback -->
        <dependency>
        <!--主要介绍的是这个jar包,这个包是负责logback随着项目启动的jar包-->
            <groupId>org.logback-extensions</groupId>
            <artifactId>logback-ext-spring</artifactId>
            <version>0.1.4</version>
        </dependency>
        <!-- slf4j start -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-access</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
        </dependency>
        <!-- slf4j end -->

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>

        
    </dependencies>
    <build>
        <finalName>kong</finalName>
    </build>
</project>

 

2.配置web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <!-- 应用路径 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>springMvc</param-value>
    </context-param>
    
    <!--Spring上下文 配置 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!-- Spring 监听器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


  
    <!-- 字符集 过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-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:springMvc.xml
            </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
   <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

3.配置 springMvc.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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.yun.yun.base.web">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <mvc:default-servlet-handler />

    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <bean
                class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes" value="text/html;charset=UTF-8" />
                <property name="features">
                    <array>
                        <value>WriteMapNullValue</value>
                        <value>WriteNullStringAsEmpty</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <mvc:resources mapping="/js/**" location="/js/" />
</beans>

 

 

4.配置 logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- 文件输出格式 -->
    <property name="PATTERN"
        value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{50} - %msg%n" />
    <!-- test文件路径 -->
    <property name="LOG_HOME" value="C:/Users/Administrator/Desktop/logs" />

    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>${PATTERN}</pattern>
        </encoder>
    </appender>


    <!-- 按照每天生成日志文件 -->
    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!--日志文件输出的文件名 -->
            <FileNamePattern>${LOG_HOME}/yun.%d{yyyyMMdd}.log
            </FileNamePattern>
            <!--日志文件保留天数 -->
            <!-- <MaxHistory>30</MaxHistory> -->
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符 -->
            <pattern>${PATTERN}</pattern>
        </encoder>
        <!--日志文件最大的大小 -->
        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>500MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

     <logger name="org.springframework.web" level="DEBUG" additivity="false">  
        <appender-ref ref="STDOUT" />
    </logger>  
    
     <logger name="druid.sql" level="DEBUG" additivity="false">  
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </logger>  
     <logger name="com.github.pagehelper" level="DEBUG" additivity="false">  
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </logger>  
     <logger name="com.yun" level="DEBUG" additivity="false">  
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </logger>  
    
    <!-- 日志输出级别 -->
    <root level="DEBUG">
        <appender-ref ref="FILE" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

 

5.编写 controller 测试

package com.yun.base.web;

import javax.servlet.http.HttpServletRequest;

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

import com.yun.base.bo.Person;


@Controller
@RequestMapping("/mvc")
public class HelloController {

    @RequestMapping("/hello")
    public String hello() {
        return "index";
    }
    
    @RequestMapping("/personRequest")
    public String toPerson(HttpServletRequest request) {
        String age = request.getParameter("age");
        System.out.println("--"+age);
        return "index";
    }

    @RequestMapping("/person/{name}/{age}")
    public String toPerson(@PathVariable String name ,@PathVariable int age) {
        System.out.println(name+"--"+age);
        return "index";
    }
    @RequestMapping("/person")
    public String requestParam(@RequestParam("name") String name,@RequestParam("age") int age) {
        System.out.println(name+"--"+age);
        return "index";
    }
    @RequestMapping(value="/person",method=RequestMethod.PUT)
    public String addPerson(@RequestBody Person person) {
        System.out.println(person.getAge()+":"+person.getName());
        return "index";
    }
    
    @RequestMapping(value="/showPerson",method=RequestMethod.POST)
    public @ResponseBody Person showPerson(@RequestBody Person person) {
        System.out.println(person.getAge()+":"+person.getName());
        return person;
    }
}

测试地址

http://localhost:8015/springMvc/

ok 完成

 

posted on 2017-07-24 17:19  莫大人  阅读(248)  评论(2编辑  收藏  举报

导航