【问题标题】:Spring MVC, Form Submission not workingSpring MVC,表单提交不起作用
【发布时间】:2014-03-30 07:06:42
【问题描述】:

有人可以帮我解决这个问题吗?

我的控制器类看起来像这样,我已经创建了客户模型类..

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView student() {
        return new ModelAndView("customer", "command", new Customer());
    }

    @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
       public String addStudent(@ModelAttribute Customer customer, 
       ModelMap model) {

        model.addAttribute("customerName", customer.getCustomerName());
        model.addAttribute("emailId", customer.getEmailId());
        model.addAttribute("sex", customer.getSex());

        return "customerDetails";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Customer Form Handling</display-name>


    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Processes application requests -->
    <servlet>
        <servlet-name>Customer</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/Customer/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Customer</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

JSP 页面

customer.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Customer Form Handling</title>
</head>
<body>

<h2>Customer Information</h2>
<form:form method="POST" commandName = "command" action="/addCustomer">
   <table>
    <tr>
        <td><form:label path="customerName">customerName</form:label></td>
        <td><form:input path="customerName" /></td>
    </tr>
    <tr>
        <td><form:label path="emailId">emailId</form:label></td>
        <td><form:input path="emailId" /></td>
    </tr>
    <tr>
        <td><form:label path="sex">sex</form:label></td>
        <td><form:input path="sex" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

customerDetails.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Customer Form Handling</title>
</head>
<body>

<h2>Customer Detail Information</h2>
   <table>
    <tr>
        <td>CustomerName</td>
        <td>${customerName}</td>
    </tr>
    <tr>
        <td>emailId</td>
        <td>${emailId}</td>
    </tr>
    <tr>
        <td>sex</td>
        <td>${sex}</td>
    </tr>
</table>  
</body>
</html>

Servlet-Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <!-- <resources mapping="/resources/**" location="/resources/" /> -->

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.customerinfo.controller" />

</beans:beans>

但是当我在 Tomcat 服务器中运行这个应用程序时。第一个 url 指向 localhost:8080/controller/.

如果我附加 localhost:8080/controller/customer 我会得到第一个表单页面..

但是一旦我点击提交..它说页面未找到错误。

【问题讨论】:

  • 是目录/WEB-INF/views 中的customerDetails 视图。或者有没有像客户这样的目录?
  • 它在WEB-INF/views里面

标签: spring spring-mvc


【解决方案1】:

这是一个相对路径问题。您的表单操作是/addCustomer(具有/ 前缀),如果您解决它是http://localhost:8080/addCustomer。你想要的大概是http://localhost:8080/appname/customer/addCustomer

在某些情况下,只需将其更改为 customer/addCustomer 即可解决问题,但如果您的页面也可以通过 http://localhost:8080/appname/customer/ 访问(注意尾部斜杠),那么这可能是个问题。相对路径将转换为http://localhost:8080/appname/customer/customer/addCustomer

当然,现在您可以考虑只执行/appname/customer/addCustomer 并解决问题,但实际上您现在正在硬编码上下文路径名称。如果有一天上下文路径发生变化,所有这些代码都会中断。

我喜欢使用的一种方法是通过定义一个根变量来让我的 JSP 找出上下文路径

<c:set var="root" value="${pageContext.request.contextPath}"/>
...
<form:form action="${root}/customer/addCustomer">

【讨论】:

  • 您好,感谢您的回复。
  • 您好,感谢您的回复。当我将操作更改为 customer/addcustomer 时,它解决了问题。我无法理解这些......当我在 tomcat 服务器上运行应用程序时。我的意思是我从 Eclipse 运行应用程序。所以我右键单击该项目并在服务器上运行。初始页面被加载为localhost:8080/controller,它说页面未加载。然后,如果我将 Url 更改为 localhost:8080/controller/customer 我打算工作的第一页,我会得到表单页面。如何纠正它。并且您应该将该操作称为客户/添加客户。
  • 因为 /customer 是您的控制器映射到的路径,而 /addcustomer 是处理程序方法映射到的路径
【解决方案2】:

试一试

<form:form method="POST" commandName = "command" action="addCustomer">

而不是

<form:form method="POST" commandName = "command" action="/addCustomer">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多