- 守望者
- 等级: 初级会员
- 文章: 28
- 积分: 62
- 来自: ...
|
ModelAndView默认是使用转发(forward)方式来完成请求的结果画面,使用RedirectView的话,则会使用重新导向(Redirect)将请求重导到指定的结果位置。如:
java 代码HelloController
- package org.beans;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.Controller;
- import org.springframework.web.servlet.view.RedirectView;
-
- public class HelloController implements Controller {
- private String viewPage;
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
- String user=request.getParameter("user");
- return new ModelAndView(viewPage,"user",user);
- }
- public void setViewPage(String viewPage) {
- this.viewPage = viewPage;
- }
-
-
- }
xml 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
-
- <beans>
- <bean name="helloController" class="org.beans.HelloController"
- abstract="false" singleton="true" lazy-init="default"
- autowire="default" dependency-check="default">
- <property name="viewPage">
- <value>hello</value>
- </property>
- </bean>
- <bean id="viewResolver"
- class="org.springframework.web.servlet.view.InternalResourceViewResolver"
- abstract="false" singleton="true" lazy-init="default"
- autowire="default" dependency-check="default">
- <property name="prefix">
- <value>/WEB-INF/jsp/</value>
- </property>
- <property name="suffix">
- <value>.jsp</value>
- </property>
- </bean>
-
- <bean id="urlHandleMapping"
- class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"
- abstract="false" singleton="true" lazy-init="default"
- autowire="default" dependency-check="default">
- <property name="interceptors">
- <list>
- <ref bean="loggingInterceptor"/>
- </list>
- </property>
- <property name="mappings">
- <props>
- <prop key="/hello.do">helloController</prop>
- </props>
- </property>
- </bean>
- <bean id="loggingInterceptor" class="org.beans.LoggingInterceptor"
- abstract="false" singleton="true" lazy-init="default"
- autowire="default" dependency-check="default">
- </bean></beans>
根据如上配置如果将java 代码HelloController改成如下:
java 代码
- package org.beans;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.web.servlet.ModelAndView;
- import org.springframework.web.servlet.mvc.Controller;
- import org.springframework.web.servlet.view.RedirectView;
-
- public class HelloController implements Controller {
- private String viewPage;
- public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
- String user=request.getParameter("user");
- return new ModelAndView(new RedirectView(viewPage),"user",user);
- }
- public void setViewPage(String viewPage) {
- this.viewPage = viewPage;
- }
-
-
- }
则运行时出现错误。该怎么改呢?
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- hgq0011
- 等级:
- 性别:
- 文章: 961
- 积分: 1017
- 来自: 广州
|
或许“http://www.iteye.com/topic/24675”对你有帮助。
|
返回顶楼 |
|
|
- codeutil
- 等级:
- 文章: 666
- 积分: 752
|
return new ModelAndView("redirect:"+viewPage,"user",user);
|
返回顶楼 |
|
|
- 守望者
- 等级: 初级会员
- 文章: 28
- 积分: 62
- 来自: ...
|
主要有以下几种:
1、 return new ModelAndView(new RedirectView(this.getViewPage()));
2、return new ModelAndView("redirect:data.html");
3、Use the new redirect: syntax in my action-servlet.xml
<property name="successView"><value>redirect:/listAuctions.html</value></property>
|
返回顶楼 |
|
|