全注解以及静态资源的处理

全注解的使用方式

application.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:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd

">

<context:component-scan base-package="springmvc"></context:component-scan>

<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/javaeehttp://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"

version="4.0">

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:application.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>

</web-app>

quaninnotation类

package springmvc;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@Controller

public class quaninnotation {

@RequestMapping("/math")

public ModelAndView method(HttpServletRequest req, HttpServletResponse resp){

ModelAndView mvc=new ModelAndView();

mvc.addObject("msg","你好吗?世界");

mvc.setViewName("/hello.jsp");

return mvc;

}

}

hello.jsp

<%--

Created by IntelliJ IDEA.

User: Lenovo

Date: 2018/11/17

Time: 14:57

To change this template use File | Settings | File Templates.

--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

${msg}

</head>

<body>

${msg}

</body>

</html>

结果如下

此时仍然存在以下问题,无法阅读静态资源,例如.html的文件,新建一个hello.html的文件,发现其无法被找到。(这就涉及到静态资源的处理

hello.html

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Title</title>

</head>

<body>

fsdfsdkfskdf

</body>

</html>

原因:

tomcat中也有很多内置的servlet,比如说数据的回显,是怎么样将我们封装的java对象放在我们的jsp页面进行回显呢?

  由tomcat里面的servlet做的

  同理,我们需要访问一个静态资源,tomcat也有对应的servlet为我们处理,恰好该servlet的映射路径也为/

  在tomcat中的web.xml是先加载的,项目的web.xml是后加载的

  如果配置了相同的路径,后面的会覆盖前面的.

  也就是说,springMVC中的DispatcherServlet的映射路径覆盖了tomcat默认对静态资源的处理的路径

  如果配置为/,那么Dispatcherservlet是不是需要对静态资源进行支持?

解决方案:

需要Spring添加支持静态资源处理的配置:

<!-- 要支持对静态资源的处理 -->

<mvc:default-servlet-handler/>

<?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:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="

http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd

">

<context:component-scan base-package="springmvc"></context:component-scan>

<mvc:annotation-driven></mvc:annotation-driven>

<mvc:default-servlet-handler></mvc:default-servlet-handler>

</beans>

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/84189831