使用Spring MVC在jar文件中显示jsp页面 [英] Show jsp page inside jar file using Spring MVC
问题描述
我正在使用Spring MVC 3.2.2用Java开发Web应用程序.我在从jar文件加载jsp页面时遇到问题.
I'm developing a web application in java using Spring MVC 3.2.2. I'm having problems loading jsp page from within the jar file.
Sring MVC Web应用程序具有以下结构:
The Sring MVC web application that the following structure:
|-META-INF
|-WEB-INF
|-spring
| |- app-config.xml
|-classes
|-lib
| |-external.jar
| |-WEB-INF
| | |-views
| | |-external.jsp
| |-classes
| |-controller-for-external.class
|-views
|-... jsp
在app-config.xml中配置
Config in app-config.xml
<context:annotation-config />
<context:component-scan base-package="com" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:resources mapping="/views/**" location="classpath:/WEB-INF/views/" />
<mvc:annotation-driven />
external.jsp的控制器
Controller for external.jsp
@Controller
public class ExternalController {
protected final Log logger = LogFactory.getLog(getClass());
@RequestMapping(value="/external.htm")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Map<String, Object> myModel = new HashMap<String, Object>();
myModel.put("msg", "External page loaded");
return new ModelAndView("external", "model", myModel);
}
}
web.xml文件
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Springapp</display-name>
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/app-config.xml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
当尝试显示external.jsp页面时,spring不想搜索它并显示错误HTTP 404-/springapp/WEB-INF/views/external.jsp
When a try to show external.jsp page, spring don't want search it and show error HTTP 404 - /springapp/WEB-INF/views/external.jsp
推荐答案
如果使用的是Servlet 3.0,则可以执行此操作.但是,它与打包JSP的方式不兼容.您必须以特定方式将资源打包到JAR文件中:
You can do this if you are using Servlet 3.0. However, it won't work with the way you have your JSPs packaged. You have to package resources into a JAR file in a specific way:
- 将所有JSP放在JAR的
META-INF/resources
目录中. - 在
META-INF
中包含一个web-fragment.xml
文件.这类似于典型的web.xml
,但有一些区别.有关某些详细信息,请参见此处.我在这里找到了一个示例.这是可选的. - 将JAR放入WAR的
WEB-INF/lib
中.
- Put all your JSPs in the
META-INF/resources
directory of your JAR. - Include a
web-fragment.xml
file inMETA-INF
. This is similar to a typicalweb.xml
with some differences. See here for some details. There is also an example I found here. This is optional. - Put your JAR in
WEB-INF/lib
of your WAR.
现在,您应该能够在应用程序的上下文中引用这些JSP.基本上META-INF/resources
映射到Web应用程序的文档根目录.请查看此以了解更多详细信息.请记住,这不仅限于JSP.您可以在其中放置图片,JavaScript甚至CSS.
Now you should be able to refer to those JSPs within your app's context. Basically META-INF/resources
is mapped to the document root of your webapp. Look at this for more details. Keep in mind that this is not limited to JSPs. You can put images, JavaScript, or even CSS in there.
这篇关于使用Spring MVC在jar文件中显示jsp页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!