【发布时间】:2017-04-28 14:12:43
【问题描述】:
我有问题。 我是 Spring MVC 的新手。我遵循 Spring MVC 4+Hibernate CRUD 的示例。
但我无法在 spring mvc 上运行映射。
我使用 intellij idea 和 tomcat 服务器。
我将分享我的代码。我错过了什么或做错了什么,但我看不到。
这是我的 AppConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "java.*")
public class AppConfig {
@Bean
public ViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public MessageSource messageSource(){
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
}
这是我的 AppInitializer.java :
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
这是我的 EmployeeController.java :
@Controller
@RequestMapping("/")
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@Autowired
MessageSource messageSource;
@RequestMapping(value = {"/","/list"},method = RequestMethod.GET)
public String listEmployees(Model model){
List<Employee> employees = employeeService.findAllEmployees();
model.addAttribute("allEmployees",employees);
return "allemployees";
}
}
另外这个类我在WEB-INF/views和Employee模型下有allemployees.jsp视图页面,service和dao类和hibernate配置类。
这一切看起来都很好。当我在 ide 上运行项目并输入此 URL http:/localhost:8080/ouremployee 时,我看到 index.jsp“Hello World”页面。之后我输入 http://localhost:8080/ouremployee/list 我应该看到 allemployees.jsp 但我收到了这个错误:
WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/ouremployee/list] in DispatcherServlet with name 'dispatcher'
我哪里错了?你能帮帮我吗?
【问题讨论】:
-
你能试着把
@RequestMapping(value = {"/","/list"},method = RequestMethod.GET)改成@RequestMapping(value = {"/","/list","/allemployees"},method = RequestMethod.GET)吗? -
对不起,我写错了。我已经尝试将我的请求设为 http:/localhost:8080/ouremployee/list。我会修复条目。
标签: spring-mvc