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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17554572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:17:57  来源:igfitidea点击:

Show jsp page inside jar file using Spring MVC

javaspringspring-mvcjspwar

提问by user2565228

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.

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

The Sring MVC web application that the following structure:

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

Config in app-config.xml

在 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 />

Controller for external.jsp

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 file

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>

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

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

回答by Vivin Paliath

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:

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

  • Put all your JSPs in the META-INF/resourcesdirectory of your JAR.
  • Include a web-fragment.xmlfile in META-INF. This is similar to a typical web.xmlwith some differences. See herefor some details. There is also an example I found here. This is optional.
  • Put your JAR in WEB-INF/libof your WAR.
  • 将所有 JSP 放在META-INF/resourcesJAR 目录中。
  • 包括web-fragment.xml文件META-INF。这与典型的相似,但web.xml有一些不同。有关详细信息,请参阅此处。还有一个我在这里找到的例子。这是可选的。
  • 将您的 JAR 放入您WEB-INF/lib的 WAR 中。

Now you should be able to refer to those JSPs within your app's context. Basically META-INF/resourcesis mapped to the document root of your webapp. Look at thisfor more details. Keep in mind that this is not limited to JSPs. You can put images, JavaScript, or even CSS in there.

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

回答by Ludovic Guillaume

Thanks Vivin for your solution. It worked for me.

感谢 Vivin 的解决方案。它对我有用。

In my case, I had:

就我而言,我有:

testModule.jar

测试模块.jar

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

testProject.war

测试项目.war

    test/
        ...
    META-INF/
        MANIFEST.MF
    WEB-INF/
        spring/
            ... spring configs ...
        views/
            test.jsp
        lib/
            testModule.jar
        web.xml

It may be important to say that Servlet 3.0 do not handle fragments on an Eclipse deployment scenarioin Glassfish 3.1.2 (it maybe works in JBoss or others, to be confirmed). Actually, I set testModule.jar in deployment assembly of testProject.war and when I deploy through Eclipse, it generates a folder testModule.jar instead of a JAR file.

重要的是,Servlet 3.0不处理Glassfish 3.1.2 中Eclipse 部署场景中的片段(它可能适用于 JBoss 或其他,有待确认)。实际上,我在 testProject.war 的部署程序集中设置了 testModule.jar,当我通过 Eclipse 部署时,它生成一个文件夹 testModule.jar 而不是 JAR 文件。

But it's working when exporting as a WAR.

但它在作为 WAR 导出时有效。

EDIT:

编辑:

To get it working with Eclipse deployment, you have to check an option in Glassfish 3.1.2 properties : Deploy as jar. I think it's a recent option implemented in the last plugin version. Visible on Kepler but not on Juno.

要使其与 Eclipse 部署一起工作,您必须检查 Glassfish 3.1.2 属性中的一个选项:部署为 jar。我认为这是在上一个插件版本中实现的最新选项。在开普勒上可见,但在朱诺上不可见。

回答by Hasan K

I did it using Vivin's solution. Here's the folder structure of my JAR in Eclipse.

我是用 Vivin 的解决方案做到的。这是我在 Eclipse 中的 JAR 的文件夹结构。

enter image description here

在此处输入图片说明

Included the jar in the WEB-INF/lib folder of my WAR:

将 jar 包含在我的 WAR 的 WEB-INF/lib 文件夹中:

enter image description here

在此处输入图片说明

Also added the following in my pom.xml of WAR:

还在我的 WAR 的 pom.xml 中添加了以下内容:

 <configuration><packagingIncludes>WEB-INF/lib/*.jar</packagingIncludes>
 </configuration>

Included the jsp in my Web Project using :

使用以下命令将 jsp 包含在我的 Web 项目中:

<jsp:include page="/myService.jsp" flush="true" />

回答by tranductrinh

Structure works for me

结构对我有用

jar module (jar)enter image description here

jar 模块(jar)在此处输入图片说明

main web app module (war)enter image description here

主网络应用程序模块(War)在此处输入图片说明

And ViewResolver config (I'm using Thymeleaf):

和 ViewResolver 配置(我使用的是 Thymeleaf):

@Bean
public ServletContextTemplateResolver templateResolver() {
    ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
    resolver.setPrefix("/WEB-INF/template/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML5");
    resolver.setOrder(1);
    return resolver;
}