springMVC系列之注解优化版——04

springMVC系列之注解优化版——04


摘要:未接触springMVC时、高效开发、灵活、低耦合、与spring的无缝连接、等种种优点就萦绕耳边、之前的接触的虽然有所显露、但并不是多明显、这里对其进行优化、从优化后的注解版的简洁、特性可窥一斑。


一:优化地方


        1、使用mvc标签代替设置扫描的两个bean

        2、给不同的controller增加“包空间”、避免请求冲突、同样也减少代码量、不用在每个方法上写上完成的访问路径。

        3、使得方法既能处理get请求、又能处理post请求。

        4、直接在方法上的@RequestMapping("url映射")。这样就是value为“url映射" 同时处理get、post

        5、改变return的值、不再返回ModeAndView、直接返回一个指定相应页面的名称的一个字符串、至于如何传递参数、使用HttpServletRequest

HttpServletRequest的获取是可以在方法中的参数给出的、可以给出一个request、也可以给出一个response、也可同时给出、真的是很强大、很灵活

exp: UserController下的add()方法   xxx add(HttpServletRquest request,HttpServletResponse response){xxx}


二:具体优化


        直接给出代码、代码中说明各优化地方及特性。

1、springAnnotation-servlet.xml:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd">
      
      
    <!-- 注解扫描包 -->
	<context:component-scan base-package="com.chy.web.controller.annotation"></context:component-scan>
	
	<!-- 优化一:使用mvc标签的代替下面的两个bean -->
	<mvc:annotation-driven/>
	
	<!-- 开启注解
		<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>	
		<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>	
	 -->	
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
 </beans>  

2、OptimizedUserController:


package com.chy.web.controller.annotation;

import javax.servlet.http.HttpServletRequest;

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

@Controller
@RequestMapping("/user2")//全局的url的前面的一个提取、这样在下面的每个方法上的原来的这个字段就不用再一一重复写出
public class OptimizedUserController {
	/*
	 * 原:@RequestMapping(value="/user/toUser", method=RequestMethod.GET)
	 * 新:@RequestMapping("/toUser")
	 * 1、将/user提出、
	 * 2、缺少了method、现在很多请求都是用ajax来发送、如果是这样我们每次都要
	 * 区别到底是get还是post、肯定是烦不胜烦、在不严格要求的情况下、可以让它
	 * 既可以处理get、也可以处理post。简单到底:直接不写method属性。
	 * 3、少了method、还有个value、既然每个方法上都有value=、同样不但没有达到
	 * 重用性、看着也不舒服、直接省略。
	 */
	
	@RequestMapping("/toUser")
	public String toUser(){
		return "/annotation";
	}

	/*
	 * 原:return new ModelAndView("/annotation","paramName",object);
	 * 新:return "/annotation";
	 * 1、不在使用对象ModelAndView
	 * 2、使用表示响应页面的名称String
	 * 3、使用原始的request向页面传递参数
	 * 4、可通过为方法传递参数 HttpServletRequest 或者	HttpServletResponse获取request或者response、
	 * 前面的两个参数可以都传递给方法、可以一个同样可以不给一个。
	 */

	
	@RequestMapping("/addUser")
	public String addUser(HttpServletRequest request){
		String result="this is addUser-------";
		request.setAttribute("result", result);
		
		return "/annotation";
	}
	
	@RequestMapping("/delUser")
	public String delUser(HttpServletRequest request){
		String result="this is delUser-------";
		request.setAttribute("result", result);
		return "/annotation";
	}
}

更多内容: springMVC系列之目录——00


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值