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路徑映射

窄化請求映射

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