目录
方式一:使用HttpServletRequest类型形参进行数据传递
方式三:使用ModelAndView类型形参进行数据传递,将该对象作为返回值传递给调用者
方式一:基于response返回数据的简化格式,返回JSON数据
方式二:使用SpringMVC提供的消息类型转换器将对象与集合数据自动转换为JSON数据
- @Controller
- public class UserController {
-
- @RequestMapping("/showPage1")
- public String showPage1(){
- System.out.println("user mvc controller is running...");
- return "forward:page.jsp";
- }
-
- @RequestMapping("/showPage2")
- public String showPage2(){
- System.out.println("user mvc controller is running...");
- return "redirect:page.jsp";
- }
-
- @RequestMapping("/showPage3")
- public String showPage3(){
- System.out.println("user mvc controller is running...");
- return "page";
- }
- }
- public class Book {
- private String name;
- private Double price;
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public Double getPrice() {
- return price;
- }
-
- public void setPrice(Double price) {
- this.price = price;
- }
-
- @Override
- public String toString() {
- return "Book{" +
- "name='" + name + '\'' +
- ", price=" + price +
- '}';
- }
- }
- @Controller
- public class BookController {
-
- @RequestMapping("/showPageAndData1")
- public String showPageAndData1(HttpServletRequest request){
- request.setAttribute("name","icpc");
- return "page";
- }
-
- @RequestMapping("/showPageAndData2")
- public String showPageAndData2(Model model){
- model.addAttribute("name","icpc");
- Book book = new Book();
- book.setName("ccpc");
- book.setPrice(66.66d);
- model.addAttribute("book",book);
- return "page";
- }
-
- @RequestMapping("/showPageAndData3")
- public ModelAndView showPageAndData3(ModelAndView modelAndView){
- Book book = new Book();
- book.setName("ccpc");
- book.setPrice(66.66d);
- modelAndView.addObject("book",book);
- modelAndView.addObject("name","icpc");
- modelAndView.setViewName("page");
- return modelAndView;
- }
- }
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/page/"/>
- <property name="suffix" value=".jsp"/>
- bean>
-
- <mvc:annotation-driven/>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-coreartifactId>
- <version>2.9.0version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-databindartifactId>
- <version>2.9.0version>
- dependency>
- <dependency>
- <groupId>com.fasterxml.jackson.coregroupId>
- <artifactId>jackson-annotationsartifactId>
- <version>2.9.0version>
- dependency>
- @Controller
- public class AccountController {
- @RequestMapping("/showData1")
- public void showData1(HttpServletResponse response) throws IOException{
- response.getWriter().write("message");
- }
-
- @RequestMapping("/showData2")
- @ResponseBody
- public String showData2(){
- return "message";
- }
-
- @RequestMapping("/showData3")
- @ResponseBody
- public String showData3() throws JsonProcessingException {
- Book book = new Book();
- book.setName("ccpc");
- book.setPrice(66.66d);
- ObjectMapper om = new ObjectMapper();
- return om.writeValueAsString(book);
- }
-
- @RequestMapping("/showData4")
- @ResponseBody
- public Book showData4(){
- Book book = new Book();
- book.setName("ccpc");
- book.setPrice(66.66d);
- return book;
- }
-
- @RequestMapping("/showData5")
- @ResponseBody
- public List
showData5(){ - Book book1 = new Book();
- book1.setName("ccpc");
- book1.setPrice(66.66d);
-
- Book book2 = new Book();
- book2.setName("acm");
- book2.setPrice(88.88d);
-
- ArrayList
al = new ArrayList<>(); - al.add(book1);
- al.add(book2);
- return al;
- }
- }