Java – a best practice for sharing web layer code (controllers and JSPS) between similar web applications

I'm trying to rewrite some aging web applications There are two very, very similar, but I didn't share the code today. My purpose is to solve this problem

These projects are being rewritten with maven, spring MVC and SiteMesh

The model layer code is easily shared using jars But I don't know any good way to share common web layer code (JSP and controller) between similar applications

Here's some background These apps are online stores One is a general store where users can log in, search for products, add to shopping carts and check out (think Amazon. Com) The other is basically the same, just a Punchout website Product browsing and shopping cart are the same However, login and checkout are completely different

I'm too simple, but it's enough to explain the problem A large portion of the web layer code in the product browsing and shopping cart sections should be able to be shared between the two

I don't think you can simply run the same war file with "schema" based on environment variables or settings from different databases One difference is the completely different spring security configuration It is best to leave the login and checkout controllers of other sites from the component scan, otherwise no one can transition to the wrong controller through URL operation

I initially started using Maven profiles and filtering to save two different configuration sets (web.xml, spring configuration, etc.) in the same war project Based on which Maven profile is selected, the generated war is built with different configuration sets (use different names for clarity) This violates principal maven, a man who produces an artifact

Is there a better way to do this? How about Maven war overlays? I've seen people talk about using overlays to share common resources like CSS, JS, images, and even some common JSPS But I haven't seen anyone mention such a shared class controller

I can send controller analogies to jars, but logically, they should remain in their respective JSPS JSP can't be pushed to jar (right?)

I also consider it as an ear with multiple war files, one for common shopping experience and the other for proper login and checkout I believe that sessions can be shared between two wars in the same ear, but I don't know if it works well with spring's session scope beans I hear they're not really stored in the conversation I also need to figure out what the SiteMesh decorator for headers / footers should do The same SiteMesh configuration and its resources need to be copied into two wars, right? So finally, the shopping war artifact will still be different in each case

I must believe that others have dealt with it before Did I think I was wrong? Is there a common solution to this kind of thing?

Solution

Good work done against copy and paste Why is it difficult to share JSPS? You can use the Maven dependency plug-in to copy them from the shared jar:

<plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-dependency-plugin</artifactId>
         <version>2.4</version>
         <executions>
           <execution>
             <id>unpack</id>
             <phase>package</phase>
             <goals>
               <goal>unpack</goal>
             </goals>
             <configuration>
               <artifactItems>
                 <artifactItem>
                   <groupId>com.example</groupId>
                   <artifactId>webapp-common</artifactId>
                   <version>1.0-SNAPSHOT</version>
                   <outputDirectory>[target jsp directory]</outputDirectory>
                   <includes>**/*.jsp</includes>
                 </artifactItem>
               </artifactItems>
             </configuration>
           </execution>
         </executions>
       </plugin>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>