SpringMVC一.. SpringMVC重要組件介紹 1. DispacherServlet:前端控制器,接收所有請求,(如果配置/不包含jsp) 2. HandlerMapping:解析請求格式,判斷希望要執行哪個方法 3. HandlerAdapter:負責調用具體的方法 4. ViewRe ...
SpringMVC
一.. SpringMVC重要組件介紹
1. DispacherServlet:前端控制器,接收所有請求,(如果配置/不包含jsp)
2. HandlerMapping:解析請求格式,判斷希望要執行哪個方法
3. HandlerAdapter:負責調用具體的方法
4. ViewResovler:視圖解析器,解析結果,準備跳轉的具體的視圖
spring的jar包官方下載地址:
https://repo.spring.io/libs-release-local/org/springframework/spring/
二. 環境搭建
1.導入jar包
2.在web.xml中配置前端控制器,若不配置<init-param>,會去/web-inf/<servlet-name>servlet.xml找
<servlet>
<servlet-name>qq</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>qq</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
3.src下麵新建springmvc.xml
3.1.引入命名空間
<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.bj.controller"></context:component-scan>
<!--註解驅動-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--放行靜態資源兩個*號表示文件夾下所有文件-->
<mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<mvc:resources mapping="/css/**" location="/css/"></mvc:resources>
<mvc:resources mapping="/imgs/**" location="/imgs/"></mvc:resources>
</beans>
3.2.編寫控制器類
1 @Controller
2 public class Democontroller {
3 @RequestMapping("demo")
4 public String demo(){
5 return "main.jsp";
6 }
7
8 }