gpt4 book ai didi

java - 在/上找不到资源上的 SpringMVC Controller

转载 作者:行者123 更新时间:2023-11-28 23:55:41 25 4
gpt4 key购买 nike

我正在跟进来自这个 url 的教程

https://aishwaryavaishno.wordpress.com/2013/07/12/spring-mvchibernate-annotation-based-mysql-maven-json-simple-example/

根据教程,我在我的 Controller 中有这个

@Autowired
private IMusicStoreService musicStoreService;
@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException{

return new ModelAndView("home","musicDetForm",new MusicDetails());
}
@RequestMapping(value="AddSong",method = RequestMethod.POST)
@ResponseBody
public String addSong(@ModelAttribute("musicDetForm") MusicDetails musicDetails){
return musicStoreService.addSong(musicDetails);
}
@RequestMapping(value = "SongList/",method = RequestMethod.GET)
@ResponseBody
public List<MusicDetails> getSongList(){
return musicStoreService.getSongList();
}

我已将我的 DispatcherServlet 和 contextConfigLocation 映射到我的 web.xml 文件

<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>SpringDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<session-config>
<session-timeout>30</session-timeout>
</session-config>

假设这是我来自 Controller 映射的默认 url

@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException{

return new ModelAndView("home","musicDetForm",new MusicDetails());
}

现在当我启动我的应用程序到这个 url

http://localhost:8080/MusicStore/

我的项目结构截图

enter image description here

作为我的 servlet 引擎的 Tomcat 返回 HTTP 状态错误 404。

请问哪里出了问题?

Error Notification

最佳答案

在mvc-dispatcher-servlet.xml中加入以下代码来配置viewResolver

    <bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

然后在/WEB-INF/views/中添加你的jsp文件

关于java - 在/上找不到资源上的 SpringMVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36914010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com