【问题标题】:Class not found exception netbeans spring mvc类未找到异常netbeans spring mvc
【发布时间】:2017-04-04 03:05:54
【问题描述】:

错误:

原因:java.lang.ClassNotFoundException: WebApplication1.SourcePackages.Domain.UserService

这里是 dispatcher-servlet.xml 代码的一部分

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />


<bean id="UserService" class="WebApplication1.SourcePackages.Domain.UserService" />

<context:component-scan base-package="WebApplication1.SourcePackages.Controller" />

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

这里是文件的截图:

用户控制器

package Controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import Service.UserService;
import Domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/userRegistration.htm")
@SessionAttributes("user")
public class UserController {

private UserService userService;

@Autowired
public void setUserService(UserService userService) {
    this.userService = userService;
}

@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model)
{
    User user = new User();
    model.addAttribute(user);
    return "userForm";
}

@RequestMapping(method = RequestMethod.POST)
public String onSubmit(@ModelAttribute("user") User user) {
    userService.add(user);
    return "UserSuccess";
}

}

用户.java

package Domain;

/** * * @作者菲奥娜 */ 公共类用户{

private String name;
private String password;
private String gender;
private String country;
private String aboutYou;
private String[] community;
private Boolean mailingList;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getAboutYou() {
    return aboutYou;
}
public void setAboutYou(String aboutYou) {
    this.aboutYou = aboutYou;
}
public String[] getCommunity() {
    return community;
}
public void setCommunity(String[] community) {
    this.community = community;
}
public Boolean getMailingList() {
    return mailingList;
}
public void setMailingList(Boolean mailingList) {
    this.mailingList = mailingList;
}

}

用户服务.java 打包服务;

import Domain.User;

public class UserService {

public void add(User user) {
    //Persist the user object here. 
    System.out.println("User added successfully");

}

}

【问题讨论】:

    标签: java spring model-view-controller netbeans


    【解决方案1】:

    这是因为您在 bean 的类名和组件扫描的包名中包含了文件夹结构“WebApplication1.SourcePackages”。 它不是包名。 “WebApplication1”是 NetBeans 项目的名称。 “SourcePackages”是包含项目所有 Java 包和类的结构的名称。

    在您的 Spring 配置文件中,您只需提供完全限定的类名,即类的包名,后跟类名。

    下面提供的示例

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    
    <bean id="userService" class="Service.UserService" />
    
    <context:component-scan base-package="Controller" />
    
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
    

    【讨论】:

    • 谢谢。但它仍然没有解决我的问题,它仍然显示类未找到错误,原因如下:java.lang.ClassNotFoundException: Service.UserService.
    • @FionaHuang 您如何部署和执行 Web 应用程序?您是在 Net Beans 中运行它,还是将其打包到一个战争中,然后将其部署到外部 Web 容器中?
    • 我在 netbeans 中运行它,我可以发布更多我拥有的代码
    • 我基本按照这个的源码:dzone.com/tutorials/java/spring/…
    • 我已经更新了我的答案,请检查。具体来说,我已将 UserService 的 id 设置为驼峰式。另外,请提供 UserService 和 UserController 的代码。
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2012-02-28
    • 2016-12-03
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多