我正在开发一个Spring MVC项目,该项目使用hibernate和postgreSQL来保存数据。 我有一个JSP文件,它将操作调用到" / user / add"住在控制器里。但我得到的错误是servlet无法解决。我在项目结构中的库中添加了jar。我在这里发布控制器和JSP代码。请看看。
user.jsp
<c:url var="addAction" value="/user/add"> </c:url>
<form:form action="${addAction}" commandName="user">
<table>
<c:if test="${!empty user.first_Name}">
<tr>
<td>
<form:label path="id">
<spring:message text="UserID:"/>
</form:label>
</td>
<td>
<form:input path="id" readonly="true" size="8" disabled="true" />
<form:hidden path="id" />
</td>
</tr>
</c:if>
UserController中
@RequestMapping(value="/user/add",method = RequestMethod.POST)
public String addPerson(@ModelAttribute("user") User p){
if(p.getId() == 0){
this.userService.addUser(p);
} else {
this.userService.updateUser(p);
}
return "redirect:/users";
}
catalina.out中
Sep 17, 2014 11:44:55 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deploying web application archive /home/akshay/apache-tomcat/webapps/WirTauschen-1.0-SNAPSHOT.war
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Wed Sep 17 11:44:56 CEST 2014]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml]
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 96 ms
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Wed Sep 17 11:44:56 CEST 2014]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/users],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.WirTauschen.UserController.listUsers(org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/user/add],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.WirTauschen.UserController.addPerson(com.WirTauschen.model.User)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/edit/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.WirTauschen.UserController.editPerson(int,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/remove/{id}],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.WirTauschen.UserController.removeUser(int,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
INFO : org.hibernate.Version - HHH000412: Hibernate Core {4.3.6.Final}
INFO : org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
INFO : org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
INFO : org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
INFO : org.hibernate.engine.jdbc.internal.LobCreatorBuilder - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
INFO : org.hibernate.engine.transaction.internal.TransactionFactoryInitiator - HHH000399: Using default transaction strategy (direct JDBC transactions)
INFO : org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory - HHH000397: Using ASTQueryTranslatorFactory
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 914 ms
Sep 17, 2014 11:44:57 AM org.apache.catalina.startup.HostConfig deployWAR
INFO: Deployment of web application archive /home/akshay/apache-tomcat/webapps/WirTauschen-1.0-SNAPSHOT.war has finished in 1,761 ms
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/WirTauschen%2D1.0%2DSNAPSHOT/] in DispatcherServlet with name 'appServlet'
Hibernate: select user0_.id as id1_0_, user0_.email as email2_0_, user0_.first_Name as first_Na3_0_, user0_.last_name as last_nam4_0_, user0_.password as password5_0_ from wirtausch2 user0_
Hibernate: insert into wirtausch2 (email, first_Name, last_name, password) values (?, ?, ?, ?)
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: 0, SQLState: 23502
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - ERROR: null value in column "id" violates not-null constraint
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Closing WebApplicationContext for namespace 'appServlet-servlet': startup date [Wed Sep 17 11:44:56 CEST 2014]; parent: Root WebApplicationContext
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Closing Root WebApplicationContext: startup date [Wed Sep 17 11:44:56 CEST 2014]; root of context hierarchy
请告诉我。谢谢。
答案 0 :(得分:1)
编辑:我的错误,见下文
您可以两次添加上下文路径。
<c:url var="addAction" value="/user/add"> </c:url>
将完整路径(上下文路径+ servlet路径)放在addAction
中,并在 <form:form action=...>
中使用它,它还会添加上下文路径。
您应该使用:
<德尔> <form:form action="/user/add" commandName="user" method="POST">
德尔>
如果您使用链接中的路径 <c:url .../>
<a href="${addAction}"...>
我对我习惯的Velocity弹簧库感到困惑,并直接了解servlet上下文。在真正的JSP中,您必须使用<c:url ...>
来获取正确的操作URL。
我真的需要堆栈跟踪来帮助你:我可以做一些测试而问题不在我想的地方。