【发布时间】:2020-10-14 14:06:37
【问题描述】:
我被这个问题困扰了一段时间,我无法在我的 jsp 页面上显示 css。
我正在使用基于注释的配置,我几乎尝试了所有方法,但似乎我根本无法显示任何 css,我的 css 页面本质上将页面的背景从白色更改为黑色,显然我可以通过 jsp 做到这一点但是这个不能解决根本问题。
public class ApplicationInit extends AbstractContextLoaderInitializer{
@Override
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext rootContext
= new AnnotationConfigWebApplicationContext();
rootContext.register(DeepAlgorithmConfiguration.class);
return rootContext;
}
}
配置类
@Configuration
@ComponentScan({"com.test.pluto"})
@EnableWebMvc
public class DeepAlgorithmConfiguration implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setSuffix(".jsp");
return internalResourceViewResolver; }
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("WEB-INF/resources/bootstrap/");
}
}
初始化类
public class DeepAlgorithmInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{DeepAlgorithmConfiguration.class};
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
控制器类
@Controller
public class HelloController {
@RequestMapping("/add")
public ModelAndView add(@RequestParam("t1") int k, @RequestParam("t2") int l)
{
int answer = l + k;
System.out.println(answer);
ModelAndView mv = new ModelAndView();
mv.setViewName("display");
mv.addObject("r1",answer);
return mv;
}
}
index.jsp 页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<link href="<c:url value="/resources/style.css/" />" rel="stylesheet">
</head>
<body>
<form action="add">
<input type="text" name="t1" style=""><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
Web.XML(不确定是否需要)
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app id = "WebApp1234" version="2.4"
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
HelloWeb-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="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-3.0.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.test.pluto"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
</bean>
</beans>
【问题讨论】:
-
你在你的应用程序中使用 spring-security 吗?
标签: java spring-mvc web model-view-controller resources