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 配置文件

<?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 注意 上面配置文件的要求

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 注意点

输入图片说明

输入图片说明

输入图片说明

二:非注解的处理器映射器和适配器

1 处理器映射器,(多个映射器可以并存,前端控制器判断能让哪个映射器处理 就让哪个映射器处理)

<!-- 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 处理器适配器 (也可以并存)

<?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>

输入图片说明

输入图片说明

输入图片说明

三 : 注解的处理器映射器 和 注解的 处理器适配器配置

输入图片说明

输入图片说明

输入图片说明

案例 :

<?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>

Handler

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>

五 源码分析

输入图片说明

输入图片说明

输入图片说明

输入图片说明

转载于:https://my.oschina.net/u/3677987/blog/1536566

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值