Develop Spring MVC Applications (1)

xiaoxiao2021-03-06  171

1. Establishment of basic applications and environments

(Translator: This tutorial is originally used by Ant manual compilation and deployment, where there is a different place to use the Eclipse development environment, you can refer to the original text)

(1) Development environment

l Java SDK 1.4.2

l apache tomcat 5.0.25

l Eclipse 3.0

l Eclipse plugin:

Ø EMF SDK 2.0.1: ECLIPSE modeling framework, Lomboz plugin, you can use Runtime version)

Ø Lomboz 3.0: J2EE plugin for developing J2EE applications in Eclipse

Ø Spring IDE 1.0.3: Spring Bean Configuration Management Plugin

l Springframework 1.0.2

(2) Plug-in configuration

The following is a simply talking about the main configuration of the above plugin in Eclipse:

Lomboz

l Window / Customize Perspective / Shoutcuts / New: Select Java / Lomboz J2EE Wizards / Lomboz J2ee Project

l Window / Customize Perspective / Commands: Check Lomboz Actions

l Window / Preferences / Lomboz: Specify the location of JDK Tools.jar

l Window / Preferences: Select Server Types to apache tomcat v5.0.x, specify Tomcat installation directory, HTTP access address and port number (Translator: Use 8888), and ClassPath variables in Properties Tomcat_home value (ie Tomcat installation directory)

Spring IDE

l WINDOW / CUSTOMIZE PERSPECTIVE / SHOUTCUTS / SHOW View: Select Spring Ide

(3) Creating a J2EE project

l File / New / Lomboz J2EE Project

l Project settings: Specify project name SpringApp

l Java settings: Note that the default output directory is bin, change to SpringApp / SpringApp / Web-INF / CLASSES

l Create J2EE Module: Select the web modules page, click the Add button to specify the web module name SpringApp; select the Targeted Servers page, Type Select Apache Tomcat V5.0.x, click Add buttons to increase

l Click the finish button to complete the creation of the project.

(4) INDEX.JSP

Create an application's entry page INDEX.JSP, here is just a test of the environment:

eXample :: spring application </ title> </ head></p> <p><body></p> <p><H1> EXAMPLE - Spring Application </ h1> <p> this is my test. </ p></p> <p></ body></p> <p></ html></p> <p>(5) Deploy the application to Tomcat</p> <p>l On the toolbar, click the Press to Open A J2EE Project Outline button, open the Lomboz J2EE VIEW view</p> <p>l Select the SpringApp module of SpringApp project in the modules page, right click to select Deploy</p> <p>(6) Test application</p> <p>l In the browser, enter http: // localhost: 8888 / springApp, see the normal test page, indicating that the environment has been established.</p> <p>l The following starts to develop Spring MVC applications</p> <p>(7) Modify the web-inflicity of Web.xml</p> <p>l Define DispatCherServlets to control all requests, while having a standard servlet-maping map to * .htm URL mode</p> <p><servlet></p> <p><servlet-name> SpringApp </ servlet-name></p> <p><servlet-class> Org.springframework.web.servlet.dispatcherServlet </ servlet-class></p> <p><load-on-startup> 1 </ load-on-startup></p> <p></ servlet></p> <p><servlet-mapping></p> <p><servlet-name> SpringApp </ servlet-name></p> <p><url-pattern> *. htm </ url-pattern></p> <p></ servlet-maping></p> <p>l Create SpringApp-Servlet.xml (App Program Name --Servlet.xml), configure the web application for DispatcherServlet to use Context</p> <p>Ø Define the bean entry named SpringAppController, point to the SpringAppController controller</p> <p>Ø Use SimpleurlHandlerMapping to define the URL mapping, map the /hello.htm to the SpringAppController controller</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><! Doctype beans public "- // Spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd"></p> <p><! -</p> <p>- Application Context Definition for "SpringApp" DispatcherServlet.</p> <p>-></p> <p><beans></p> <p><bean id = "SpringAppController" class = "springappcontroller" /></p> <p><bean id = "urlmapping" class = "org.springframework.web.servlet.Handler.SIMPLEURLHANDLERMAPPING"></p> <p><Property Name = "MAppings"> <props></p> <p><prop key = "/ hello.htm> SpringAppController </ prop></p> <p></ prOPS></p> <p></ Property></p> <p></ bean></p> <p></ beans></p> <p>l (Translator: Supplement Spring IDE)</p> <p>Ø Select SpringApp project, right click to select Add Spring Beans Project Nature, the original J symbol turns S.</p> <p>Ø Window / show View / Spring Beans, Open Spring Beans View</p> <p>Ø Select SpringApp in the view, right click to select Properties</p> <p>Ø Click the Add button in the config files page to join SpringApp-Servlet.xml</p> <p>Ø In the Spring Beans view, you can see the tree of springapp-servlet.xml, double-click SpringApp-servlet.xml to open files, double-click the specific structure, automatically position</p> <p>Ø Select SpringApp-servlet.xml, right click to select Show Graph, you can display the view structure of the file</p> <p>(8) Copy Jars to Web-INF / LIB Directory</p> <p>l Import Dist / Spring.jar, LIB / JAKARTA-COMMONS / Commons-Logging.jar, Lib / Log4j / Log4j-1.2.8.jar file into the springapp / web-inf / lib directory of SpringApp project</p> <p>(9) Creating a controller</p> <p>l HandleRequest () method to implement the Controller interface to process the request</p> <p>l Add logging here to check if you are executed</p> <p>l HandleRequest () Method Returned ModelandView not specified Model, so redirect to the specified view hello.jsp</p> <p>Import Org.SpringFramework.Web.Servlet.mvc.controller;</p> <p>Import org.springframework.Web.Servlet.ModelandView;</p> <p>Import javax.servlet.servletException;</p> <p>Import javax.servlet.http.httpservletRequest;</p> <p>Import javax.servlet.http.httpservletResponse;</p> <p>Import java.io.ioException;</p> <p>Import org.apache.commons.logging.log;</p> <p>Import org.apache.commons.logging.logfactory;</p> <p>Public Class SpringAppController Implements Controller {</p> <p>/ ** Logger for this class and subclasses * /</p> <p>Protected final log logger = logfactory.getlog (getclass ());</p> <p>Public ModlandView HandleRequest (httpservletRequest Request, HttpservletResponse Response)</p> <p>Throws servletexception, IOException {Logger.info ("SpringAppController - Returning Hello View");</p> <p>Return New ModelandView ("Hello.jsp");</p> <p>}</p> <p>}</p> <p>(10) Creating view hello.jsp</p> <p><html></p> <p><head> <title> eXample :: spring application </ title> </ head></p> <p><body></p> <p><H1> Hello - Spring Application </ h1></p> <p><p> greetings. </ p></p> <p></ body></p> <p></ html></p> <p>(11) Copy and modify Log4j.properties</p> <p>l Springframework uses the log4j record log, so import Samples / PetClinic / War / Web-INF / CLASSES / LOG4J.PROPERTIES file to the SpringApp / Web-INF / CLASSES directory of SpringApp project</p> <p>l Modify log4j.properties, specify the log output file:</p> <p># For jboss: Avoid to setup log4j outside $ jboss_home / server / default / deploy / log4j.xml!</p> <p># For all other servers: Comment Out the log4j listener in web.xml too.</p> <p># log4j.rootlogger = info, stdout, logfile</p> <p>Log4j.appender.stdout = org.apache.log4j.consoleAppender</p> <p>Log4j.Appender.stdout.Layout = Org.apache.log4j.patternlayout</p> <p>Log4j.Appender.stdout.Layout.conversionPattern =% D% p [% C] - <% m>% N</p> <p>Log4j.Appender.logfile = org.apache.log4j.rollingfileappender</p> <p>Log4j.Appender.logfile.file = $ {Springapp.Root} /web-inf/springapp.log</p> <p>Log4j.Appender.logfile.maxfilesize = 512KB</p> <p># Keep Three Backup Files.</p> <p>Log4j.Appender.logfile.maxbackupindex = 3</p> <p># Pattern to output: Date Priority [category] - Message</p> <p>Log4j.Appender.logfile.Layout = org.apache.log4j.patternlayout</p> <p>Log4j.Appender.logfile.Layout.conversionPattern =% D% p [% C] -% M% N</p> <p>L (Translator: Tips below when deploying, log4j does not work properly, looking high of high finger)</p> <p>Log4j: warn no appenders could be found for logger (org.apache.catalina.session.managerbase).</p> <p>Log4J: Warn Please Initialize The log4j system prot in.</p> <p>(12) Re-deploy the application L Re-deploy the application via the previous method</p> <p>l (Translator: Before re-deployment, you need to remove the SpringApp directory under Tomcat's WebApps directory, otherwise Tomcat will not extract the new WAR file. What is the problem?)</p> <p>l Enter http: // localhost: 8888 / springApp / hello.htm, you can display the content of Hello.jsp.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-128280.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="128280" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.048</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'gon9jGgCWVmxuRLh8myum5uQru_2BLp7ZGdv_2B1lRJMSd2vkcLUCATcHlufBCAGVQF3rtI1dF7MKOvP1aNVfc2jpA_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>