spring-mvc 学习笔记

一、web.xml配置

<!-- 注意:第一句很重要,修改动态web项目为3.0版本 -->
<web-app
    version="3.0"
    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">
    <servlet>
        <!-- 两个example的名称对应 -->
        <servlet-name>example</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!-- 配置spring-mvc的配置文件路径 -->
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <!-- 配置url中什么样的情况会进入映射器处理 -->
        <servlet-name>example</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
View Code

二、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: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.xsd
      http://www.springframework.org/schema/util
      http://www.springframework.org/schema/util/spring-util.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">

    <!-- 自动扫描controller包中的控制器 -->
    <context:component-scan base-package="cn.duanjt" />

    <!-- 使用默认的注解映射 -->
    <mvc:annotation-driven />

    <!-- 配置映射器处理类 -->    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property
            name="prefix"
            value="/WEB-INF/jsp/" />
        <property
            name="suffix"
            value=".jsp" />
    </bean>

</beans>
View Code

三、Java示例代码 

package cn.duanjt;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.duanjt.pojo.Student;

@Controller
@RequestMapping("/mvc")
public class UserController {
    /**
     * 返回视图
     * @return
     */
    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
    
    /**
     * 接收参数,返回视图,将根据xml配置返回路径为:WEB-INF/jsp/hello.jsp
     * @param name
     * @param age
     * @return
     */
    @RequestMapping("/SayHello")
    public String SayHello(String name,int age){
        System.out.println("姓名:"+name+",年龄:"+age);
        return "hello";
    }
    
    /**
     * 直接返回json数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/index")
    public Student index(){
        Student s=new Student();
        s.setName("张三");
        s.setAddr("重庆");
        return s;
    }

}
View Code

四、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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>cn.duanjt</groupId>
    <artifactId>spring-mvc_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.9</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>spring-mvc_test</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
View Code

五、对于POST请求可能出现中文乱码问题,在web.xml中增加过滤器,如下: 

<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
<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>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
View Code

 

注意:

web.xml的位置是放到 WEB-INF下面。

 

感谢:http://www.cnblogs.com/sunniest/p/4555801.html

posted @ 2017-07-18 12:49  段江涛IT  阅读(268)  评论(0编辑  收藏  举报
页脚HTML代码