Springmvc----註解和非註解開發

一 : 入門程序

1 在web.xml文件中配置咱們的 springmvc容器前端

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
    <!-- 配置咱們的前端控制器   DispatcherServlet -->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--   	 contextConfigLocation配置spingmv加載配置文件(配置處理器,映射器等等)
  		若是不配置contextConfigLocation 他就默認加載WEB-INF/Servlet名稱-servlet.xml(springmvc-servlet.xml)
  	 -->
  	<init-param> 	
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring/springmvc.xml</param-value>
  	</init-param>
  </servlet>
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	  <!-- 第一種:*.action 訪問以*.action爲結尾的DispatcherServlet進行解析
  	  		第二種:/  全部訪問地址都有DispatcherServlet進行解析,
 					對於靜態資源的解析須要配置不讓DispatcherServlet進行解析,此種方法能夠實現resuful的url
  	  		第三種:/* 這樣不對,這種配置,在轉發jsp到一個jsp頁面時,
  	  				任然是有DispatcherServlet解析jsp地址,不能根據jsp查找到handler會報錯
  	   -->
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>

  
</web-app>

2 配置咱們 springmvc.xml 配置文件java

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

<!-- 3  配置handler  "/queryUser.action": 就是請求的url  -->
<bean  id="/queryUser.action" class="com.shi.controller.ItemsController1"></bean>

<!-- 2  配置處理器映射器
	將bean的name做爲url進行查找,須要配置Handler時,指定beanname就是url-->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!-- 1  配置處理器適配器
	全部的適配器都要實現 HandlerAdapter 接口
	SimpleControllerHandlerAdapter適配器 要求咱們的handler實現Controller接口-->
	<bean  class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

<!-- 4  配置試圖解析器  解析jsp的 視圖解析器:默認使用jstl標籤 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

3 編寫 handler 注意 上面配置文件的要求web

package com.shi.controller;

import java.util.ArrayList;
import java.util.List;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.shi.pojo.User;

/**
 * SimpleControllerHandlerAdapter該適配器要求實現Controller接口
 * @author SHF
 *
 */
public class ItemsController1 implements Controller{

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		//模擬靜態數據
		List<User> user_list=new ArrayList<User>();
		
		User user1=new User();
		user1.setUsername("張三");
		User user2=new User();
		user2.setUsername("李四");
		
		user_list.add(user1);
		user_list.add(user2);
		
		//建立模型和視圖對象
		ModelAndView mv=new ModelAndView();
		mv.addObject("user_list", user_list);
		
		mv.setViewName("WEB-INF/jsp/user.jsp");
		
		return mv;
	}
}

4 注意點spring

輸入圖片說明

輸入圖片說明

輸入圖片說明

二:非註解的處理器映射器和適配器

1 處理器映射器,(多個映射器能夠並存,前端控制器判斷能讓哪一個映射器處理 就讓哪一個映射器處理)json

<!-- 3  配置handler  "/queryUser.action": 就是請求的url  -->
<bean id="itemsController"  name="/queryUser.action" class="com.shi.controller.ItemsController1"></bean>
					<!-- 全部的映射器都實現HanderMapping接口 -->
<!-- 2 .1 配置處理器映射器
	將bean的name做爲url進行查找,須要配置Handler時,指定beanname就是url-->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>

<!-- 2.3 配置簡單的 url 映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
		<props >
		<!-- 對 itemsController進行url映射,url是:/query1.action -->
			<prop key="/query1.action">itemsController</prop>
			<prop key="/query2.action">itemsController</prop>
		</props>
	</property>
</bean>

2 處理器適配器 (也能夠並存)spring-mvc

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

<!-- 3  配置handler    -->
<bean id="HttpController" class="com.shi.controller.ItemsController2"></bean>

					<!-- 全部的映射器都實現HanderMapping接口 -->

<!-- 2.3 配置簡單的 url 映射器 -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
		<props >
			<prop key="/HttpController.action">HttpController</prop>
		</props>
	</property>
</bean>


<!-- 1.1  配置處理器適配器  SimpleControllerHandlerAdapter
	全部的適配器都要實現 HandlerAdapter 接口
	SimpleControllerHandlerAdapter適配器 要求咱們的handler   實現Controller接口 -->
	<bean  class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>

<!-- 1.2  配置處理器適配器HttpRequestHandlerAdapter    
		要求controller實現 HttpRequestHandler 接口 -->
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"></bean>

<!-- 4  配置試圖解析器  解析jsp的 視圖解析器:默認使用jstl標籤 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

輸入圖片說明

輸入圖片說明

輸入圖片說明

三 : 註解的處理器映射器 和 註解的 處理器適配器配置

輸入圖片說明

輸入圖片說明

輸入圖片說明

案例 :mvc

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

			<!-- 註解的開發 -->
<!-- 2  配置 掃描包下的 handler -->
<context:component-scan base-package="com.shi.controller"></context:component-scan>
			
<!-- 1  配置註解掃描驅動  單獨配置 註解映射器和註解適配器
	mvc:annotation-driven 默認加載不少參數綁定的方法(推薦使用)
	好比json默認轉換器就默認加載了,
 -->
<mvc:annotation-driven></mvc:annotation-driven>

<!-- 3  配置試圖解析器  解析jsp的 視圖解析器:默認使用jstl標籤 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>

</beans>

Handlerapp

package com.shi.controller;

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


// @Controller 標識 他是一個控制器
@Controller
public class UserHandler {
	
	//@RequestMapping 實現對queryUsers的url映射 ,一個方法對應一個url
	@RequestMapping("/queryUsers")
	public ModelAndView queryUsers()throws Exception{
		ModelAndView mv =new ModelAndView();
		mv.setViewName("WEB-INF/jsp/user.jsp");
		return mv;
	}
}

四 視圖解析器

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <!--  默認加載jstl標籤 因此不須要配置jstl了-->
		<property name="prefix" value="/WEB-INF/"></property>
		<property name="suffix" value=".jsp"></property>
</bean>

五 源碼分析

輸入圖片說明

輸入圖片說明

輸入圖片說明

輸入圖片說明

相關文章
相關標籤/搜索