Web App hits jar package full record

Source: Internet
Author: User
Tags tld

Content is original, reproduced please indicate the source

Outside the question

Because of the needs of the project-no matter how it is generated-the need to support the use of a Web application as a jar package for other applications has the following process.

This process uses spring and SPRINGMVC (the version used is 3.1.4, after the description) of a number of things, if not the thing, please float.

What do you want to do?

The web app that needs to be packaged contains the following:

    1. Static resources, including JS, CSS, image
    2. Paging File: JSP
    3. JSP tags, including TLD files and several Java code
    4. Several other control and service based on the SPRINGMVC

To package a web app, you need to support these files for packaging and to be able to access them properly after packaging.

Workarounds for Static Resources

SPRINGMVC provides a "mvc:resources", which can be accessed after using the static file in the jar (specific mvc:resources is what and ask Niang) and look at the configuration example:

configuration of the Dispatcher-servlet.xml: <  mapping= "/test/js/**"  location= "classpath:/staticres/test/js/"  Cache-period= "31556926"/>

Here are a few pits:

    1. The static resources that go into this configuration need to be intercepted by the spring servlet, and look at the configuration:
configuration of Web. xml:<servlet>        <Servlet-name>Dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Init-param>            <Param-name>Contextconfiglocation</Param-name>            <Param-value>classpath*:d ispatcher-servlet.xml</Param-value>        </Init-param>        <Load-on-startup>3</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Dispatcher</Servlet-name>        <Url-pattern>/test/js/*</Url-pattern></servlet-mapping>

2. For Springmvc, in the above configuration, the path in the mapping of the resources is relative to the path after the Url-pattern match in the servlet-mapping of the Web. XML, for example, the access resource file is:/ Test/js/test.js, by Weib.xml match, into the mvc:resources to determine the path is/test.js, and mapping is configured/test/js/**, so that the match is unsuccessful, Will not be located in the back of the location.

This hole is a bit big, for some reason, in addition to the test directory JS, there are JSP, CSS and other files, finally, modified the SPRINGMVC a little bit of code:

class: Org.springframework.web.servlet.handler. abstracturlhandlermapping

  Method: Gethandlerinternal

Original code:

The Modified code:

  

With this configuration, all you have to do is hit the static resource into the jar package and configure the appropriate mapping path.

Page file

Fixed the static resources, JSP page to find another way. Given that the container will compile the JSP file to generate Java into class, then it is not possible to consider the JSP resource generation class to hit the jar package. In the mother's tireless help, the Buddy told me how to do:

JSP into JAR package: http://mrhouzhibin.blog.163.com/blog/static/19459624120120119445038/

Since there is such a good thing, take to use Bai.

Very smooth jar generation, JSP corresponding servlet configuration also generated, but encountered a problem, so many JSP servlet, if you throw to the Web. XML, follow-up maintenance can not be a headache, there is no way to split the web. The answer is natural, the degree Niang said, please refer to the following:

How to introduce additional XML files into the Web. http://www.blogjava.net/jiangjf/archive/2009/04/13/264685.html

Note the following:

Considering that the XML file is always validate error in eclipse, the filename suffix of the servlet corresponding to the generated JSP is changed to TXT, and then introduced in Web. XML, done.

JSP tags

JSP tags in the Java code is nothing special, that is, *.tld files to think of ways. It seems that the container parsing is like only the TLD files in the Web-inf directory and the Meta-inf in the jar package, how can I make TLD files available in a Web application that needs to be packaged, and then hit the Meta-inf directory when packaged. Think of a way to deal with the following:

    1. Create a new resource directory, for example named resource
    2. Create a new directory in resource Meta-inf
    3. copy TLD files to Meta-inf
    4. Add the following configuration to the Web application's XML:
<Jsp-config><taglib>         <Taglib-uri>http://www.test.com/jsp/t</Taglib-uri>     <taglib-location>Classes/meta-inf/test.tld</taglib-location>     </taglib></Jsp-config>

In this case, the TLD file in the jar package is in the Meta-inf directory and does not need to be configured in Web. Xml.

Several code based on spring

Here is the basic Java code, it should be in the jar, just a little bit small pits, in the export jar package, you need to tick an east, or spring annotations are not valid.

Where are the results?

What did you get after such a number of steps?

The following are some of the outputs:

    1. A common jar, named Com.test.base-1.0.0.jar
    2. JSP's jar, named Com.test.base.jsp-1.0.0.jar
    3. The JSP is converted to a servlet configuration file and named JspServlet.txt

So, just throw these two jars under the project Lib to reference the Web App (and throw in a couple of other coherent jar packages at the same time), and bring this jspservlet into the WEB.XM.

Have you succeeded?

If, as I generally, spring used in the 3.1.4,web.xml version is 3.0,tomcat used is not the version is too old, I think it will be successful.

I'm not testing on another Web container, and I'm not testing other versions of spring

With this article, I would like to thank a number of people who helped me with my mother's help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.