1. 程式人生 > >全註解以及靜態資源的處理

全註解以及靜態資源的處理

 

全註解的使用方式

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/context

http://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>