SpringMVC的配置具體過(guò)程
1.web.xml配置過(guò)程
1.創(chuàng)建maven項(xiàng)目(maven具體搭建過(guò)程以后會(huì)說(shuō))
2.在java中不管你想要引入新框架還是什么新功能第一步就要做的是引入Jar包
3.spring的核心配置文件是WEB-INF下的web.xml
4.在web.xml中加入如下內(nèi)容(具體意義下面解釋)
<servlet>
<servlet-name>dername</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dernaem</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
5.每一個(gè)DispatchServlet都對(duì)應(yīng)了一個(gè)xml配置文件,這個(gè)xml命名規(guī)則dername-servlet.xml這個(gè)文件與web.xml在同級(jí)目錄下
2.dername-servlet.xml配置
1.在web.xml的同級(jí)目錄下(也就是WEB-INF下)新建dername-servlet.xml文件
2.倒入Spring命名空間即下列代碼
<?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:util="http://www.springframework.org/schema/util"
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-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
">
</beans>
接下來(lái)說(shuō)的是在<beans></benans>中所加入的暇咆。
1.開(kāi)啟注解
<mvc:annotation-driver></mvc:annotation-driver>
2.掃描類和方法所在的包
<context:component-scan base-package="Controller所在的包"></context:component-scan>
3.配置視圖解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
各個(gè)點(diǎn)的解釋:
在web.xml中加入的內(nèi)容是配置的攔截器物蝙,為什么要配置攔截器呢测垛?我們都知道在瀏覽器中輸入的網(wǎng)址也就是常說(shuō)url,那么url是怎么找到我們所寫的頁(yè)面呢?可也這樣理解我們所寫的url既然不知道怎么去找,那不如把書寫的url攔截下來(lái)加載到庫(kù)里以map形勢(shì)去匹配淌山,匹配到啦就是所對(duì)應(yīng)的頁(yè)面拯勉。
在說(shuō)說(shuō)dername-servlet.xml中所對(duì)應(yīng)的配置竟趾。
在dername-servlet.xml中的倒入Spring命名空間是讓Spring的標(biāo)簽生效
如開(kāi)啟注解標(biāo)簽:
<mvc:annotation-driver></mvc:annotation-driver>
這些標(biāo)簽生效啦,自然也就能用這些標(biāo)簽陪SpringMVC.
掃描類和方法所在的包:
在Controller中在servlet中是一個(gè)類控制一個(gè)頁(yè)面宫峦,而在Spring中可以做到
一個(gè)方法控制一個(gè)頁(yè)面岔帽,而RequestMapping就是所指示的葉敏的key
而return就是指的是所返回的頁(yè)面。你可以理解為把這些返回的頁(yè)面加載到一個(gè)又一個(gè)盒子里导绷。你拿這這些鑰匙去匹配犀勒。
那么在說(shuō)說(shuō)開(kāi)啟注解標(biāo)簽是什么意思。一般在控制層中代碼都是這樣的
/**
*
*/
package com.neusoft.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.neusoft.bean.Student;
import com.neusoft.bean.Teacher;
@Controller
public class StudentController {
@RequestMapping("/abcdefg")
public String abc(){
return "abc";
}
@RequestMapping("/abc")
public String abcd(Teacher teacher,Student student){
return "abc";
}
}
這里的@Controller 與@RequestMapping就是指的注解
開(kāi)啟注解自然就是讓這些標(biāo)簽生效妥曲。
視圖解析器:在上面代碼中所看到的return返回的字符串就是一個(gè)一個(gè)jsp頁(yè)面的名字而視圖解析器的作用就是把這些字符串匹配到相應(yīng)的jsp頁(yè)面贾费。
具體原理請(qǐng)自行百度。
因?yàn)?lt;servlet>的原因把所有Url的地址進(jìn)行攔截檐盟,所以一些資源文件也會(huì)被攔截褂萧。只需在deranme-servlet.xml 中加入
<mvc:resources location="WEB-INF為根目錄的資源文件路徑" mapping="img/***"></mvc:resources>
或者在攔截器中
<url-pattern>/</url-pattern>
的“ / ”,寫成*.do " . "后邊隨意禁止使用.jsp。
攔截.jsp會(huì)出現(xiàn)循環(huán)攔截問(wèn)題遵堵。
更改<servlet-name>-serlet.xml名字的兩種方式箱玷。
1.在<serlet></serlet>節(jié)點(diǎn)下加入以下代碼:
<servlet>
<servlet-name>dername</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
<init-param>
<pram-name>namespace</param-name>
<param-value>任意名字<param-value>
</init-param>
</servlet>
<param-value>的名字就是視圖解析器的名字怨规。
一般情況程序下在第一次啟動(dòng)的情況下時(shí)候加載servlet的類那么想在服務(wù)器啟動(dòng)的時(shí)候加載類。需要在web.xml<serlet></serlet>的節(jié)點(diǎn)下加入锡足。
以下代碼
<load-on-startap>1</load-onstartap>
注:必須加到<init-param></init-param>節(jié)點(diǎn)上方要不然會(huì)報(bào)錯(cuò)波丰。
注:serlet在調(diào)用init的時(shí)候加載類。