gpt4 book ai didi

java - Spring : ClassNotFound exception

转载 作者:行者123 更新时间:2023-12-01 09:45:39 31 4
gpt4 key购买 nike

我正在 spring mvc 中启动一个新项目,当我尝试运行 hello world 时,它抛出错误:

这是我的dispatcher-servlet.xml 映射:

     <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean name="/welcome.html"
class="com.shaurya.FirstController" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

这是 web.xml :

       <web-app id="WebApp_ID" version="2.4" 
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>

Controller 代码在这里:

    package com.shaurya;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController

public class FirstController extends AbstractController{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String message="welcome to Spring";
ModelAndView model= new ModelAndView("welcome");
model.addObject("message", message);
return model;
}
}

这是 Controller 渲染welcome.jsp的 View 页面:

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="welcome.html">sdsd</a>
</body>
</html>

为什么会出现错误,看起来像这样:

    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find       class [com.shaurya.FirstController] for bean with name'/welcome.html' enter code heredefined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.shaurya.FirstController.

有什么建议吗?这里缺少什么?

最佳答案

您可以尝试更改此代码吗

<bean name="/welcome.html" 
class="com.shaurya.FirstController" />

到此

<bean name="/welcome" 
class="com.shaurya.FirstController" />

由于 .html 的映射已经在 web.xml 中完成,您可能不需要在 bean 映射中再次提及它

关于java - Spring : ClassNotFound exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38069135/

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