【问题标题】:No mapping found for HTTP request with URI Spring MVC 4没有为带有 URI Spring MVC 4 的 HTTP 请求找到映射
【发布时间】: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


【解决方案1】:

您可以尝试使用 DispatcherConfig 作为类名吗?让我知道它是否有效。

【讨论】:

  • 好的,我会尝试,我会简要介绍一下结果。但根据我的研究,Spring MVC 配置类名并不重要,但我不确定我是否会尝试它是否有效。
  • 我认为您不能使用值为“java.*”的 ComponentScan 配置,而是使用与您的控制器类包密切相关的包名称。并且不要在上面使用 *。
  • 我在 30 分钟前解决了。我将解释为答案。感谢您的关注。
【解决方案2】:

为什么你的网址中有“ouremployee”? 这是您在运行配置中的应用基础名称吗?

我会尝试 http:/localhost:8080/ 或 http:/localhost:8080/list urls 看看这是否会给我带来任何不同的结果。

【讨论】:

  • 是的,它是运行/调试配置中的基本名称。这有关系吗?如果它可以作为 localhost:8080/list 它将作为 localhost:8080/ouremployee/list 工作吗?好的,我会尝试,我会向您简要介绍结果。
【解决方案3】:

我解决了我的问题。首先,我要感谢您对我的问题的关注。

我在 Intellij IDEA 上的项目结构是这样的

-src -主要的 -java -com -样本 . . -资源 . . -webapp .

我标记为 src 的源根目录。之后我尝试将参数输入到@ComponentScan "java.com.sample" Intellij 不接受,我以 java.* 的形式输入,但我的映射不起作用。

我尝试将其标记为 java 的源目录,并将 @ComponentScan 输入为“com.sample” Intellij 接受这一点。完成此操作后,我运行我的项目并运行弹簧映射。

我想说;我的问题原因是由于我错误的 prokect 结构定义,spring mvc 找不到扫描包。我解决了这个问题,映射工作正常。

谢谢你们。

【讨论】:

    猜你喜欢
    • 2011-12-08
    • 1970-01-01
    • 2013-07-27
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多