添加“<form:input` spring =”“jsp

时间:2017-06-01 06:08:43

标签: jsp spring-mvc

=”“

在我的jsp页面中使用<form:input path="name" />时遇到错误500它显示HTTP状态500错误。

我还在index.jsp页面上添加了<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

的index.jsp

<%@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>
</head>
<body>
 <form:form method = "POST" action="hello/addCustomer" modelAttribute="mycustomer">
         <table>
            <tr>
               <td><form:label path = "name">Name</form:label></td>
               <td><form:input path = "name" /></td>
            </tr>
            <tr>
               <td colspan = "2">
                  <input type = "submit" value = "Submit"/>
               </td>
            </tr>
         </table>  
      </form:form>
</body>
</html>

TestController.java

@Controller
@RequestMapping("/hello")
public class TestController {

     @RequestMapping(value = "/index", method = RequestMethod.GET)
       public ModelAndView customer() {
          return new ModelAndView("index", "command", new Customer());
       }
    @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
    public String addCustomer(@ModelAttribute("mycustomer")Customer customer, 
                    ModelMap model) {
    model.addAttribute("name", customer.getUserName());
    return "details";
 }
}

Customer.java

public class Customer {

    String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

Details.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
    <center>
        <h2>Hello</h2>
        <h2>
             ${name}
        </h2>
    </center>
</body>
</html>

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zm</groupId>
  <artifactId>demo</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>Test Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
        <spring.version>4.0.1.RELEASE</spring.version>
      <jcl.slf4j.version>1.7.12</jcl.slf4j.version>
      <jstl.version>1.2</jstl.version>
      <servletapi.version>3.1.0</servletapi.version>
    </properties>
  <dependencies>
    <!-- Spring dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

      <!-- jstl -->
      <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
      </dependency>

      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${servletapi.version}</version>
          <scope>provided</scope>
      </dependency>
  </dependencies>
  <build>
    <finalName>demo</finalName>
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
</plugins>
  </build>
</project>

500错误 enter image description here

dispatche-servlet.xml中

enter image description here

的web.xml

enter image description here

TestController.java

enter image description here

1 个答案:

答案 0 :(得分:1)

错误在此行return new ModelAndView("index","command",new Customer());中。将command替换为mycustomer。如果必须使用command字样,请将modelAttribute="mycustomer"更改为modelAttribute="command"