Jsp 添加`
当我在jsp页面中使用时,它显示HTTP状态500错误 我还添加了index.jsp页面的顶部 index.jspJsp 添加`,jsp,spring-mvc,Jsp,Spring Mvc,当我在jsp页面中使用时,它显示HTTP状态500错误 我还添加了index.jsp页面的顶部 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"> <
<%@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>
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错误
dispatch-servlet.xml
web.xml
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";
}
}
错误在此行中,返回新模型和视图索引,命令,新客户。用mycustomer替换命令。如果有必要使用单词command,则将modelAttribute=mycustomer更改为modelAttribute=command。这是在提交表单时还是在加载页面时发生的?页面加载时出错。错误页面底部还显示了java.lang.IllegalStateException:bean名称“mycustomer”的BindingResult和普通目标对象都不能作为请求attributery使用,path=userName,您的客户属性名称。我已删除该代码,现在我已addCustomer@ModelAttributemycustomer但是当我尝试向添加模型属性时,您更改了returnnewmodelandviewindex,command,newcustomer;要返回新模型和视图索引、mycustomer、new Customer;?是的。但同样的错误。它在index.jsp页面中以红色显示modelAttribute=mycustomer。我想我不能使用imported,因为您的pom.xml文件包含org.springframework spring webmvc${spring.version}这个依赖性??因为这个依赖项包含标签库,所以我用你的代码创建了这个应用程序。工作正常。只需要更改path=name。我将其更改为path=username,这是实体Customer下的属性