我正在建立一个MVC架构的网站,
这意味着我必须将所有views
个请求发送到Controller
。
首先,这就是我的项目的样子:
使用web.xml
,我将请求映射到控制器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>firstXDw</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>implementations.Controller</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/views/login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/views/*</url-pattern>
</servlet-mapping>
<!--
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/views/login.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/views/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>implementations.Controller</servlet-class>
</servlet>
<servlet>
<servlet-name>admincp</servlet-name>
<jsp-file>/views/admincp.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>handleList</servlet-name>
<jsp-file>/views/handleList.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>/views/login.jsp</servlet-class>
</servlet>
<servlet>
<servlet-name>index</servlet-name>
<jsp-file>/views/index.jsp</jsp-file>
</servlet>
<servlet>
<servlet-name>yourcoupons</servlet-name>
<jsp-file>/views/yourcoupons.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/views/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>admincp</servlet-name>
<url-pattern>/views/admincp.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>handleList</servlet-name>
<url-pattern>/views/handleList.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/views/login.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/views/index.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>yourcoupons</servlet-name>
<url-pattern>/views/yourcoupons</url-pattern>
</servlet-mapping>
-->
</web-app>
不幸的是,这是将每个请求映射到控制器,而不仅仅是.JSP文件(views
),
这使资源 - Css
和Javascript
文件无法访问。 (因为读取它们的instread,调用控制器)
如何仅映射.JSP
个文件?
类似的东西:
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/views/*.jsp</url-pattern>
</servlet-mapping>
提前致谢!我正在使用休眠。