错误:
引起: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" />
以下是文件的截图:
UserController中
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";
}
}
User.java
package Domain;
/ ** * * @author fiona * / 公共类用户{
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;
}
}
UserService.java 包服务;
import Domain.User;
public class UserService {
public void add(User user) {
//Persist the user object here.
System.out.println("User added successfully");
}
}
答案 0 :(得分:0)
这是因为您在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" />