Spring MVC 4.1 RedirectAttributes无法正常工作

时间:2015-05-22 14:58:46

标签: java spring spring-mvc

我是Spring MVC的新手。我正在使用Spring版本4.1.6并在tomcat 7上为开发环境部署了我的两个Web应用程序A和B.但是在实际的生产环境中,应用程序A将部署在weblogic上,而应用程序B将部署在websphere上。以下是开发环境中出现的情况。

应用程序A在test目录中有testrequest.jsp页面。下面是jsp页面的代码。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Media Request</title>
</head>
<body>
<h2>Fill your form123!</h2>
<form:form method="post" commandName="testobj" action="http://localhost:8080/b/createtestrequest.test">
    <table>
        <tr>
            <td>Enter your name:</td>
            <td><form:input  path="requestId" /></td>
            <td><form:errors path="requestId" cssStyle="color: #ff0000;"/></td>
        </tr>
        <tr>
             <td><input type="submit" name="submit" value="Submit"></td>
        </tr>
    </table>
</form:form>
</body>
</html>

如果我们仔细查看表单的action属性,当表单提交请求时必须转到TestController.java。 TestController.java有处理 GET (加载页面)和 POST (提交页面)请求的方法.Below是相同的代码。

@Controller
public class TestController {
@RequestMapping(value="/createtestrequest.test",method = RequestMethod.GET)
    public String  requestForm(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest,RedirectAttributes redirectAttrs){
        System.out.println("*********** Get Request reaches the TestController *******************");

        RequestDetails obj=new RequestDetails();
        obj.setRequestId("12345");
        redirectAttrs.addAttribute("testobj", obj);

        return "redirect:http://localhost:8080/a/test/testrequest.jsp";
    }

    @RequestMapping(value="/createtestrequest.test",method = RequestMethod.POST)
    public void submitForm(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest,RedirectAttributes redirectAttrs){
                    System.out.println("*********** Post Request reaches the TestController *******************");


    }

}

以下是应用程序B中可用的RequestDetails(Model)对象。

public class RequestDetails implements java.io.Serializable{

    String requestId;        

    public String getRequestId() {
        return requestId;
    }
    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }
}

当我执行URL以显示jsp页面 http://localhost:8080/b/createtestrequest.test (在请求中设置空模型对象)时,控制器方法**(requestForm(HttpServletResponse httpServletResponse,HttpServletRequest) httpServletRequest,RedirectAttributes redirectAttrs)**)处理get请求会被以下输出调用,但它会重定向到应用程序A的测试目录中可用的页面testrequest.jsp。但是它在浏览器上给出了以下错误< / p>

在tomcat控制台上输出

***********获取请求到达TestController *******************

以下是浏览器上的错误

org.springframework.beans.ConversionNotSupportedException: Failed to convert value of type 'com.test.bean.RequestDetails' to required type 'java.lang.String'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.test.bean.RequestDetails] to required type [java.lang.String]: no matching editors or conversion strategy found
    org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:74)
    org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:40)
    org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:596)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.formatValue(RedirectAttributesModelMap.java:79)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:71)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:34)
    com.cira.raws.mediawf.api.services.controller.MediaWFController.requestForm(MediaWFController.java:87)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
    org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.IllegalStateException: Cannot convert value of type [com.test.bean.RequestDetails] to required type [java.lang.String]: no matching editors or conversion strategy found
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:287)
    org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:107)
    org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
    org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:40)
    org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:596)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.formatValue(RedirectAttributesModelMap.java:79)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:71)
    org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap.addAttribute(RedirectAttributesModelMap.java:34)
    com.cira.raws.mediawf.api.services.controller.MediaWFController.requestForm(MediaWFController.java:87)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)

我不知何故需要在我的请求对象中使用Userdefined对象“testobj”来加载test.jsp页面,其中包含对象中的可用值,但它似乎没有按预期工作。我有两个问题

  1. 在使用其他解决方法重定向请求的情况下,是否可以使用RedirectAttribute类使用户定义的对象可用?
  2. 将来我将使用Spring Validation支持来验证jsp表单,在这种情况下,我需要将org.springframework.validation.BindingResult对象提供给应用程序A中的jsp页面,那么这也是可能的吗? / LI>

1 个答案:

答案 0 :(得分:4)

要添加用户定义的对象,我相信您需要使用addFlashAttribute。

Example