使用Spring MVC在jar文件中显示jsp页面

use*_*228 4 java spring jsp spring-mvc war

我正在使用Spring MVC 3.2.2在java中开发一个Web应用程序.我在jar文件中加载jsp页面时遇到问题.

Sring MVC Web应用程序具有以下结构:

|-META-INF
|-WEB-INF
    |-spring
    |    |- app-config.xml
    |-classes
    |-lib
    |   |-external.jar
    |       |-WEB-INF
    |       |   |-views
    |       |       |-external.jsp
    |       |-classes
    |            |-controller-for-external.class
    |-views
        |-... jsp
Run Code Online (Sandbox Code Playgroud)

在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 />
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

}

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>
Run Code Online (Sandbox Code Playgroud)

当试图显示external.jsp页面时,spring不想搜索它并显示错误HTTP 404 - /springapp/WEB-INF/views/external.jsp

Viv*_*ath 19

如果您使用的是Servlet 3.0,则可以执行此操作.但是,它不适用于打包JSP的方式.您必须以特定方式将资源打包到JAR文件中:

  • 将所有JSP放在META-INF/resourcesJAR 的目录中.
  • 包含一个web-fragment.xml文件META-INF.这类似于典型web.xml的一些差异.请看这里了解一些细节.我在这里找到一个例子.这是可选的.
  • 将JAR放入WEB-INF/libWAR中.

现在,您应该可以在应用程序的上下文中引用这些JSP.基本上META-INF/resources映射到webapp的文档根目录.看看这个更多细节.请记住,这不仅限于JSP.您可以在其中放置图像,JavaScript甚至CSS.


Lud*_*ume 8

感谢Vivin提供的解决方案.它对我有用.

在我的情况下,我有:

testModule.jar

    test/
        TestModule.class
    META-INF/
        resources/
            WEB-INF/
                views/
                    testModule.jsp
        MANIFEST.MF
        web-fragment.xml (empty)
    WEB-INF/
        lib/
Run Code Online (Sandbox Code Playgroud)

testProject.war

    test/
        ...
    META-INF/
        MANIFEST.MF
    WEB-INF/
        spring/
            ... spring configs ...
        views/
            test.jsp
        lib/
            testModule.jar
        web.xml
Run Code Online (Sandbox Code Playgroud)

可能很重要的是,Servlet 3.0 不会处理 Glassfish 3.1.2中的Eclipse部署方案中的碎片(它可能在JBoss或其他工作中有效,有待确认).实际上,我在testProject.war的部署程序集中设置了testModule.jar,当我通过Eclipse部署时,它生成一个文件夹testModule.jar而不是JAR文件.

但是当它作为WAR导出时它正在工作.

编辑:

要使其与Eclipse部署一起使用,您必须检查Glassfish 3.1.2属性中的选项:Deploy as jar.我认为这是最近一个插件版本中实现的最新选项.在开普勒可见但在Juno上没有.