springMVC 几种页面跳转方式

你的名字 2021-12-24 09:39 589阅读 0赞

1.在注解的方式中

1.1通过HttpServletResponse的API直接输出(不需要配置渲染器)

controller类的主要代码

  1. @Controller
  2. public class RequestController{
  3. @RequestMapping("/resp")
  4. public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
  5. resp.getWriter().println("hello HttpServletResponse");
  6. }

web.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  5. version="3.1">
  6. <servlet>
  7. <servlet-name>dispatcher</servlet-name>
  8. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  9. <load-on-startup>1</load-on-startup>
  10. </servlet>
  11. <servlet-mapping>
  12. <servlet-name>dispatcher</servlet-name>
  13. <url-pattern>/</url-pattern>
  14. </servlet-mapping>
  15. </web-app>

dispatcher-servlet.xml主要代码

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  9. <!--作用是扫描指定包下所有的包含注解的类-->
  10. <context:component-scan base-package="com.jsu.mvc"/>
  11. </beans>

1.2 使用HttpServletResponse 重定向到另一个视图(其他不变 )

  1. @RequestMapping("/resp")
  2. public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
  3. resp.sendRedirect("index.jsp");
  4. }
  5. }

1.3 使用HttpServletRequest 转发(默认访问/下的index.jsp页面 不受渲染器的影响)

  1. @RequestMapping("/resp")
  2. public void handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
  3. req.setAttribute("message","it's forword ");
  4. req.getRequestDispatcher("index.jsp").forward(req,resp);
  5. }

1.4直接返回jsp页面的名称(无渲染器)

其他的配置不变

  1. @RequestMapping("/nice")
  2. public String hello1(){
  3. //转发方式1
  4. return "home.jsp";
  5. //转发方式2
  6. return "forward:index.jsp";
  7. //重定向方式
  8. return "redirect:index.jsp";
  9. }

1.5当有渲染器指定

  1. @RequestMapping("/nice")
  2. public String hello1(){
  3. //转发方式1
  4. return "home";
  5. //转发方式2
  6. return "forward:index";
  7. //重定向方式 hello指的是requsrmapping
  8. return "redirect:hello";
  9. }

2 使用view

2.1 使用modelandview

需要视图解析器 能指定跳转页面

  1. public class HelloController implements Controller {
  2. @Override
  3. public ModelAndView handleRequest(javax.servlet.http.HttpServletRequest httpServletRequest,
  4. javax.servlet.http.HttpServletResponse httpServletResponse) throws Exception {
  5. ModelAndView mv = new ModelAndView();
  6. //封装要显示到视图的数据
  7. mv.addObject("msg","hello myfirst mvc");
  8. //视图名
  9. mv.setViewName("hello");
  10. return mv;
  11. }
  12. }

[servlet-name]-servlet.xml

  1. <!--配置渲染器-->
  2. <!--配置hellocontroller中页面的位置-->
  3. <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
  4. <bean id="viewResolver"
  5. class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  6. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  7. <!--结果视图的前缀-->
  8. <property name="prefix" value="/WEB-INF/jsp/"/>
  9. <!--结果视图的后缀-->
  10. <property name="suffix" value=".jsp"/>
  11. </bean>
  12. <bean name="/hello.do" class="com.jsu.mvc.HelloController"></bean>

2.2 使用modelview

不需要视图解析器 不能指定跳转页面

  1. //通过modelmap方式
  2. @RequestMapping("/modelmap")
  3. public String modelHello(String name,ModelMap map){
  4. map.addAttribute("name",name);
  5. System.out.println(name);
  6. return "index.jsp";
  7. }

转载于:https://www.cnblogs.com/userzf/p/11016352.html

发表评论

表情:
评论列表 (有 0 条评论,589人围观)

还没有评论,来说两句吧...

相关阅读