Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置

1.最简单的Spring mvc

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.0modelVersion>
    <groupId>test-mvcgroupId>
    <artifactId>demo-mvcartifactId>
    <packaging>warpackaging>
    <version>0.0.1-SNAPSHOTversion>
    <name>demo-mvc Maven Webappname>
    <url>http://maven.apache.orgurl>
    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>4.3.5.RELEASEversion>
        dependency>
    dependencies>
    <build>
        <finalName>demo-mvcfinalName>
    build>
project>
2 web.xml


<web-app>
    
    <display-name>Archetype Created Web Application demo_mvcdisplay-name>


    <filter>
        <filter-name>encodingfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>

    <filter-mapping>
        <filter-name>encodingfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>demoservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <load-on-startup>1load-on-startup>
    servlet>
    <servlet-mapping>
        <servlet-name>demoservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>
    
web-app>
3 /demo-mvc/src/main/webapp/WEB-INF/applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
  http://www.springframework.org/schema/beans/spring-beans.xsd  
  http://www.springframework.org/schema/aop   
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  http://www.springframework.org/schema/tx   
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/context   
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/util   
  http://www.springframework.org/schema/util/spring-util-4.0.xsd
  ">

beans>
4 demo-mvc/src/main/webapp/WEB-INF/demo-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="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/util 
       http://www.springframework.org/schema/util/spring-util-3.0.xsd 
       http://www.springframework.org/schema/mvc 
       http://www.springframework.org/schema/mvc/spring-mvc.xsd
      ">

             
         <mvc:annotation-driven>mvc:annotation-driven>
         
         <context:component-scan base-package="demo">context:component-scan>
         
         <mvc:resources location="/resources/" mapping="/resources/**"/>

         
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               
               <property name="prefix" value="/WEB-INF/pages">property>
               
               <property name="suffix" value=".jsp">property>
         bean> 
beans>
5 Controller.java
package demo;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 参考示例
 */
@RestController
//@Order(value = 5)
public class ControllerExcample{
    @RequestMapping("/tst")
    public Object get_value(HttpServletRequest req) {
        return "heihei";
    }
}

工程结构如图
Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置_第1张图片

2 spring mvc + logback

1 添加pom.xml依赖
<dependency>
            <groupId>ch.qos.logbackgroupId>
            <artifactId>logback-classicartifactId>
            <version>1.1.7version>
        dependency>
2 在src/main/resource添加logback.xml
<configuration>
    
    
    <property name="log_path" value="demo_logs"/>
    <property name="maxHistory" value="30"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>UTF-8charset>
            <pattern>%d %p (%file:%line\)- %m%npattern>
        encoder>
    appender>
    <appender name="console"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${log_path}/console.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log_path}/console.log.%dfileNamePattern>
            <maxHistory>${maxHistory}maxHistory>  
        rollingPolicy>
        <encoder>
            <pattern>
                %d (%file:%line\)- %m%n
            pattern>
        encoder>
    appender>
    <appender name="baselog"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${log_path}/base.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedR
        ollingPolicy">
            <fileNamePattern>${log_path}/base.log.%dfileNamePattern>
            <maxHistory>${maxHistory}maxHistory>  
        rollingPolicy>
        <encoder>
            <pattern>
                %d (%file:%line\)- %m%n
            pattern>
        encoder>
    appender>
    <appender name="userlog"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>${log_path}/user.logFile>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${log_path}/user.log.%dfileNamePattern>
            <maxHistory>${maxHistory}maxHistory>  
        rollingPolicy>
        <encoder>
            <pattern>
                %d (%file:%line\)- %m%n
            pattern>
        encoder>
    appender>
    <root level="info">
        <appender-ref ref="STDOUT" />
        
    root>
    
    <logger name="demo" level="debug">
        <appender-ref ref="baselog" />
    logger>
    <logger name="USER">
        <appender-ref ref="userlog" />
    logger>
    <jmxConfigurator/>
configuration>
3 Java代码
package demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogCore{
    public static Logger  BASE = LoggerFactory.getLogger(LogCore.class);
    public static Logger  USER = LoggerFactory.getLogger("USER");
}
4 查看日志
1 查看tomcat的日志(与本文无关)

在catalina路径也就是-Dcatalina.base=/Users/bao/data/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0 参数
Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置_第2张图片

2 查看logback的日志

也就是System.getProperty(“user.dir”) 输出的地址
Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置_第3张图片

3 将配置文件移动到/src/main/resource里

1 修改web.xml

displayname 标签后添加

    param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    param>

servlet 的配置添加contextConfigLocation

<servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:demo-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

移动demo-servlet.xml和applicationContext.xml

Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置_第4张图片

4 注意事項,找不到http servlet錯誤”The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build “的解決方式

方式1(不推荐)在环境变量引入外部Tomcat作为库

这样打包的时候不打进去httpServlet,因为我们要将项目部署在Tomcat上,tomcat作为容器已经有这些jar了。
Spring mvc(1) 搭建一个最简单的Spring mvc, 以及最简单的logback的配置_第5张图片

方式2,但是我们用maven 工程在编译的时候需要有这个包,而package的时候忽略。

<dependency>
    <groupId>javax.servletgroupId>
    <artifactId>servlet-apiartifactId>
    <version>2.5version>
    <scope>providedscope>
dependency>

就跟Spring boot使用外部Tomcat的时候添加依赖一样

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-tomcatartifactId>
    <scope>providedscope>
dependency>

你可能感兴趣的:(spring-mvc)