002.SSM之Spring MVC

来源:互联网 发布:三菱fx5u编程手册 编辑:程序博客网 时间:2024/05/17 18:40

项目构成:

  1. 配置文件
  2. Controller
  3. JSP文件
  4. 测试
  5. demo

配置文件

  • 统一编码格式
  • 加载Spring MVC配置文件

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_3_0.xsd"   version="3.0">        <display-name>springmvctest</display-name>        <!-- 统一编码 -->        <filter>            <filter-name>charsetEncoding</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>charsetEncoding</filter-name>            <url-pattern>/*</url-pattern>        </filter-mapping>        <!-- 前端控制器 -->        <servlet>            <servlet-name>springmvc</servlet-name>            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>            <!-- 配置文件在src下时 -->            <!--             <init-param>                <param-name>contextConfigLocation</param-name>                <param-value>classpath:springmvc-servlet.xml</param-value>            </init-param> -->            <!-- 加载/WEB-INF/[servlet-name]-servlet.xml -->            <load-on-startup>1</load-on-startup>        </servlet>        <servlet-mapping>            <servlet-name>springmvc</servlet-name>            <url-pattern>/</url-pattern>        </servlet-mapping>    </web-app>
  • 开启注解功能
  • 自动扫描Controller
  • 配置视图
  • 其它各种配置(与本文无关)

springmvc-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:p="http://www.springframework.org/schema/p"         xmlns:context="http://www.springframework.org/schema/context"         xmlns:mvc="http://www.springframework.org/schema/mvc"         xmlns:task="http://www.springframework.org/schema/task"        xsi:schemaLocation="http://www.springframework.org/schema/beans             http://www.springframework.org/schema/beans/spring-beans-4.2.xsd             http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-4.2.xsd             http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd             http://www.springframework.org/schema/task             http://www.springframework.org/schema/task/spring-task-4.2.xsd">        <!-- 开启注解   -->        <!-- @RequestMapping, @ExceptionHandler,数据绑定 ,@NumberFormat ,       @DateTimeFormat ,@Controller ,@Valid ,@RequestBody ,@ResponseBody等  -->        <mvc:annotation-driven />        <!-- 扫描路径 -->        <context:component-scan base-package="com.wsk.test" >            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>        </context:component-scan>        <!-- 静态资源配置 -->        <mvc:resources location="/assets/" mapping="/assets/**" />        <!-- 视图层配置 -->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name="prefix" value="/WEB-INF/pages/"/>            <property name="suffix" value=".jsp"/>        </bean>        <!-- 配置根视图 -->        <mvc:view-controller path="/" view-name="index"/>        <!-- 文件上传配置 -->        <!--         <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">            默认编码            <property name="defaultEncoding" value="UTF-8"/>            上传文件大小限制为31M,31*1024*1024            <property name="maxUploadSize" value="32505856"/>            内存中的最大值            <property name="maxInMemorySize" value="4096"/>        </bean> -->    </beans>

Controller

  • 视图
  • 接口

web.xml

    package com.wsk.test;    import javax.servlet.http.HttpServletRequest;    import org.springframework.stereotype.Controller;    import org.springframework.ui.Model;    import org.springframework.web.bind.annotation.RequestMapping;    import org.springframework.web.bind.annotation.RequestMethod;    import org.springframework.web.bind.annotation.ResponseBody;    @Controller    @RequestMapping("/hello")    public class HelloController {        @RequestMapping(value="/view",method=RequestMethod.GET)        public String hello(Model model){            model.addAttribute("msg", "hello world");            return "index";        }        @RequestMapping(value="/get",method=RequestMethod.GET)        public @ResponseBody String test(HttpServletRequest request){       return "hello world";        }    }

JSP文件

  • 视图

index.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>hello</title>        </head>        <body>            <h1>test</h1>            ${msg }        </body>    </html>

测试

  • 浏览器访问 http://localhost:8080/SpringMVCTest/hello/view 会显示index.jsp
  • 浏览器访问 http://localhost:8080/SpringMVCTest/hello/get 会显示返回的字符串

demo

  • 百度云 SpringMVCTest.rar
原创粉丝点击