Beast
Beast

Reputation: 629

Spring MVC 4.1 RedirectAttributes is not working properly

I am new to the Spring MVC. I am using the Spring release 4.1.6 and deployed my two web applications A and B on tomcat 7 for the development environment. But in the actual production environment the application A will be deployed on weblogic and application B will be deployed on websphere. Below is the scenario occuring on the development environment.

Application A has testrequest.jsp page available in the test directory.Below is the code for the jsp page.

<%@ 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>

If we look at the action attribute of the form carefully , when the form submits the request must go to the TestController.java. TestController.java has methods to handler the GET(load the page) and POST(submit the page) request.Below is the code for the same.

@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 *******************");


    }

}

Below is the RequestDetails(Model) object available in the application B.

public class RequestDetails implements java.io.Serializable{

    String requestId;        

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

When I am executing the URL to display the jsp page http://localhost:8080/b/createtestrequest.test (to set the empty model object in the request) then the controller method**(requestForm(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest,RedirectAttributes redirectAttrs)**) to handle get request does gets invoked with the below output but it does redirect to the page testrequest.jsp available in the test directory of the Application A. But it is giving the below error on the browser

output on the tomcat console

*********** Get Request reaches the TestController *******************

Below is the error coming on the browser

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)

I somehow need the Userdefined object "testobj"available in my request object to load the test.jsp page with the available values in the object but it seems to be not working as expected.I have two questions

  1. Is it possible to make the user defined object available using the RedirectAttribute class in the case of redirecting the request using some other workaround?
  2. In future I will be validating the jsp form using the Spring Validation support in that case I need to made available org.springframework.validation.BindingResult object to my jsp page in the application A so will that be possible as well?

Upvotes: 1

Views: 6095

Answers (1)

user3018861
user3018861

Reputation: 63

To add a user defined object, i believe you need to use addFlashAttribute.

Example

Upvotes: 5

Related Questions