SpringMVC:响应数据和结果视图
2021-06-09 06:04
阅读:541
YPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
标签:lazy domain clean c3p0 img java string exception hang
一、返回值分类
1.字符串
2.void
3.ModelAndView
二、转发和重定向
1.forward转发
2.Redirect重定向
三、ResponseBody响应json数据
需要在pom.xml中添加如下代码:
"1.0" encoding="UTF-8"?>"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 cn.itcast ssm1.0-SNAPSHOT war ssm Maven Webapp http://www.example.com UTF-8 1.8 1.8 5.0.2.RELEASE 1.6.6 1.2.12 5.1.6 3.4.5 org.aspectj aspectjweaver1.6.8 org.springframework spring-aop${spring.version} org.springframework spring-context${spring.version} org.springframework spring-web${spring.version} org.springframework spring-webmvc${spring.version} org.springframework spring-test${spring.version} org.springframework spring-tx${spring.version} org.springframework spring-jdbc${spring.version} junit junit4.12 compile mysql mysql-connector-java${mysql.version} javax.servlet servlet-api2.5 provided javax.servlet.jsp jsp-api2.0 provided jstl jstl1.2 log4j log4j${log4j.version} org.slf4j slf4j-api${slf4j.version} org.slf4j slf4j-log4j12${slf4j.version} org.mybatis mybatis${mybatis.version} org.mybatis mybatis-spring1.3.0 com.fasterxml.jackson.core jackson-databind2.9.0 com.fasterxml.jackson.core jackson-core2.9.0 com.fasterxml.jackson.core jackson-annotations2.9.0 c3p0 c3p00.9.1.2 jar compile ssm maven-clean-plugin 3.0.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.7.0 maven-surefire-plugin 2.20.1 maven-war-plugin 3.2.0 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2
完整代码:
jsp代码:
"text/html;charset=UTF-8" language="java" %> String path=request.getContextPath(); %>重定向 "/user/testReturnString">testReturnString
"/user/testReturnVoid">testReturnVoid
"/user/testModelAndView">testModelAndView
"/user/testForwardOrRedirect">testForwardOrRedirect
"button" value="发送ajax的请求" id="btn">
控制层
package cn.itcast.controller; import cn.itcast.domain.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @Controller("jsonController") @RequestMapping("/user") public class UserController { @RequestMapping("/testReturnString") public String testReturnString(){ System.out.println("testReturnString方法执行了............"); return "success"; } @RequestMapping("testReturnVoid") public void testReturnVoid( HttpServletRequest request, HttpServletResponse response) throws Exception{ System.out.println("testVoid方法执行了..."); // 编写请求转发的程序 /* request.getRequestDispatcher("/WEB-INF/pages/success.jsp") .forward(request,response);*/ // 重定向:这种方式相当于重新定向到webapp下的根目录下去寻找. //response.sendRedirect(request.getContextPath()+"/index.jsp"); // 设置中文乱码 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); // 直接会进行响应 response.getWriter().print("你好"); return; } /** * 使用关键字的方式进行转发或者重定向 * @return */ @RequestMapping("/testForwardOrRedirect") public String testForwardOrRedirect(){ System.out.println("testForwardOrRedirect方法执行了..."); // 请求的转发 // return "forward:/WEB-INF/pages/success.jsp"; // 重定向 return "redirect:/index.jsp"; } /* * 返回ModeAndView * @return * */ @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ System.out.println("testModelAndView方法执行了..."); // 创建ModelAndView对象 ModelAndView mv = new ModelAndView(); // 模拟从数据库中查询出User对象 User user = new User(); user.setUsername("小凤"); user.setPassword("456"); user.setAge(30); // 把user对象存储到mv对象中,也会把user对象存入到request对象 mv.addObject("user",user); // 跳转到哪个页面 mv.setViewName("success"); return mv; } /** * 模拟异步请求响应 */ @RequestMapping("/testAjax") public @ResponseBody User testAjax(@RequestBody User user){ System.out.println("testAjax方法执行了..."); // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中 System.out.println(user); // 做响应,模拟查询数据库 user.setUsername("haha"); user.setAge(40); // 做响应 return user; } }
SpringMVC:响应数据和结果视图
标签:lazy domain clean c3p0 img java string exception hang
原文地址:https://www.cnblogs.com/byczyz/p/14497596.html
上一篇:一致性hash算法
评论
亲,登录后才可以留言!