【发布时间】:2017-03-02 05:58:03
【问题描述】:
在我使用 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
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 获取图像?非常感谢!!
【问题讨论】:
-
“图片”没有 URL 匹配模式
-
是的,我明白了,图像是图像文件夹保存图像,那么我如何配置“图像”的模式?
-
图片存放在哪里?你有一个包含静态资源(js、css、图像...)的资源文件夹吗?
-
是的,图片位置:webapp/images/avatar.jpg
-
正如@JimHawkins 所说,没有图像映射。所以在 servlet-context.xml 你应该有
这将为 /images 添加映射,然后调用 http://localhost :9999/images/avatar.jpg
标签: java json spring spring-mvc model-view-controller