1.1 返回ModelAndView
@RequestMapping("/func1.action")
public ModelAndView func1() {
System.out.println("func1..");
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index02.jsp");
modelAndView.addObject("username", "admin");
modelAndView.addObject("age", 18);
return modelAndView;
}
1.2 返回String
/**
* 返回值String其实是视图的名称,在Model中可以设置数据
* @param model
* @return
*/
@RequestMapping("/func1.action")
public String func1(Model model) {
System.out.println("func1..");
model.addAttribute("age",18);
return "index02.jsp";
}
1.3 返回对象
<?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: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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven/>
<!--配置注解扫描器-->
<context:component-scan base-package="com.it.bigdata"/>
<!--处理器映射器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!--处理器适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!--视图解析器-->
<!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"></property>
</bean>-->
</beans>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
@RequestMapping("/func1.action")
@ResponseBody
public User func1() throws IOException {
User user = new User();
user.setName("小花");
user.setAge(20);
return user;//把对象转换成json字符串进行返回了
}
1.4 返回void
@RequestMapping("/func1.action")
public void func1(Model model, HttpServletRequest request, HttpServletResponse resp) throws IOException {
System.out.println("func1..");
User user = new User();
user.setName("小花");
user.setAge(20);
System.out.println(request.getParameter("id"));
resp.getWriter().write("hello");
}
1.5 处理器的参数
-
可以直接以参数名字当做形参
@RequestMapping("/func1.action")
public void func1( Integer id) throws IOException {
System.out.println(“func1…”);
System.out.println(id);
} -
可以和形参不一致(不建议使用)
@RequestMapping("/func1.action")
public void func1(@RequestParam(“id”) Integer idd) throws IOException {
System.out.println(“func1…”);
System.out.println(idd);
} -
使用request获取参数
@RequestMapping("/func1.action")
public void func1(Model model, HttpServletRequest request, HttpServletResponse resp) throws IOException {
System.out.println(request.getParameter(“id”));
} -
把参数直接包装成对象
@RequestMapping("/func1.action")
@ResponseBody
public User func1(User user) throws IOException {
System.out.println(user);
return user;
}
1.6. 返回Json对象
- 返回对象的做法
- 自己使用fastjson或者其他转换json的工具实现
@RequestMapping("/func1.action")
@ResponseBody
public String func1(User user) throws IOException {
System.out.println(user);
return JSON.toJSONString(user);
}