gpt4 book ai didi

java - 在 web.xml 中未找到带有 URI 的 HTTP 请求的映射

转载 作者:行者123 更新时间:2023-11-30 07:05:50 25 4
gpt4 key购买 nike

在我使用 Spring MVC 和 JSON 的项目中,我将 web.xml 文件配置如下:

Web.xml

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

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

<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

我的 Controller :

@Controller
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
return "home";
}
@RequestMapping(value = "/json", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody List<Person> personsReturn(@RequestParam("name") String name, @RequestParam("age") int age) {
Person p1 = new Person("dat1", 11);
Person p2 = new Person("dat2", 22);

Person p3 = new Person("dat3", 33);
Person p4 = new Person("dat4", 44);

List<Person> lists = new ArrayList<Person>();

if (name.equals("dat")) {
lists.add(p1);
lists.add(p2);
} else {
lists.add(p3);
lists.add(p4);
}
return lists;
}
}

我的 Servlet-Context.xml:

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

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan base-package="com.luongkhanh.restfulspringmvc" />

</beans:beans>

我可以通过 URL 从 JSON 获取数据:

http://localhost:9999/restfulspringmvc/json?name=dat1&age=11

结果 JSON:

[{"name":"dat3","age":33},{"name":"dat4","age":44}]

但是如果我想通过位置的URL获取图像,则不成功:

http://localhost:9999/restfulspringmvc/images/avatar.jpg

Eclipse 中的记录器警告异常:

ARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/restfulspringmvc/images/avatar.jpg] in DispatcherServlet with name 'appServlet'

如何配置通过浏览器从 URL 获取图像?非常感谢!!

最佳答案

您已将 DispatcherServlet 配置为使用 url-pattern/这会覆盖容器的默认 servlet 映射。

默认 servlet 映射用于加载任何资源或请求,而无需在 web.xml 中进行显式映射。通常静态资源是使用它来加载的。如果您通过将其指定为 web.xml 中 servlet 的模式来覆盖它,那么您必须自己负责加载资源

Spring MVC 通过提供资源处理来提供帮助您必须指定与您的静态资源 url 模式和资源物理驻留位置相匹配的映射

您确实指定了

<resources mapping="/resources/**" location="/resources/" />

但是您的图像并不驻留在 webapp Maven 文件夹下的资源文件夹中。相反,它位于图像文件夹中。因此,您必须添加另一个这样的资源标签

<resources mapping="/images/**" location="/images/" />

NB 映射属性和位置属性不必对应。例如,您可以有一个像

这样的网址
http://localhost:9999/restfulspringmvc/images/avatar.jpg

并且您的图像位于 webapp 下的/static-resources/images 中,您的映射将是

<resources mapping="/images/**" location="/static-resources/images/" />

您还可以在同一应用程序上下文中拥有多个资源规范

关于java - 在 web.xml 中未找到带有 URI 的 HTTP 请求的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40150956/

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