gpt4 book ai didi

java - Spring MVC POST 提交不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 06:09:40 25 4
gpt4 key购买 nike

我的应用程序服务器是 Weblogic 12C,它在 apache 代理后面工作。

当我通过 Spring MVC 表单进行 GET 提交时,值将到达后端,但是当我进行 POST 提交时,所有值在后端都为 null。

这是我的文件。

JSP 文件

    <form:form method="post" modelAttribute="userForm" action="createUser">

<form:input path="userInfo.number" readonly="true" maxlength="100"/>
<form:input path="userInfo.firstName" readonly="true" maxlength="500"/>
<form:input path="userInfo.lastName" readonly="true" maxlength="500"/>
<input type="submit" style="font-weight: bold;" value="Send"></td>
</form:form>

Controller

@Controller
@RequestMapping(value = "/")
@SessionAttributes("user")
public class TestController {

@RequestMapping(method = RequestMethod.POST, value="createUser")
public String createUser(@Valid UserForm userForm,BindingResult result,ModelMap modelMap) {
LOGGER.debug("Handling POST description" + userForm.getFirstName());
}
}

web.xml

<web-app>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>userFormServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-beans/servlet-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>userFormServlet</servlet-name>
<url-pattern>/apps/*</url-pattern>
</servlet-mapping>

最佳答案

从这行 userForm.getFirstName() 的外观来看,属性 firstNamelastNamenumber 似乎是 userForm 对象的属性,在这种情况下,您的表单应该是

<form:form method="post" modelAttribute="userForm" action="createUser">
<form:input path="number" readonly="true" maxlength="100"/>
<form:input path="firstName" readonly="true" maxlength="500"/>
<form:input path="lastName" readonly="true" maxlength="500"/>
<input type="submit" style="font-weight: bold;" value="Send"></td>
</form:form>

根据您的UserForm类的结构,可能略有不同,但您应该从中获得解决方案的要点

关于java - Spring MVC POST 提交不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28897115/

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