Springmvc学习-Springmvc02-静态资源放行

Springmvc02-静态资源放行问题

1. web.xml配置

<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"
	id="WebApp_ID" version="3.0">
	
	

  
  	
	
  <servlet>
  	<servlet-name>springmvcservlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  	
  	<init-param>
  		<param-name>contextConfigLocationparam-name>
  		<param-value>classpath:springmvc.xmlparam-value>
  	init-param>
  	<load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
  	<servlet-name>springmvcservlet-name>
  	<url-pattern>/url-pattern>  
  servlet-mapping>
web-app>

其中/和/*的区别

  1. < url-pattern > / 不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。
  2. < url-pattern > /* 会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。

总之,关于web.xml的url映射的小知识:

  1. < url-pattern>/ 会匹配到/login这样的路径型url,不会匹配到模式为*.jsp这样的后缀型url
  2. < url-pattern>/* 会匹配所有url:路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)
1. springmvc.xml配置

<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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

  	
  	<context:component-scan base-package="com.caorui.handlers">context:component-scan>
  	
  	<mvc:annotation-driven/>
  	
  	//和上面的注册组件扫描器,注册注解驱动有异曲同工之妙
  	
  	
    
    
    <mvc:resources location="/images/" mapping="/images/**">mvc:resources>
beans>
3. Controller类
package com.caorui.handlers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

//后端控制器
@Controller //该注解表示将当前类给spring容器管理
@Scope("prototype")
@RequestMapping("/springmvc") //该注解起了限定范围的作用
public class MyController {

	@RequestMapping("/hadleRequest") //用注解来约定俗成路径
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView();
		System.out.println("进入到后端控制器方法");
		mv.addObject("msg", "hello world");
		mv.setViewName("/js/welcome.jsp");
		return mv;
	}
}

你可能感兴趣的:(springmvc)