• Spring MVC 总结一(spring mvc 基本例子)


    一、目录结构

      

    二、 具体步骤

      2.1 applicationContext.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"
    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-3.0.xsd">

    <context:annotation-config />
    </beans>

      2.2 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-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">

    <mvc:annotation-driven/>
    <!-- 自动扫描包 -->
    <context:component-scan base-package="pr.cgl.controller" />

    <!-- mvc返回页面的配置 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <!-- 模板路径为WEB-INF/pages/ -->
    <property name="prefix" value="/WEB-INF/"/>
    <!-- 视图模板后缀为.JSP -->
    <property name="suffix" value=".jsp"/>
    </bean>

    </beans>

      2.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_2_5.xsd" version="2.5">


    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>SpringWebT1.root</param-value>
    </context-param>
    <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>

    <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

    <!-- 加载springmvc -->
    <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:mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 以.htm结尾的都被mvc拦截 -->
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/SpringWebT1/*</url-pattern>
    </servlet-mapping>

    </web-app>

      2.4 添加日志

        2.4.1 配置log4j.properties

    ### Log4j Properties ###

    # Options are <DEBUG, INFO, WARN, ERROR or FATAL>

    # Use two appenders, one to log to console, another to log to a file
    log4j.rootLogger=ALL,debug,info,error,console
    #log4j.rootLogger=debug,error,console
    #log4j.rootLogger=error,console
    log4j.threshold=ALL

    log4j.category.com.jlt.mis=debug,AdapterFile
    log4j.appender.AdapterFile=org.apache.log4j.RollingFileAppender
    log4j.appender.AdapterFile.MaxFileSize=46MB
    log4j.appender.AdapterFile.MaxBackupIndex=3
    log4j.appender.AdapterFile.File=${SpringWebT1.root}/logs/JLTShop.log
    log4j.appender.AdapterFile.append=true
    log4j.appender.AdapterFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.AdapterFile.layout.ConversionPattern=%t %d{DATE} %c{3} %p - %m%n

    # WRITE LOG TO A CONSOLE
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.Target=System.out
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    #log4j.appender.console.layout.ConversionPattern=[SpringWebT1] %d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%n
    log4j.appender.console.layout.ConversionPattern=%d{MM HH:mm:ss,SSS} - %M (%F:%L) >> %p: %m%n
    log4j.appender.console.threshold=DEBUG

    # WRITE LOG TO A FILE, ROLL THE FILE EVERY DAY
    log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.info.layout=org.apache.log4j.PatternLayout
    log4j.appender.info.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS}-%C{1}.%M (%F:%L) >> %p: %m%n
    log4j.appender.info.DatePattern='.'yyyy-MM-dd
    log4j.appender.info.threshold=INFO
    log4j.appender.info.append=true
    log4j.appender.info.File=${SpringWebT1.root}/logs/infoLogFile.log

    log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.debug.layout=org.apache.log4j.PatternLayout
    log4j.appender.debug.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%n
    log4j.appender.debug.DatePattern='.'yyyy-MM-dd
    log4j.appender.debug.threshold=DEBUG
    log4j.appender.debug.append=true
    log4j.appender.debug.File=${SpringWebT1.root}/logs/debugLogFile.log

    log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.error.layout=org.apache.log4j.PatternLayout
    log4j.appender.error.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss,SSS} - %C{1}.%M (%F:%L) >> %p: %m%n
    log4j.appender.error.DatePattern='.'yyyy-MM-dd
    log4j.appender.error.threshold=ERROR
    log4j.appender.error.append=true
    log4j.appender.error.File=${SpringWebT1.root}/logs/errorLogFile.log
    2.4.2 配置web.xml
      添加以下代码段
        <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.properties</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

      2.5 controller

    package pr.cgl.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;

    /**
    * Created by CGL on 2015/9/10.
    */
    @Controller
    @RequestMapping("/main")
    public class TestController {

    @RequestMapping("/index.html")
    public String index(Model m){
    m.addAttribute("name", "clg");
    return "index";
    }
    }
    2.6 测试
      地址栏输入http://localhost:8080/SpringWebT1/main/index.html,得到Hello, clg

    
    
  • 相关阅读:
    ant design同时获取下拉框值与文本(选择框)
    nodejs express vue实现文件上传
    Vue路由传参的两种方式
    Vue项目中的接口调用
    表单上传文件 nodejs express Vue (优化)
    httprunner安装可能遇到的问题及解决办法之hrun V查询失败mac
    Eclipse调整编码方式总结
    css水平垂直居中的方法
    手机web前端调试页面的几种方式
    寻找共线特征和寻找缺失值
  • 原文地址:https://www.cnblogs.com/cglWorkBook/p/4797451.html
Copyright © 2020-2023  润新知