gpt4 book ai didi

java - Spring MVC 在 Dispatcher Servlet 中找不到映射

转载 作者:行者123 更新时间:2023-11-30 04:57:21 25 4
gpt4 key购买 nike

我刚刚开始学习Spring mvc。我浏览了很多关于同一问题的类似问题,但仍然无法解决这个错误。有人可以查看我的代码并让我知道缺少什么吗?

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>myphotosharingapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>myphotosharingapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/myphotosharingapp-service.xml</param-value>
</context-param>

<welcome-file-list>
<welcome-file>
jsp/index.jsp
</welcome-file>
</welcome-file-list>
</web-app>

myphotosharingapp-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- mapping -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="browsefiles.htm">browseFilesController</prop>
</props>
</property>
</bean>

<!-- The view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<bean id="browseFilesController" class="springmvc.controller.BrowseFilesController">
<property name="browseAlbumsService" ref="browseAlbumsService"></property>
<property name="methodNameResolver">
<bean
class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/browsefiles.htm">browse</prop>
</props>
</property>
</bean>
</property>
</bean>

</beans>

myphotosharingapp-service.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="browseAlbumsService" class="springmvc.service.BrowseAlbumsService">
</bean>
</beans>

home.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Photos</title>
</head>
<%-- <%
String sourcePath = ("${sourceAlbumPath}" == null)? "" : "${sourceAlbumPath}";
%> --%>

<body>
<h1>
Welcome
</h1>
<form name="frmHome" method="Post" action="browsefiles.htm">
<a href=".">View Albums</a>
<br>
<br>
<input type="text" name="sourceAlbumPath" value="">
</input>
<button name="Browse" >
Browse
</button>
</form>
</body>
</html>

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Share Photos</title>
</head>
<body>
<h1>
Welcome!!
<a href="jsp/home.jsp">home</a>
</h1>

</body>
</html>

索引页面加载良好,主页也加载良好。但是,单击 home.jsp 上的“浏览”按钮时,我只收到“HTTP STATUS 404”错误。

Tomcat 说“Dispatcher Servlet 中没有名为 myphotosharingapp 的 [/springmvc/jsp/browsefiles.htm] 映射”

有人可以帮忙吗?

最佳答案

主页中的路径是相对路径,指向/jsp/browsefiles.htm,而xml中的映射是/browsefiles.htm。

解决方案是使用 request.getContextPath() 使 URL 相对于基址在 JSP 或 <spring:url> 中Spring MVC的JSP标签。

关于java - Spring MVC 在 Dispatcher Servlet 中找不到映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8109670/

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