002.SSM之Spring MVC

项目构成:

  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>
测试
demo
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值