SpringMVC数据响应-页面跳转
1.SpringMVC的数据响应方式
(1)页面跳转
- 直接返回字符串
- 通过ModelAndView对象返回
(2)回写数据
- 直接返回字符串
- 返回对象或集合
2.页面跳转
2.1 返回字符串形式
直接返回字符串:此种方式会将返回的字符串与视图解析器的前后缀拼接后跳转
为什么转发有/WEB-INF/views/,而重定向没有了
重定向代表客户端再次访问,再次请求,而WEB-INF属于一个受保护的文件夹,外界不能直接访问,要想重定向,资源必须处在一个可以有权限被访问的位置
2.2 返回ModelAndView对象
方式一:
在UserController中
index.jsp中
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>helll</title> </head> <body> <p>index_success!!${username}</p> </body> </html>
输入请求
方式二:
方式三:
这些save方法都是最终spring帮我们调用的, 如果写的是save(Model model)这样的形参的话,springmvc调用这个方法时,会传给实际的model对象,spring内部能用;如果save(ModelAndView modelAndView)springmvc调用这个方法时,会传给实际的modelAndView对象供你使用。
方式四:
想要通过request存数据,需要request对象,通过request.setAttribute()可以往request域中存数据,在转发页面显示数据,
但是这种方式不常用
完整项目代码:
UserController.java
package com.company.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/user") public class UserController { @RequestMapping(value = "/quick2") public ModelAndView save2() { /** * Model:模型 作用封装数据 * View:视图 作用展示数据 */ ModelAndView modelAndView = new ModelAndView(); // 设置模型数据 modelAndView.addObject("username","root"); // 设置视图名称 modelAndView.setViewName("index"); return modelAndView; } // 访问quick时,帮你映射到save()方法,执行下面的逻辑,最终返回,跳到success.jsp视图中 // 请求地址,http://localhost:8080/user/quick @RequestMapping(value = "/quick", method = RequestMethod.GET, params = {"username"}) public String save() { System.out.println("Controller save running..."); return "success"; } }
UserDao.java
package com.company.dao; public interface UserDao { void save(); }
UserDaoImpl.java
package com.company.dao.impl; import com.company.dao.UserDao; public class UserDaoImpl implements UserDao { public void save() { System.out.println("save running..."); } }
UserService.java
package com.company.service; public interface UserService { void save(); }
UserServiceImpl
package com.company.service.impl; import com.company.dao.UserDao; import com.company.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save() { userDao.save(); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置userDao--> <bean id="userDao" class="com.company.dao.impl.UserDaoImpl"></bean> <!--配置userService--> <bean id="userService" class="com.company.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>
spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--Controller组件扫描--> <context:component-scan base-package="com.company.controller"/> <!--配置内部资源视图解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!--/jsp/success.jsp--> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置springmvc的前端控制器--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--全局初始化参数--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--配置监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>UserServlet</servlet-name> <servlet-class>com.company.web.UserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/userServlet</url-pattern> </servlet-mapping> </web-app>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>helll</title> </head> <body> <p>index_success!!${username}</p> </body> </html>