【问题标题】:Servlet cannot be resolvedServlet 无法解析
【发布时间】:2014-11-10 09:58:10
【问题描述】:

我正在开发一个 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>

用户控制器

 @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

请告诉我。谢谢。

【问题讨论】:

  • 我猜是你的URL映射问题,应该是value="user/add"
  • 当我使用“user/add”时,它说,它无法解析目录。
  • 您能给出确切的错误信息吗?或者更好的堆栈跟踪...

标签: java spring jsp spring-mvc


【解决方案1】:

编辑:我的错误,见下文

你添加了两次上下文路径。

&lt;c:url var="addAction" value="/user/add"&gt; &lt;/c:url&gt;将完整路径(上下文路径+servlet路径)放在addAction中,你在&lt;form:form action=...&gt;中使用,这也添加了上下文路径。

你应该改用:

&lt;form:form action="/user/add" commandName="user" method="POST"&gt;

如果使用链接中的路径&lt;a href="${addAction}"...&gt;,则只需使用&lt;c:url .../&gt;

我对我习惯的 Velocity 的 spring 库感到困惑,它直接知道 servlet 上下文。在真正的 JSP 中,您必须使用 &lt;c:url ...&gt; 来获取正确的操作 url。

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

【讨论】:

  • 你好 Serge,我猜你的方法有效,至少我没有得到任何错误。但是现在我有来自 Apache 的错误 404,因为找不到 /user/add。为什么 catalina 日志没有显示任何内容让我感到惊讶。
  • 请在公共 Dropbox 链接中找到 catalina.out 文件,如下所示:dl.dropboxusercontent.com/u/1994132/catalina.out
  • 很高兴你给了这一切,但它真的太多了,有来自不同运行的错误,与数据库相关的错误,到 Tomcat 引导等 :-( 我需要的只是提交表单时发生的错误...
  • 编辑了帖子。请看一看。没有任何东西保存在数据库中。这是主要问题。
  • 嗯,现在可以了,但是我仍然有无法解决的错误。
猜你喜欢
  • 1970-01-01
  • 2012-03-17
  • 2015-11-16
  • 2020-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多