我的Spring MVC Web应用程序的控制器监听:
"/admin"
返回管理页面,"/admin/change"
,在提交管理页面上的表单时调用。然后控制器再次返回管理页面。这是我的 AdminController.java :
@Controller
@RequestMapping("/admin")
public class AdminController {
@RequestMapping(method = RequestMethod.GET)
public String admin(Map<String, Object> model, Principal principal) {
return "admin"; //returns admin.jsp
}
@RequestMapping(value = "/change", method = RequestMethod.POST)
public String changeClient(@RequestBody MultiValueMap<String, String> form,
Map<String, Object> model, Principal principal) {
model.put("message", "Changed to " + form.getFirst("client"));
return "admin";
}
}
以下是 admin.jsp :
的摘录<form action="/admin/change" method="post" role="form">
<div class="form-group has-success">
<label class="control-label">Select the client</label>
<div>
<label>
<input class="radio" name="client" type="radio" value="1">
<span>Melik</span>
</label>
<label>
<input class="radio" name="client" type="radio" value="2">
<span>Memmed</span>
</label>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<button type="submit" class="btn btn-primary">Switch</button>
<c:if test="${message != null}">
<span class="has-success"><c:out value="${message}"/></span>
</c:if>
</div>
</div>
</form>
问题是,当我访问/admin
时,会提供一个漂亮的admin.jsp,但是,当我在此页面上提交表单然后返回admin.jsp时,这次静态文件不会被提供admin.jsp。
这就是我在jsp中引用静态文件的方式(我猜这里可能有问题,但我无法修复它):
<script src="/js/vendor.js"></script>
<link rel="stylesheet" href="/css/vendor.css">
我还尝试使用<c:url>
标记进行以下变体:
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title> Admin Panel </title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="<c:url value="/apple-touch-icon.png"/>">
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="<c:url value="/css/vendor.css"/>">
<!-- Theme initialization -->
<script>
var themeSettings = (localStorage.getItem('themeSettings')) ? JSON.parse(localStorage.getItem('themeSettings')) :
{};
var themeName = themeSettings.themeName || '';
if (themeName)
{
document.write('<link rel="stylesheet" id="theme-style" href="<c:url value="/css/app-' + themeName + '.css"/>">');
}
else
{
document.write('<link rel="stylesheet" id="theme-style" href="<c:url value="/css/app.css"/>">');
}
</script>
</head>
表单提交后,Chrome在返回的管理页面上显示以下错误:
Refused to apply style from 'http://localhost:8080/admin/css/app.css'
because its MIME type ('text/html') is not a supported stylesheet MIME type,
and strict MIME checking is enabled.
以下是我的 dispatcher-servlet.xml 的摘录:
<mvc:annotation-driven/>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
这是我的项目结构:
src:
--main:
----java
----resources:
------app.properties
----webapp:
------css
------js
------WEB-INF:
--------dispatcher-servlet.xml
--------web.xml
--------pages:
----------admin.jsp
那么,为什么在提交表单后返回没有静态文件的admin.jsp?