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);
......
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章