Java 使用Spring/JSF don';不加载CSS
我正在尝试运行下面的项目,但是我在InternetExplorer9上遇到了一个文档模式的“bug”:IE9标准。它只是不加载css 在Chrome、Firefox、IE7和IE8、IE9和IE8标准/IE7标准中运行正常 我能做些什么来修复这个“错误” web.xmlJava 使用Spring/JSF don';不加载CSS,java,css,spring,internet-explorer,jsf,Java,Css,Spring,Internet Explorer,Jsf,我正在尝试运行下面的项目,但是我在InternetExplorer9上遇到了一个文档模式的“bug”:IE9标准。它只是不加载css 在Chrome、Firefox、IE7和IE8、IE9和IE8标准/IE7标准中运行正常 我能做些什么来修复这个“错误” web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
<?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">
<display-name>HibernateSpringJSFDispatcher</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<context-param>
<param-name>facelets.REFRESH_PERIOD</param-name>
<param-value>2</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWorldController.java
package br.gov.pr.maringa.hibernatespringjsfdispatcher.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import br.gov.pr.maringa.exception.InvalidEntityException;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.HelloWorld;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.Usuario;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.UsuarioHome;
@Controller
public class HelloWorldController {
@Autowired
private HelloWorld helloWorld;
@Autowired
private UsuarioHome uh;
@RequestMapping(value="/helloWorld", method=RequestMethod.GET)
public void helloWorld() {
helloWorld.setMessage("Hello World from Spring MVC to JSF");
Usuario u = new Usuario();
u.setUsuario("claudio");
u.setSenha("1234");
try {
uh.save(u);
} catch (InvalidEntityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value="/helloWorld", method=RequestMethod.POST)
public void helloWorldPost(@RequestParam String msg) {
helloWorld.setMessage(msg);
}
}
helloWorld.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Hello World</title>
</h:head>
<h:body>
<p>${helloWorld.message}</p>
<p:panel header="qualquercoisa">abc</p:panel>
<p>Say something: </p>
<form method="post">
<input name="msg" type="text"></input>
<input type="submit" ></input>
</form>
<p>This image is linked as a JSF resource</p>
<h:graphicImage name="images/jsf.jpg" />
<p>This image is directly linked</p>
<img src="resources/images/spring.png"></img>
</h:body>
</html>
你好,世界
${helloWorld.message}
abc
说点什么:
此图像作为JSF资源链接
此图像是直接链接的
参考资料:
IE9标准模式仅接受css资源的
内容类型:text/css
。如果返回另一个内容类型,IE9将完全忽略整个CSS资源
您需要绝对确保自定义资源处理程序设置了正确的内容类型头。我不知道Spring在这里是如何发挥作用的,也不知道定制资源处理程序在这个构造中到底有什么意义,但是默认情况下,内容类型是基于文件扩展名设置的。至少,您需要确保
CustomResource
的Resource#getContentType()
方法返回text/cssIE9标准模式只接受内容类型:text/css
作为css资源。如果返回另一个内容类型,IE9将完全忽略整个CSS资源
您需要绝对确保自定义资源处理程序设置了正确的内容类型头。我不知道Spring在这里是如何发挥作用的,也不知道定制资源处理程序在这个构造中到底有什么意义,但是默认情况下,内容类型是基于文件扩展名设置的。至少,您需要确保CustomResource
的Resource\getContentType()
方法返回text/css
package br.gov.pr.maringa.hibernatespringjsfdispatcher;
import javax.faces.application.Resource;
import javax.faces.application.ResourceHandler;
import javax.faces.application.ResourceHandlerWrapper;
import javax.faces.application.ResourceWrapper;
import javax.faces.context.FacesContext;
import com.sun.faces.util.Util;
/**
* Custom JSF ResourceHandler.
*
* This handler bridges between Spring MVC and JSF managed resources. The handler takes
* care of the case when a JSF facelet is used as a view by a Spring MVC Controller and the
* view uses components like h:outputScript and h:outputStylesheet by correctly pointing the
* resource URLs generated to the JSF resource handler.
*
* The reason this custom handler wrapper is needed is because the JSF internal logic assumes
* that the request URL for the current page/view is a JSF url. If it is a Spring MVC request, JSF
* will create URLs that incorrectly includes the Spring controller context.
*
* This handler will strip out the Spring context for the URL and add the ".jsf" suffix, so the
* resource request will be routed to the FacesServlet with a correct resource context (assuming the
* faces servlet is mapped to the *.jsf pattern).
*
*
*/
public class CustomResourceHandler extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
public CustomResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ResourceHandler getWrapped() {
return this.wrapped;
}
@Override
public Resource createResource(String resourceName, String libraryName) {
return new CustomResource(super.createResource(resourceName, libraryName));
}
@Override
public Resource createResource(String resourceName, String libraryName,
String contentType) {
return new CustomResource(super.createResource(resourceName, libraryName, contentType));
}
private static class CustomResource extends ResourceWrapper {
private Resource wrapped;
private CustomResource(Resource wrapped) {
this.wrapped = wrapped;
}
@Override
public Resource getWrapped() {
return this.wrapped;
}
@Override
public String getRequestPath() {
String path = super.getRequestPath();
FacesContext context = FacesContext.getCurrentInstance();
String facesServletMapping = Util.getFacesMapping(context);
// if prefix-mapped, this is a resource that is requested from a faces page
// rendered as a view to a Spring MVC controller.
// facesServletMapping will, in fact, be the Spring mapping
if (Util.isPrefixMapped(facesServletMapping)) {
// remove the Spring mapping
path = path.replaceFirst("(" + facesServletMapping + ")/", "/");
// append .jsf to route this URL to the FacesServlet
path = path.replace(wrapped.getResourceName(), wrapped.getResourceName() + ".jsf");
}
return path;
}
}
}
package br.gov.pr.maringa.hibernatespringjsfdispatcher.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import br.gov.pr.maringa.exception.InvalidEntityException;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.HelloWorld;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.Usuario;
import br.gov.pr.maringa.hibernatespringjsfdispatcher.model.UsuarioHome;
@Controller
public class HelloWorldController {
@Autowired
private HelloWorld helloWorld;
@Autowired
private UsuarioHome uh;
@RequestMapping(value="/helloWorld", method=RequestMethod.GET)
public void helloWorld() {
helloWorld.setMessage("Hello World from Spring MVC to JSF");
Usuario u = new Usuario();
u.setUsuario("claudio");
u.setSenha("1234");
try {
uh.save(u);
} catch (InvalidEntityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@RequestMapping(value="/helloWorld", method=RequestMethod.POST)
public void helloWorldPost(@RequestParam String msg) {
helloWorld.setMessage(msg);
}
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:p="http://primefaces.org/ui"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Hello World</title>
</h:head>
<h:body>
<p>${helloWorld.message}</p>
<p:panel header="qualquercoisa">abc</p:panel>
<p>Say something: </p>
<form method="post">
<input name="msg" type="text"></input>
<input type="submit" ></input>
</form>
<p>This image is linked as a JSF resource</p>
<h:graphicImage name="images/jsf.jpg" />
<p>This image is directly linked</p>
<img src="resources/images/spring.png"></img>
</h:body>
</html>