zoukankan      html  css  js  c++  java
  • IDEA之maven(springmvc)项目

    1、在idea下创建maven项目(参考IDEA之web项目(maven项目)创建

    2、项目结构

    3、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 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
      <display-name>Archetype Created Web Application</display-name>
    
      <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 -->
      <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!-- 字符集过滤器 -->
      <filter>
      <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

    4、dispatcher-servlet.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"
           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/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd"
           default-lazy-init="true">
    
        <!-- 通过mvc:resources设置静态资源,这样servlet就会处理这些静态资源,而不通过控制器 -->
        <!-- 设置不过滤内容,比如:css,jquery,img 等资源文件 -->
        <mvc:resources location="/*.html" mapping="/**.html" />
        <mvc:resources location="/css/*" mapping="/css/**" />
        <mvc:resources location="/js/*" mapping="/js/**" />
        <mvc:resources location="/images/*" mapping="/images/**" />
    
        <!-- 添加注解驱动 -->
        <mvc:annotation-driven />
        <!-- 默认扫描的包路径 -->
        <context:component-scan base-package="com.*" />
    
        <!-- mvc:view-controller可以在不需要Controller处理request的情况,转向到设置的View -->
        <!-- 像下面这样设置,如果请求为/,则不通过controller,而直接解析为/index.jsp -->
        <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    </beans>

    5、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>test.com</groupId>
      <artifactId>test-com</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>test-com Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <!-- spring begin -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${spring.version}</version>
        </dependency>
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <!-- spring end -->
      </dependencies>
      <build>
        <finalName>test-com</finalName>
      </build>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>3.1.2.RELEASE</spring.version>
      </properties>
    </project>

    6、TestController.java

    package com.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * Created by sunjf on 2016/6/26.
     */
    @Controller
    public class TestController {
    
        @RequestMapping(value = "test1")
        public String test1(){
            System.out.println("sunjianfei");
            return "success";
        }
    
    }

    7、index.jsp

    <html>
    <body>
    <h2>Hello World!</h2>
    <form action="test1" method="post">
        <input type="submit" name="submit">
    </form>
    
    </body>
    </html>

    8、success.jsp

    <html>
    <body>
    <h2>Hello Success!</h2>
    </body>
    </html>
  • 相关阅读:
    编码上的小改进
    自定义HttpFilter模块完善
    Log4Net日志分类和自动维护
    也来写写基于单表的Orm(使用Dapper)
    要知道的DbProviderFactory
    仿Orm 自动生成分页SQL
    【问题帖】压缩图片大小至指定Kb以下
    [leetcode]Find Minimum in Rotated Sorted Array
    [leetcode]Maximum Product Subarray
    join
  • 原文地址:https://www.cnblogs.com/sunjf/p/springmvc_maven_idea.html
Copyright © 2011-2022 走看看