Java Web development: WebJars, Java Web development webjars
After changing to a new job, I got a lot of work, basically had no time to access the Internet, and my Blog writing became less. I was still in the technology-driven environment that I used to drive, and everyone was actively learning new technologies, share new technologies. Now there are fewer technical environments and less passion for learning. Don't talk about it. Write your own learning WebJars.
Java Web Front-end usually uses JS or CSS technologies, such as jQuery, Backbone. js, and Twitter Bootstrap. Previously, I copied these Web resources to the corresponding Webapp directory of the Java Web project. This manual copy may produce version errors, copy version errors, and missing copies, the front-end page cannot be correctly displayed. Of course, to some extent, I feel very good that many folders and files have become a jar.
WebJars packages Web Front-end Javascript and CSS resources into Java Jar packages. In this way, we can use Maven to manage these dependent libraries in Java Web development to ensure the uniqueness of these Web resource versions. The basic principles are as follows:With any Servlet 3 compatible container, the WebJars that are in the WEB-INF/lib directory are automatically made available as static resources. This works because anything in a META-INF/resources directory in a JAR in WEB-INF/lib is automatically exposed as a static resource.The following describes how to use WebJars, which is very simple. 1. Define the jar package for js or css in the maven configuration file.
<dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery-cookie</artifactId> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>dojo</artifactId> </dependency>
2. In the jsp file, obtain the file (webjars/js or css jar name/version/detail file name) in the following path)
<script type="text/javascript" src="<%=request.getContextPath() %>/webjars/jquery/1.9.0/jquery.min.js"></script><script type="text/javascript" src="<%=request.getContextPath() %>/webjars/bootstrap/3.0.2/js/bootstrap.min.js"></script> <script type="text/javascript" src="<%=request.getContextPath() %>/webjars/jquery-cookie/1.3.1/jquery.cookie.js"></script>
PS: Processing static resources for large websites is certainly not a Java Server. Basically, apache or nginx is used to process static processing, with better performance. For small websites, you can directly use WebJars.