Python 官方文档:入门教程 => 点击学习
目录数据处理及跳转1.结果跳转方式2.ResponseBody响应JSON数据1、导入关键jar导入依赖2、标签配置不过滤静态资源数据处理及跳转 1.结果跳转方式 ①.ModelAn
①.ModelAndView
设置ModelAndView对象 , 根据view的名称 , 和视图解析器跳到指定的页面
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5"/>
</bean>
对应的controller类:
@RequestMapping("/save3")
public ModelAndView save3(){
System.out.println("执行了...");
// 创建mv对象
ModelAndView mv = new ModelAndView();
// 把一些数据,存储到mv对象中
mv.addObject("msg","用户名或者密码已经存在");
// 设置逻辑视图的名称
mv.setViewName("suc");
return mv;
}
②.Servletapi:请求转发和重定向
通过设置ServletAPI , 不需要视图解析器 .
1、通过httpservletResponse进行输出
2、通过HttpServletResponse实现重定向
3、通过HttpServletResponse实现请求转发
(1)请求转发
1、是服务器端的行为
2、请求地址不发生改变,用户从始至终都是同一个请求
3、request数据在servlet共享,因为从始至终都是同一个请求
4、通过setAttribute进行数据传递,getAttribute进行数据接收
(2)重定向
1.服务器端指导,客服端行为的行为
2.两次request请求
3.用户浏览器访问的地址栏也会发生变化
4.重定向可以解决跨域问题
跨域:我们当前访问的地址不是我们项目中的地址
@RequestMapping("/test1")
public void test1(HttpServletRequest request, HttpServletResponse response) throws ioException, ServletException {
System.out.println("test1=" + request.getParameter("name"));
request.setAttribute("msg", "你好");
//request.getRequestDispatcher("/dept/test2").forward(request,response);
request.getRequestDispatcher("/html/suc.html").forward(request, response);
}
@RequestMapping("t3")
public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException {
System.out.println("我是test3");
//response.sendRedirect("/springMVCTest/dept/t4");
response.sendRedirect("/SpringmvcTest/html/suc.html");
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
DispatcherServlet会拦截到所有的资源,导致一个问题就是静态资源(img、CSS、js)也会被拦截到,从而不能被使用。解决问题就是需要配置静态资源不进行拦截,在springmvc.xml配置文件添加如下配置
<!--设置静态资源不过滤-->
<mvc:resources mapping="/css
@RequestMapping("/save6")
public @ResponseBody User save6(User user){
System.out.println(user);
// 模拟,调用业务层代码
user.setUsername("hello");
user.setAge(100);
// 把user对象转换成json,字符串,再响应。使用@ResposeBody注解 response.getWriter().print()
return user;
}
到此这篇关于SpringMVC响应处理详细解读的文章就介绍到这了,更多相关SpringMVC响应内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringMVC响应处理详细解读
本文链接: https://www.lsjlt.com/news/165436.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0