gpt4 book ai didi

java - Spring MVC 4.1 RedirectAttributes 无法正常工作

转载 作者:行者123 更新时间:2023-11-29 03:09:08 26 4
gpt4 key购买 nike

我是 Spring MVC 的新手。我正在使用 Spring 版本 4.1.6 并将我的两个 Web 应用程序 A 和 B 部署在用于开发环境的 tomcat 7 上。但在实际生产环境中,应用A会部署在weblogic上,应用B会部署在websphere上。以下是开发环境中发生的场景。

应用程序 A 在测试目录中有可用的 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 (在请求中设置空模型对象)然后 Controller 方法**(requestForm(HttpServletResponse httpServletResponse,HttpServletRequest httpServletRequest,RedirectAttributes redirectAttrs)**)处理get请求确实被调用了下面的输出但它确实重定向到应用程序 A 的测试目录中可用的页面 testrequest.jsp。但它在浏览器上给出以下错误

tomcat控制台输出

************ Get Request 到达 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)

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

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

最佳答案

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

Example

关于java - Spring MVC 4.1 RedirectAttributes 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30400002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com