无法在spring mvc + hibernate + maven中加载资源404
针对home.jsp
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title> Spring MVC 4 REST + AngularJS </title>
<%-- <link type="text/css" rel="stylesheet" href="<c:url value="resources/css/style.css"/>" /> --%>
<link href="<c:url value="/aresources/css/style.css" />" rel="stylesheet">
</head>
<body ng-app="myApp">
<div ng-controller="PersonController as personCtrl">
<h1> Spring MVC 4 REST + AngularJS </h1>
<form name="personForm" method="POST">
<table>
<tr><td colspan="2">
<div ng-if="personCtrl.flag != 'edit'">
<h3> Add New Person </h3>
</div>
<div ng-if="personCtrl.flag == 'edit'">
<h3> Update Person for ID: {{ personCtrl.person.pid }} </h3>
</div> </td>
</tr>
<tr>
<td>Name: </td> <td><input type="text" name="name" ng-model="personCtrl.person.name" required/>
<span ng-show="personForm.name.$error.required" class="msg-val">Name is required.</span> </td>
</tr>
<tr>
<td>Location: </td> <td> <input type="text" name="location" ng-model="personCtrl.person.location" required/>
<span ng-show="personForm.location.$error.required" class="msg-val">Location is required.</span> </td>
</tr>
<tr>
<td colspan="2"> <span ng-if="personCtrl.flag=='created'" class="msg-success">Person successfully added.</span>
<span ng-if="personCtrl.flag=='failed'" class="msg-val">Person already exists.</span> </td>
</tr>
<tr><td colspan="2">
<div ng-if="personCtrl.flag != 'edit'">
<input type="submit" ng-click="personCtrl.addPerson()" value="Add Person"/>
<input type="button" ng-click="personCtrl.reset()" value="Reset"/>
</div>
<div ng-if="personCtrl.flag == 'edit'">
<input type="submit" ng-click="personCtrl.updatePersonDetail()" value="Update Person"/>
<input type="button" ng-click="personCtrl.cancelUpdate()" value="Cancel"/>
</div> </td>
</tr>
<tr>
<td colspan="2"> <span ng-if="personCtrl.flag=='deleted'" class="msg-success">Person successfully deleted.</span>
</tr>
</table>
</form>
<table>
<tr><th>ID </th> <th>Name</th> <th>Location</th></tr>
<tr ng-repeat="row in personCtrl.persons">
<td><span ng-bind="row.pid"></span></td>
<td><span ng-bind="row.name"></span></td>
<td><span ng-bind="row.location"></span></td>
<td>
<input type="button" ng-click="personCtrl.deletePerson(row.pid)" value="Delete"/>
<input type="button" ng-click="personCtrl.editPerson(row.pid)" value="Edit"/>
<span ng-if="personCtrl.flag=='updated' && row.pid==personCtrl.updatedId" class="msg-success">Person successfully updated.</span> </td>
</tr>
</table>
</div>
<%-- <script src="${pageContext.request.contextPath}/resources/js/lib/angular.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/lib/angular-resource.min.js"></script>
<script src="${pageContext.request.contextPath}/resources/js/app.js"></script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css"/>--%>
</body>
</html>
appconfig.java
package com.concretepage.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan("com.concretepage")
@Import(DBConfig.class)
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/view/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/aresources/**").addResourceLocations("/resources/");
}
}
webappinitializer.java
package com.concretepage.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {AppConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
GET 访问资源时出现http://localhost:8081/springAngulerHibernate/aresources/css/style.css 404错误。所以style.css没有加载
答案 0 :(得分:0)
您的a
似乎还有mvc:resources
封信。而不是:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/aresources/**")
.addResourceLocations("/resources/");
}
使用它:
addResourceHandler("/resources/**")
尝试与文件夹命名更加一致,是资源吗?还是资源?
其次,使用Spring URL标记,以便更好地解析您的网址。
以下是我如何导入bootstrap.min.css:
<link rel="stylesheet" href='<spring:url value="/resources/bootstrap/bootstrap.min.css"/>' type="text/css" />
不要忘记添加taglib,如下所示: