gpt4 book ai didi

java - 如何修复找不到 Spring DispatcherServlet 类?

转载 作者:行者123 更新时间:2023-11-28 23:26:47 25 4
gpt4 key购买 nike

我开始学习 Spring,所以我尝试使用 Spring MVC 创建一些简单的应用程序。看了很多命题怎么修复这个错误,但是还是没有解决。请问如果有人知道什么,你能帮忙吗?因为我已经花了很多时间,仍然不知道如何解决它。
所以在索引页上我创建了下一个链接:

 <a href="${pageContext.servletContext.contextPath}/sign/up">Sign up</a>

必须将我引向注册页面。我有 hibernate.cfg.xml 文件,我用它在 spring-context.xml 中配置 LocalSessionFactoryBean。一般 spring-context.xml 看起来像:

<beans 
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocations">
<value>classpath:resources/hibernate.cfg.xml</value>
</property>
</bean>

<bean class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<context:annotation-config/>
<context:component-scan base-package="milkiv.easyword"/>
<tx:annotation-driven/>
</beans>

web.xml 喜欢:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/resources/spring-context.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/resources/spring-context.xml</param-value>
</context-param>
</web-app>

我的 Controller 看起来像:

@RequestMapping("/sign/up")
@Controller
public class Up {

@RequestMapping(method = RequestMethod.GET)
public String showForm(ModelMap map) {
return "sign/up";
}

@RequestMapping(method = RequestMethod.POST)
public String signUp(@ModelAttribute User user) {
if (user != null) {
user.setRegistrationDate(new Date(new java.util.Date().getTime()));
ApplicationContext context = new ClassPathXmlApplicationContext("resources/spring-context.xml");
Storages storages = context.getBean(Storages.class);
storages.uSM.create(user);
}
return "sign/up";
}
}

所以我有下一个问题。当我按下链接时,它会向我推荐 EasyWordWeb/sign/up 并给出下一个异常:

type Exception report

message Error instantiating servlet class >org.springframework.web.servlet.DispatcherServlet

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class >org.springframework.web.servlet.DispatcherServlet
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
root cause

java.lang.ClassNotFoundException: >org.springframework.web.servlet.DispatcherServlet
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)

P.S.DispatcherServlet实际所在的jar文件(spring-webmvc-4.2.0.RELEASE.jar)在WEB-INF/lib/目录下。

最佳答案

请在删除 web.xml 文件中多余的“>”后尝试。这可能是导致它的原因。

<servlet-class>>org.springframework.web.servlet.DispatcherServlet</servlet-class>

关于java - 如何修复找不到 Spring DispatcherServlet 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36074184/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com