美文网首页
Spring MVC基础

Spring MVC基础

作者: stutterr | 来源:发表于2017-08-01 21:57 被阅读31次

web.xml配置

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <description>mvc.xml</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:book-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

mvcServlet.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-3.0.xsd   
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       http://www.springframework.org/schema/context <a href="http://www.springframework.org/schema/context/spring-context-3.0.xsd">http://www.springframework.org/schema/context/spring-context-3.0.xsd</a>">

 <mvc:annotation-driven />
    <mvc:resources mapping="/static/**" location="/static/"/> <!-- 静态资源排除 -->

    <!-- 启用spring mvc 注解 -->
    <context:annotation-config />

    <!-- 设置使用注解的类所在的jar包 -->
    <context:component-scan base-package="com.**.controller.**"></context:component-scan>

    <!-- 对转向页面的路径解析。prefix:前缀, suffix:后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

基本controller

package com.gavin.exam.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//完整url  /page/test/dashboard
@Controller
@RequestMapping("/page/test")
public class TestController {

    @RequestMapping(value = "/dashboard", method = RequestMethod.GET)
    public ModelAndView viewCourseDashboard(HttpServletRequest request) {
        System.out.println("hi");
        return null;
    }
}

请求参数的接收

  • @PathVariable
  • @RequestParam
    (@RequestParam(value = "go", defaultValue = "") String go)
  • @RequestHeader
  • @RequestBody
  • 使用对象直接接收
  • 文件上床

相关文章

网友评论

      本文标题:Spring MVC基础

      本文链接:https://www.haomeiwen.com/subject/jiwjlxtx.html