In my project using Spring MVC and JSON, I configure my web.xml file following as:
在我使用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>
My 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;
}
}
My 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>
I can get data from JSON by URL:
我可以通过URL从JSON获取数据:
http://localhost:9999/restfulspringmvc/json?name=dat1&age=11
Result JSON:
[{"name":"dat3","age":33},{"name":"dat4","age":44}]
But If I want to get the image by URL at location, it's unsuccessful:
但是,如果我想通过URL在位置获取图像,则不成功:
http://localhost:9999/restfulspringmvc/images/avatar.jpg
Logger in eclipse alert an exception:
记录器在eclipse中警告异常:
ARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/restfulspringmvc/images/avatar.jpg] in DispatcherServlet with name 'appServlet'
How to configure to get image from URL by Browser? Thank you so much !!
如何配置以通过浏览器从URL获取图像?非常感谢 !!
1 个解决方案
#1
1
You have configured your DispatcherServlet to use the url-pattern / This overrides the default servlet mapping of the container.
您已将DispatcherServlet配置为使用url-pattern /这将覆盖容器的默认servlet映射。
The default servlet mapping is used to load any resources or requests without an explicit mapping in web.xml. Typically static resources are loaded using this. If you override it by specifying it as a pattern for your servlet in web.xml then you have to take care of loading the resources yourself
默认的servlet映射用于加载任何资源或请求,而无需在web.xml中进行显式映射。通常使用此加载静态资源。如果通过在web.xml中将其指定为servlet的模式来覆盖它,那么您必须自己负责加载资源
Spring MVC helps by providing resource handling via You have to specify the mapping which matches your static resources url pattern and the location where your resources reside physically
Spring MVC通过提供资源处理来帮助您必须指定与您的静态资源URL模式匹配的映射以及资源实际驻留的位置
You did specify
你确实指定了
<resources mapping="/resources/**" location="/resources/" />
but your image does not reside in a resources folder under webapp Maven folder. Instead its in images folder. Hence you have to add another resources tag like this
但您的图像不驻留在webapp Maven文件夹下的资源文件夹中。而是在图像文件夹中。因此,您必须添加这样的另一个资源标记
<resources mapping="/images/**" location="/images/" />
NB The mapping attribute and the location attribute does not have to correspond. For example you can have a url like
NB映射属性和位置属性不必对应。例如,你可以有一个网址
http://localhost:9999/restfulspringmvc/images/avatar.jpg
and your images are in /static-resources/images under webapp the your mapping will be
并且您的映像将位于webapp下的/ static-resources / images中
<resources mapping="/images/**" location="/static-resources/images/" />
You can also have multiple resources specification in the same application context
您还可以在同一应用程序上下文中具有多个资源规范
#1
1
You have configured your DispatcherServlet to use the url-pattern / This overrides the default servlet mapping of the container.
您已将DispatcherServlet配置为使用url-pattern /这将覆盖容器的默认servlet映射。
The default servlet mapping is used to load any resources or requests without an explicit mapping in web.xml. Typically static resources are loaded using this. If you override it by specifying it as a pattern for your servlet in web.xml then you have to take care of loading the resources yourself
默认的servlet映射用于加载任何资源或请求,而无需在web.xml中进行显式映射。通常使用此加载静态资源。如果通过在web.xml中将其指定为servlet的模式来覆盖它,那么您必须自己负责加载资源
Spring MVC helps by providing resource handling via You have to specify the mapping which matches your static resources url pattern and the location where your resources reside physically
Spring MVC通过提供资源处理来帮助您必须指定与您的静态资源URL模式匹配的映射以及资源实际驻留的位置
You did specify
你确实指定了
<resources mapping="/resources/**" location="/resources/" />
but your image does not reside in a resources folder under webapp Maven folder. Instead its in images folder. Hence you have to add another resources tag like this
但您的图像不驻留在webapp Maven文件夹下的资源文件夹中。而是在图像文件夹中。因此,您必须添加这样的另一个资源标记
<resources mapping="/images/**" location="/images/" />
NB The mapping attribute and the location attribute does not have to correspond. For example you can have a url like
NB映射属性和位置属性不必对应。例如,你可以有一个网址
http://localhost:9999/restfulspringmvc/images/avatar.jpg
and your images are in /static-resources/images under webapp the your mapping will be
并且您的映像将位于webapp下的/ static-resources / images中
<resources mapping="/images/**" location="/static-resources/images/" />
You can also have multiple resources specification in the same application context
您还可以在同一应用程序上下文中具有多个资源规范