SpringMVC的初体验-3

一. SpringMVC对Servlet API的支持
  1. 使用Servlet API主要是想使用HttpServletRequest request和HttpServletResponse response以及HttpSession session。

  2. 控制层Controller

@Controller
@RequestMapping("/user")
public class UserController {

	@RequestMapping("/login")
	//引用的request和response直接以参数形式给出即可,SpringMVC会自动DI。
	public String login(HttpServletRequest request,HttpServletResponse response){
		System.out.println("----登录验证---");
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		Cookie cookie=new Cookie("user",userName+"-"+password);
		cookie.setMaxAge(1*60*60*24*7);
		User currentUser=new User(userName,password);
		response.addCookie(cookie);
		HttpSession session=request.getSession();
		session.setAttribute("currentUser", currentUser);
		return "redirect:/main.jsp";
	}
	
	@RequestMapping("/login2")
	// request直接以参数形式给出,SpringMVC会自动DI。
	public String login2(HttpServletRequest request){
		return "redirect:/main.jsp";
	}
	
	@RequestMapping("/login3")
	// session也可以直接以参数形式给出,SpringMVC会自动DI。
	public String login3(HttpSession session){
		return "redirect:/main.jsp";
	}
}
二. SpringMVC对JSON的支持
  1. 主要使用在前后台通过Ajax交互时的情形

  2. 配置spring-mvc.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:p="http://www.springframework.org/schema/p"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 定义启用注解的包,一直扫描到所有子路径。 -->
    <context:component-scan base-package="com.bee"/>

	<!-- 支持对象与json的转换。 -->
    <mvc:annotation-driven/> 

    <!-- 视图解析器 -->
	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp"></property>
	</bean>

</beans>
  1. 需要引入jackson的jar包
> dir *.jar /b
jackson-annotations-2.2.1.jar
jackson-core-2.2.1.jar
jackson-core-asl-1.8.8.jar
jackson-databind-2.2.1.jar
jackson-mapper-asl-1.8.8.jar
jackson-module-jaxb-annotations-2.2.1.jar
  1. 控制层Controller
@Controller
@RequestMapping("/user")
public class UserController {
......
	@RequestMapping("/ajax")
	@ResponseBody  // 使用该注解就可以把返回的对象转换为JSON
	public User ajax(){
		User user=new User("zhangsan","123");
		return user;
	}
}
  1. 其他处理JSON的方式
  • 阿里的FastJson
  • json-lib
public class ResponseUtil {

	public static void write(HttpServletResponse response,Object o)throws Exception{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println(o.toString());
		out.flush();
		out.close();
	}
}

使用时导包
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

//单个JSON对象
......
JSONObject resultJson=new JSONObject();
resultJson.put("name", "张三");
resultJson.put("age", 22);
......
//JSON数组
......
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();

JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", "张三");
jsonObject1.put("age", 22);

JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", "李四");
jsonObject2.put("age", 23);

JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", "王五");
jsonObject3.put("age", 24);

jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);		

resultJson.put("students", jsonArray);
......
//JSON嵌套
......
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();

JSONObject jsonObject1=new JSONObject();
jsonObject1.put("name", "张三");
jsonObject1.put("age", 22);
		
JSONObject scoreObject1=new JSONObject();
scoreObject1.put("chinese", 90);
scoreObject1.put("math", 100);
scoreObject1.put("english", 80);
jsonObject1.put("score", scoreObject1);
		
JSONObject jsonObject2=new JSONObject();
jsonObject2.put("name", "李四");
jsonObject2.put("age", 23);
		
JSONObject scoreObject2=new JSONObject();
scoreObject2.put("chinese", 70);
scoreObject2.put("math", 90);
scoreObject2.put("english", 90);
jsonObject2.put("score", scoreObject2);
		
JSONObject jsonObject3=new JSONObject();
jsonObject3.put("name", "王五");
jsonObject3.put("age", 24);
		
JSONObject scoreObject3=new JSONObject();
scoreObject3.put("chinese", 80);
scoreObject3.put("math", 60);
scoreObject3.put("english", 90);
jsonObject3.put("score", scoreObject3);
		
jsonArray.add(jsonObject1);
jsonArray.add(jsonObject2);
jsonArray.add(jsonObject3);
		
resultJson.put("students", jsonArray);
......
//使用封装好的ResponseUtil类返回json
......
ResponseUtil.write(response, resultJson);
......
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值