返回数值
@Controller
public class returnObject {
@RequestMapping("/first")
@ResponseBody
public Object doFirst()
{
return 1;
}
//返回字符串
@RequestMapping(value = "/str")
@ResponseBody
public String dos()
{
return "么么哒";
}
public class userInfo {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
// /处理器返回值 List
@RequestMapping(value = "lis")
@ResponseBody
public Object doList(){
List<userInfo> list=new ArrayList<userInfo>();
userInfo info=new userInfo();
info.setName("啦啦");
info.setAge(12);
return list;
}
@RequestMapping(value = "/third")
@ResponseBody
public Object doThird(){
UserInfo info=new UserInfo();
info.setName("星星");
info.setAge(20);
return info;
}
@RequestMapping(value = "/four")
@ResponseBody
public Object doFour(){
Map<String,UserInfo> map=new HashMap<String, UserInfo>();
UserInfo info=new UserInfo();
info.setName("微冷的雨");
info.setAge(20);
UserInfo info2=new UserInfo();
info2.setName("微冷的雨2");
info2.setAge(20);
map.put(info.getName(),info);
map.put(info2.getName(),info2);
return map;
}
@RequestMapping(value = "/five")
@ResponseBody
public Object doFive(){
List<UserInfo> list=new ArrayList<UserInfo>();
UserInfo info=new UserInfo();
info.setName("微冷的雨");
info.setAge(20);
list.add(info);
return list;
}
需要导入的两个jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1<
ersion>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1<
ersion>
</dependency>
package cn.happy.controller06exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping
e cn.happy.controller05requestdirect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Happy on 2017-08-20.
*/
@Controller
public class FirstController {
@RequestMapping("/add")
public String addUser(){
return "/WEB-INF/insert.jsp";
}
@RequestMapping("/addUser")
public String addRealUser(HttpServletRequest request){
request.setAttribute("happy","goodperson");
return "/listUser";
}
@RequestMapping("/listUser")
public String list(){
return "/WEB-INF/list.jsp";
}
@RequestMapping("/addUser2")
public String addRealUser2(HttpServletRequest request){
request.setAttribute("happy","goodperson");
return "redirect:/listUser2";
}
@RequestMapping("/listUser2")
public String list2(){
return "redirect:/list2.jsp";
}
}
@Controller
public class FirstController {
@RequestMapping("/list")
public String list(){
//构造异常
int result=5/0;
return "/WEB-INF/list.jsp";
}
}