Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 找不到URI为Spring MVC 4的HTTP请求的映射_Spring Mvc - Fatal编程技术网

Spring mvc 找不到URI为Spring MVC 4的HTTP请求的映射

Spring mvc 找不到URI为Spring MVC 4的HTTP请求的映射,spring-mvc,Spring Mvc,我有个问题。 我是春季MVC的新手。我以SpringMVC4+HibernateCrud为例 但我无法在SpringMVC上运行映射 我使用intellij idea和tomcat服务器 我将分享我的代码。我错过了什么,或者我做错了什么,但我看不见 这是我的AppConfig.java @Configuration @EnableWebMvc @ComponentScan(basePackages = "java.*") public class AppConfig { @Bean publi

我有个问题。 我是春季MVC的新手。我以SpringMVC4+HibernateCrud为例

但我无法在SpringMVC上运行映射

我使用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";
}
}

我错在哪里?您能帮我吗?

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

为什么url中有“我们的员工”? 这是运行配置中的应用程序基名称吗


我会尝试http:/localhost:8080/或http:/localhost:8080/列出URL,看看是否会得到不同的结果。

我解决了我的问题。首先我要感谢你对我的问题感兴趣

我在Intellij IDEA上的项目结构如下

-src -主要 -爪哇 -com -样品 . . -资源 . . -网络应用

我将其标记为src的源根目录。之后,我尝试将参数输入到@ComponentScan“java.com.sample”Intellij中,但Intellij不接受这一点,因此我以java.*为中心,并且我的映射不起作用

我尝试将其标记为java的源目录,并将@ComponentScan的条目标记为“com.sample”Intellij接受该项。完成此操作后,我运行我的项目,并运行spring映射

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


谢谢大家。

您能尝试更改
@RequestMapping(value={”/“,“/list”},method=RequestMethod.GET)
中的
@RequestMapping(value={”/“,“/list”,“/allemployees”},method=RequestMethod.GET)
?哦,对不起,我写错了。我已经尝试以http://localhost:8080/ouremployee/list的身份请求了。我将修复条目。是的,它是运行/调试配置中的基本名称。这有关系吗?如果它可以作为localhost:8080/list工作,那么它可以作为localhost:8080/ouremployee/list工作吗?好的,我会尝试并向您简要介绍结果。好的,我会尝试并简要介绍结果。但根据我的研究,Spring MVC配置类名并不重要,但我不确定我是否会尝试,也许它会起作用。我认为您不能使用值为“java.*”的ComponentScan配置,而是使用与您的控制器类包密切相关的包名。不要在上面用*我30分钟前就解决了。我来解释一下。谢谢你的关注。
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'