Springmvc基础三之注解深入学习

上一节讲到注解的基本使用;这一节我们来更加深入的学习;

先说一下上一节的注解怎么优化:

前面一章在配置文件 里要配置两个Bean来解析注解;Springmvc3.0已经把它们合在一起了;看如下配置:

<!--  
   
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
 
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
  -->
  <!-- 此注解代表了上面两个类 -->
  <mvc:annotation-driven/>

 还有就是在控制器里的优化来看一看;

package com.tgb.web.controller.anotation;


import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
//此注解代表这个是一个控制器;必须配置
@Controller
@RequestMapping("/test")
public class MyAnotationController2 {

	
	@RequestMapping("/testget")
	public String testGet(HttpServletRequest request){
		String result = "test anotation  get方式";
		request.setAttribute("result", result);
		return "anotation";
	}
	
	@RequestMapping(value="/testpost")
	public ModelAndView testPost() {
		String result = "test anotation post方式";
		return new ModelAndView("anotation","result",result);
	}
	
	@RequestMapping(value="/topost")
	public ModelAndView toPost() {
		return new ModelAndView("anotationpost");
	}
}

此处做了一些简单的优化:

在类上有一个包的一个Url;以前在方法上配置的URL是  /test/testget;现在可以把第一个test放到类上去;当作所有方法的共用;

第二个方法上面的配置也少了;上一节

//此注解有两个参数:一个是Value为Url;即我们通过哪个请求到达这个方法;  还有一个method;访问 方法的名称 ;常用 的是Get和Post两种;
	//然后和以前的一样;返回 一个ModelAndView;
	@RequestMapping(value="/test/testget",method=RequestMethod.GET)
	public ModelAndView testGet() {
		String result = "test anotation  get方式";
		return new ModelAndView("anotation","result",result);
	}

这里有一个value; 和一个method方法;这里可以不用配置GET或者 Post;Springmvc自动帮你分析这一点;value也可以不用写出来;

还有一个看返回值 :以前要返回一个ModelAndView 这里只需要一个String类型即可;这个String类型就是View;那这里就有一个问题了;怎么返回参数呢;

可以用request.setAttribute();


那怎么接收参数呢?

Springmvc里为我们提供了很好的支持:

@RequestMapping(value="/testpost2")
	public String  testPost(HttpServletRequest request,String username) {
		String result = "test anotation post方式";
		request.setAttribute("result",username);
		return "anotation";
	}

我们只需要将这个参数名和传过来的参数名字对应一样就可以得到 了;当然如果传过来的是中文那么我们需要在WEB。XML文件 里配置一个格式过滤的一个Filter

<!-- 配置一个Filter为设置编码格式  -->
  <filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<!-- encoding filter for jsp page -->
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

如果要传入很多值;设置一个对象:

@RequestMapping(value="/testpost2")
	public String  testPost(HttpServletRequest request,User user) {
		String result = "test anotation post方式";
		request.setAttribute("result",user.getUsername());
		return "anotation";
	}
	

调用的同样是User的Set和Get方法;



传输JSON数据:前台JS提交

	$(function() {
		$("#buttonSubmit").click(function() {
			var username = $("#username").val();
			$.ajax({
				type : "POST",
				url:"/spring01/test/testpost2",
				
				data:{username:username},
				success:function(data){
					alert(data.username);
				}
			});
		}) ;
	});

后台:
@RequestMapping(value="/testpost2")
	public void  testPost(HttpServletRequest request,HttpServletResponse response,User user) {
		String result = "test anotation post方式";
		PrintWriter out = null;
		String json = "{\"username\":\"" + user.getUsername() +"\"}";
		try {
			response.setContentType("text/json; charset=UTF-8");
			out = response.getWriter();
			out.println(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

项目例子下载


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值