首页 > 代码库 > SpringMVC07处理器方法的返回值

SpringMVC07处理器方法的返回值

技术分享
  <body>
  <!--返回值是string的内部视图  -->
   <a href="user/add">add</a>
  <!--返回值是string的外部视图  -->
   <a href="user/taobao">淘宝</a>
  <!--没有返回值  转发到内部视图  -->
   <a href="user/request">request</a>
  <!--没有返回值  重定向到内部视图  -->
   <a href="user/response">response</a>
  
  </body>
index.jsp页面

 

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:context="http://www.springframework.org/schema/context" 
    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-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- 配置需要扫描的包 -->
<context:component-scan base-package="cn.bdqn.controller"/>
<!-- 开启注解
<mvc:annotation-driven/> -->

<!-- 外部视图  -->
<bean id="taobao" class="org.springframework.web.servlet.view.RedirectView">
 <property name="url" value="http://www.taobao.com"/>
</bean>

<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

</beans>
springmvc-servlet.xml文件

 

技术分享
@Controller
@RequestMapping("/user")
public class MyController {
    /**
     * @return 返回的是内部视图的名称
     */
    @RequestMapping("/add")
    public String add() {
        System.out.println("进入了add...........");
        return "/WEB-INF/jsp/success.jsp";
    }

    /**
     * 
     * @return 返回外部视图
     */
    @RequestMapping("/taobao")
    public String taobao() {
        System.out.println("进入了taobao...........");
        return "taobao";
    }

    /**
     * 之前使用HttpServletRequest 转发
     */
    @RequestMapping("/request")
    public void doRequest(HttpServletRequest request,
            HttpServletResponse response) {
        System.out.println("进入了request...........");
        try {
            request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(
                    request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 之前使用HttpServletResponse  重定向
     * @throws IOException 
     */
    @RequestMapping("/response")
    public void doResponse(HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        System.out.println("进入了response...........");
        response.sendRedirect("success.jsp");
    }

}
MyController中的代码

 

SpringMVC07处理器方法的返回值