我知道有很多关于这个问题的问题,但我的不同,我根据以前的问题找不到任何解决方案。我正在使用spring MVC,一切正常,直到我发现我的浏览器没有渲染图像。然后我试图给我的浏览器完整的URL到我的图像,它返回404.我试过
<mvc:default-servlet-handler/>
<mvc:resources mapping="/images/**" location="/images/"/>
这没有显示我的图像,但它也以某种方式弄乱了我的控制器,它没有处理url映射。我会告诉你我的大部分代码。
这是我的 web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>spring-brains</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<multipart-config/>
</servlet>
<servlet-mapping>
<servlet-name>spring-brains</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-brains-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
这是我的 spring-brains-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package="bobi.spring.spring.brains"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:default-servlet-handler/>
<mvc:resources mapping="/images/**" location="/images/"/>
<security:debug/>
<security:http pattern="/admin/login.jsp" security="none"/>
<security:http auto-config="true">
<security:intercept-url pattern="/admin/**" access="ROLE_USER" />
<security:form-login login-page="/admin/login.jsp" default-target-url="/admin/admin.jsp" always-use-default-target="true"/>
<security:logout logout-success-url="/admin" />
</security:http>
<bean class="service.DummyUserService" id="user-service-bobi"/>
<security:authentication-manager>
<security:authentication-provider user-service-ref="user-service-bobi">
</security:authentication-provider>
</security:authentication-manager>
这是我的 AdminController :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bobi.spring.spring.brains;
import domains.User;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.Authentication;
import service.ProductManager;
import service.Service;
/**
*
* @author BOBBOO
*/
@MultipartConfig
@Controller
@RequestMapping(value = "/admin")
public class AdminController {
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping(value = "/{adminview}")
public ModelAndView getAdminView(@PathVariable("adminview") String view) {
switch (view) {
case "login": {
return new ModelAndView("admin/login");
}
case "admin": {
return new ModelAndView("admin/admin");
}
case "newcategory": {
return new ModelAndView("admin/newcategory");
}
case "newproduct": {
return new ModelAndView("admin/newproduct", "categories", Service.getCategories());
}
case "newuser": {
return new ModelAndView("admin/newuser");
}
case "products": {
return new ModelAndView("admin/products", "products", Service.getProducts());
}
default:
return new ModelAndView("admin/login");
}
}
@RequestMapping("/form")
public void formHandler(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileUploadException {
String formId = request.getParameter("formId");
request.setCharacterEncoding("UTF-8");
if (formId != null && formId.equals("newcat")) {
service.Admin.saveCategory((HashMap) ServletUtils.prepareRequestArguments(request));
response.sendRedirect("admin/newcategory");
} else if (formId != null && formId.equals("login")) {
Authentication auth = new Authentication();
User user = service.Service.getUser(request.getParameter("username"));
} else if (formId != null && formId.equals("newuser")) {
Authentication auth = new Authentication();
boolean success = auth.CheckUserName(request.getParameter("username"));
if (success) {
service.Admin.saveUser((HashMap) ServletUtils.prepareRequestArguments(request));
response.sendRedirect("admin/newuser");
} else {
response.sendRedirect("admin/login");
}
}
}
@RequestMapping(value = "/upload")
public void productAdd(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
if (ServletFileUpload.isMultipartContent(request)) {
request.setCharacterEncoding("UTF-8");
Part part = request.getPart("picture");
String path = service.Constants.LINUX_ABS_PROJECT_PATH + System.getProperty("file.separator") + "images";
Service serv = new Service();
String kategorija = request.getParameter("category");
System.out.println(kategorija);
try {
serv.SaveFile(path, part);
} catch (Exception e) {
e.printStackTrace();
}
ServletUtils util = new ServletUtils();
try {
service.Admin.saveProduct((HashMap) util.prepareRequestMultiPartArguments(request));
} catch (Exception e) {
logger.info("Error while saving product @ AdminController, line 92!!");
}
response.sendRedirect(request.getContextPath() + "/admin/admin.jsp");
}
}
@RequestMapping(value="/edit/{object}/{objectId}")
public ModelAndView editTable(@PathVariable("object") String editItemType,
@PathVariable("objectId") String itemId){
switch(editItemType){
case "product":{
return new ModelAndView("admin/editproduct", "product", Service.getProduct(Integer.parseInt(itemId)));
}
case "category":{
return new ModelAndView("admin/editcategory", "category", Service.getProduct(Integer.parseInt(itemId)));
}
case "user":{
return new ModelAndView("admin/edituser", "user", Service.getProduct(Integer.parseInt(itemId)));
}
}
return new ModelAndView("admin/admin");
}
@RequestMapping(value="/formedit")
public void editForm(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileUploadException {
String formId = request.getParameter("formId");
request.setCharacterEncoding("UTF-8");
if (formId != null && formId.equals("newcat")) {
service.Admin.saveCategory((HashMap) ServletUtils.prepareRequestArguments(request));
response.sendRedirect("admin/newcategory");
} else if (formId != null && formId.equals("login")) {
Authentication auth = new Authentication();
User user = service.Service.getUser(request.getParameter("username"));
} else if (formId != null && formId.equals("newuser")) {
Authentication auth = new Authentication();
boolean success = auth.CheckUserName(request.getParameter("username"));
if (success) {
service.Admin.saveUser((HashMap) ServletUtils.prepareRequestArguments(request));
response.sendRedirect("admin/newuser");
} else {
response.sendRedirect("admin/login");
}
}
}
}
基本上我有两个问题: 1.如何让我的应用程序显示静态图像内容? 2.为什么以及如何 - &gt;
<mvc:default-servlet-handler/>
<mvc:resources mapping="/images/**" location="/images/"/>
弄乱我的控制器,或者我说停止它处理它应该处理的映射。
先谢谢你,我一直在努力解决这个问题
答案 0 :(得分:-2)
您还没有在问题中包含jsp或视图。使用JSTL标记从Web应用程序的根目录生成绝对路径:
<img src="<c:url value='/images/yourimage.jpg'/>">