一. 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
  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);
......