700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > springmvc之响应类型 string void ModelAndView

springmvc之响应类型 string void ModelAndView

时间:2021-09-10 04:53:12

相关推荐

springmvc之响应类型 string void ModelAndView

目录

响应之返回值是String类型

响应之返回值是void类型

响应之返回值是ModelAndView类型

响应之返回值是String类型

springmvc.xml需要先配置好视图解析器的jsp文件路径及对应文件的后缀名

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:mvc="/schema/mvc"xmlns:context="/schema/context"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans.xsd/schema/mvc/schema/mvc/spring-mvc.xsd/schema/context/schema/context/spring-context.xsd"><!--开启组件扫描,视图解析器,返回的jsp文件配置--><context:component-scan base-package="com.itcatst"></context:component-scan><bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/pages/"></property><property name="suffix" value=".jsp"></property></bean><bean id="conversionServiceFactoryBean" class="org.springframework.context.support.ConversionServiceFactoryBean"><property name="converters"><set><bean id="stringToDateConverter" class="com.itcatst.utils.StringToDateConverter"></bean></set></property></bean><mvc:annotation-driven conversion-service="conversionServiceFactoryBean"></mvc:annotation-driven></beans>

返回的jsp页面,开启EL表达式,取出存入域中的数据

<%--Created by IntelliJ IDEA.User: MocarDate: /9/12Time: 17:16To change this template use File | Settings | File Templates.--%><%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %><html><head><title>Title</title></head><body><h3>success...</h3><br>${us.username}<br>${us.toString()}</body></html>

controller层

/*** @Date /9/12 17:57* by mocar*/@Controller@RequestMapping(path = "/user")public class UserController {@RequestMapping("/testString")public String testString(Model model){System.out.println("执行了testString....");User user = new User();user.setUsername("张三");user.setPassword("aaaaaa");user.setAge(34);model.addAttribute("us",user);//model把对象存入request域中System.out.println(user);return "success";}}

测试:

响应之返回值是void类型

请求可返回页面,也可返回流

情况1:不指明跳转页面的情况下

@RequestMapping("/testVoid")public void testVoid(){System.out.println("testVoid....");}

默认会去找url请求,以请求为名字的jsp

情况2:指明跳转页面

转发,一次请求,通过请求直接访问页面:

public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {System.out.println("testVoid....");/*转发 不会调用mvc的视图解析器,需要写相对web-app的文件路径*/request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);return;}

重定向,两次请求,返回需要请求的页面自己再次请求:

不能直接访问WEB-INF下的页面的,只能访问web-app下的资源,否则报错:

public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {/*重定向*//*String contextPath = request.getContextPath();System.out.println(contextPath);///springmvc_response_warresponse.sendRedirect(contextPath+"/WEB-INF/pages/success.jsp");//写法报错return;}

正确写法

public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {System.out.println("testVoid....");/*重定向*//*String contextPath = request.getContextPath();System.out.println(contextPath);///springmvc_response_war//response.sendRedirect(contextPath+"/WEB-INF/pages/success.jsp");//写法报错response.sendRedirect(contextPath+"/index.jsp");return;}

情况3:返回输出流,需要设置编码

public void testVoid(HttpServletRequest request, HttpServletResponse response) throws Exception {System.out.println("testVoid....");/*转发 不会调用mvc的视图解析器,需要写相对web-app的文件路径*///request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request,response);/*重定向*//*String contextPath = request.getContextPath();System.out.println(contextPath);///springmvc_response_war//response.sendRedirect(contextPath+"/WEB-INF/pages/success.jsp");//写法报错response.sendRedirect(contextPath+"/index.jsp");*//*返回输出流,需要设置编码*/response.setCharacterEncoding("UTF-8");response.setContentType("text/html;charset=UTF-8");PrintWriter writer = response.getWriter();writer.print("熊大,你好");return;}

返回输出流

响应之返回值是ModelAndView类型

springmvc提供的对象,实现model接口

可以将对象存入request域

视图解析器会去解析跳转的页面

@RequestMapping("/testModelAndView")public ModelAndView testModelAndView(){System.out.println("testModelAndView....");ModelAndView mv = new ModelAndView();//springmvc提供的对象,实现model接口User us = new User();us.setUsername("熊大");us.setPassword("ffffff");us.setAge(34);mv.addObject("us",us);//存入request域mv.setViewName("success");//视图解析器会去解析跳转的页面System.out.println(us);return mv;}

响应之使用关键字forward和redirect进行页面跳转

关键字forward:

@RequestMapping("/testForwardAndRedirect")public String testForwordAndRedirect(){System.out.println("testForwardAndRedirect....");return "forward:/WEB-INF/pages/success.jsp";}

关键字redirect:

@RequestMapping("/testForwardAndRedirect")public String testForwordAndRedirect(){System.out.println("testForwardAndRedirect....");//return "forward:/WEB-INF/pages/success.jsp";// 符号/表示返回上一级目录,不用/则表示在请求/springmvc_response_war/user下访问,不存在index.jsp文件,报错return "redirect:index.jsp";}

正确写法

@RequestMapping("/testForwardAndRedirect")public String testForwordAndRedirect(){System.out.println("testForwardAndRedirect....");//return "forward:/WEB-INF/pages/success.jsp";return "redirect:/index.jsp";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。