zoukankan      html  css  js  c++  java
  • 快速创建一个 spring mvc 示例

    1. 创建一个 servlet 项目 参考 http://www.cnblogs.com/zno2/p/5908589.html

    2. 引入 spring mvc 依赖 

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>4.3.3.RELEASE</version>
            </dependency>

    注:在sts中创建spring bean configuration file 时就可以载入Namespaces (视情况需 update project)

    如下图

    3. 修改 web.xml

    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
        <display-name>Archetype Created Web Application</display-name>
    
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/root-context.xml</param-value>
        </context-param>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value></param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    </web-app>

    ps: url-pattern 一定要是 '/' 不能是 '/*'

    4. 创建 WEB-INF/root-context.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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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.3.xsd">
    
    
        <mvc:annotation-driven />
    
        <context:component-scan base-package="zmvc" />
    
    
        <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>

    ps: base-package 是用来扫描指定包下的 controller 

    ps: eclipse 在 类上加 @Controller 后,Alt+/ 不再提示 ,修改成权限定名即可 @org.springframework.stereotype.Controller

    5. 编写 controller 

    package zmvc;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class Test {
    
        
        @RequestMapping(path = "/test")
        public String test() {
    
            return "testresult";
        }
    }

    6. 添加 view

    创建 WEB-INF/jsp/testresult.jsp

    <html>
    <body>
    <h2>This is a test result!</h2>
    </body>
    </html>

    7. 开启 log

    ①添加依赖

            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.14</version>
            </dependency>

    ②添加 log4j.properties

    # Root logger option
    log4j.rootLogger=TRACE, file, stdout
    
    # Direct log messages to a log file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=E:\logging.log
    log4j.appender.file.MaxFileSize=10MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    # Direct log messages to stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

    8.补充依赖

    经测试 需要用到 jstl 添加如下依赖

            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>

    9. 最终目录结构 

  • 相关阅读:
    SVN服务器搭建和使用(一)
    SVN服务器搭建和使用(一)
    lua loadstring与loadfile
    lua loadstring与loadfile
    lua_getstack
    lua_getstack
    让程序在崩溃时体面的退出之Dump文件
    bzoj1054
    poj3678
    poj2749
  • 原文地址:https://www.cnblogs.com/zno2/p/5913980.html
Copyright © 2011-2022 走看看