java - Servlet 无法解析

标签 java spring jsp spring-mvc

我正在开发一个 Spring MVC 项目,该项目使用 hibernate 和 postgreSQL 来保存数据。 我有一个 JSP 文件,它调用驻留在 Controller 中的“/user/add”操作。但我得到的错误是 servlet 无法解析。我已将 jar 添加到项目结构的库中。我在这里发布 Controller 和 JSP 代码。请看一下。

用户.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>

用户 Controller

 @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";
    }

卡特琳娜.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

请告诉我。谢谢。

最佳答案

编辑:我的错误,见下文

您添加上下文路径两次。

<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 的 spring 库感到困惑,它直接了解 servlet 上下文。在真正的 JSP 中,您必须使用 <c:url ...>获取正确的操作 URL。

我真的需要堆栈跟踪来帮助你:我可以做一些测试,问题不在我的想象中。

关于java - Servlet 无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25872369/

相关文章:

java - 如何在java7中为特定用户设置文件访问属性

java - 使用 selenium webdriver 计算网格中的行数

java - 我可以在 AnyLogic 拆分组件中将单个代理拆分为多个代理吗?

java - Spring AOP 切入点表达式排除特定的返回类型或参数

java - 如何配置 Spring Boot 以使用两个数据库?

java - 如何让JSP页面扩展抽象的HttpServlet

Java MySQL IndexOutOfBounds 错误

java - 如何关闭 ExecutorService?

Springquartz调度程序记住运行间信息

java - Spring Boot Jsp 不适用于版本 2.0.2.RELEASE