SpringMVC的基本配置2

使用注解配置基本配置

  • 这个是Controller
package com.bamzhy.controller;

import com.bamzhy.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SecondController{

    @RequestMapping("/haha.html")
    public ModelAndView handleRequest() throws Exception {

        //模型和视图
        ModelAndView mv = new ModelAndView();

        User user = new User();
        user.setUsername("苟利国家生死以");
        user.setPassword("岂因祸福避趋之");
        //把对象放进去了
        mv.addObject("user",user);
        //把视图放进去了
        mv.setViewName("/index.jsp");

        return mv;
    }
}

applicationContext.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:context="http://www.springframework.org/schema/context"
       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/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">

        <context:component-scan base-package="com.bamzhy"/>
        <!--注解驱动:替我们自动配置最新版的注解的处理器映射器和处理器适配器-->
        <mvc:annotation-driven></mvc:annotation-driven>
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1"
         metadata-complete="false">
    <!--你要调用的核心控制器的id 和全类名DispatcherServlet-->
    <servlet>
        <servlet-name>springmvcservlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--初始化参数,将applicationContext.xml传给contextConfigLocation-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
    </servlet>

    <!--你要调用的核心控制器的id和能够访问到这个servlet的url-->
    <servlet-mapping>
        <servlet-name>springmvcservlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

</web-app>

RequestMapping注解详解

@RequestMapping("/haha.html")
public ModelAndView handleRequest() throws Exception {
@RequestMapping("/haha")
public ModelAndView handleRequest() throws Exception {
    <servlet-mapping>
        <servlet-name>springmvcservlet</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
  • 这俩效果一样,因为你访问任意带html结尾的url都会被去掉html,然后去RequestMapping里边找匹配的

URL路径映射

窄化请求映射

  • 请求方法限定
  • 其他
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章