Java康复性训练——SpringMVC

2016/7/24 posted in  Java  

SpringMVC是一套功能强大,性能强悍且使用方便的优秀MVC框架

SpringMVC-HelloWorld

  1. 导入相关jar包
  2. 配置web.xml文件,设置全局拦截器。拦截所有请求,转发给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:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
  3. 添加SpringMVC配置文件spring-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:p="http://www.springframework.org/schema/p"
        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.xsd">
    
        <!-- 使用注解的包,包括子集 -->
        <context:component-scan base-package="site.zhanjingbo"/>
    
        <!-- 视图解析器 -->
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    
  4. 添加控制器HelloContorller

    package site.zhanjingbo.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class HelloController {
    
        @RequestMapping("/hello")
        public String hello(Model model){
            model.addAttribute("message", "HelloWorld!");
            return "hello";
        }
    }
    
  5. 在/WEB-INF/jsp/下建立显示返回页面hello.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    ${message }
    </body>
    </html>
    
  6. 完成。启动Tomcat,访问http://localhost:8080/SpringMVC/hello.do 查看

SpringMVC控制器

  • @RequestMapping:用于指定方法的响应URL或模块URL
  • @RequestParam:绑定提交参数与控制器方法参数的注解。@RequestParam(value="",required=true/false)
  • ModelAndView:作为控制器的返回值类型,返回模型与视图
  • SpringMVC对象属性自动封装:SpringMVC可以根据表单传入的数据自动封装控制器参数对象
  • SpringMVC POST请求中文乱码:在web.xml中添加过滤器

    <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
  • Controller内部转发与重定向

    • 转发:forward:
    • 重定向:redirect:
  • SpringMVC对Servlet API的支持

    • 只需要在控制器方法的参数列表中添加HttpServletRequest、HttpServletResponse参数即可自动注入实例对象。
  • SpringMVC对json的支持:这个有很多实例,建议直接上搜索引擎。