繁体   English   中英

Spring MVC 4.1 RedirectAttributes无法正常工作

[英]Spring MVC 4.1 RedirectAttributes is not working properly

我是Spring MVC的新手。 我正在使用Spring版本4.1.6,并将两个Web应用程序A和B部署在tomcat 7上以用于开发环境。 但是在实际生产环境中,应用程序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 (提交页面)请求的方法,下面是相同的代码。

@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,用于处理get请求的RedirectAttributes redirectAttrs)**)确实会被以下输出调用,但它的确重定向到了Application A的test目录中可用的页面testrequest.jsp。但是,它在浏览器中给出了以下错误

在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验证支持来验证jsp表单,在这种情况下,我需要使org.springframework.validation.BindingResult对象在应用程序A的jsp页面中可用,这样是否也可能?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM