Tuesday, April 8, 2014

Spring MVC Labs

http://saltnlight5.blogspot.com/2013/10/getting-started-with-annotation-based.html  -- gives an example of how to take advantage of Servlet 3/Tomcat 7 web.xml-free setup. To boot also shows how to use the http://tomcat.apache.org/maven-plugin-2.0/tomcat7-maven-plugin/usage.html Tomcat 7 Maven plugin.
Alls I did was create a new Maven project in Eclipse Kepler EE version, naming the groupId mvcmvnsprng and the artifact id mvcmvn. Then created a source folder under Java Resources named "javasrc" in which I put the mentioned packages and java files. Also updated the pom.xml and right click project and click Validate. I suppose you could also Maven, download sources or just wait for it happen automatically. index.jsp was already there with hello world in it. then I changed to cmd to root folder of project containing the pom.xml and typed: mvn org.apache.tomcat.maven:tomcat7-maven-plugin:run
That downloaded the tomcat plugin and started the web server! Loading localhost:8080/mvcmvn/ I could see "Hello World!" it was that easy.
Also did the follow up which showed how to connect the same to embedded H2 database via jdbctemplate http://java.dzone.com/articles/getting-started-spring-jdbc

The next step was to add logging. Taking hints from several tutorials I ended up just right-clicking the project and choosing maven add dependency twice. Once by typing commons-logging and choosing 1.1.1. Then for log4j by typing "log4j" and choosing 1.2.17. It added a "bundle" element which cause errors so I deleted it. Then I added the log4j.properties copied from http://www.beingjavaguys.com/2013/05/logging-in-spring-framework-using-log4j_8.html  to src/main/resources (so its in root of a classpath). That was it. Didn't need to use Logger.getLogger or import org.apache.log4j.Logger because apparently it's plugged in automatically via spring and appache commons logging. So the LOG.info command in PingService.java worked; it outputed the message in the logingFile.log and to the batch output.

Word of caution: Don't even think of doing Spring MVC in JBoss. All indicators are that Spring was built with Tomcat in mind, JBoss was built with JEE in mind, and never the twain should cross paths (unless you want to spend all-nighters trying to fit a square peg (Spring MVC 3) in a round hole (JBoss 7) like I just did.

No comments:

Post a Comment