background preloader

Spring 3

Facebook Twitter

Spring 3 MVC, Ajax and jQuery Magic (or better: simplicity) « maxheapsize.com. I’m playing around with some web frameworks lately and to see what’s in store with Spring 3 MVC (never did too much with it) I gave it a try to see how it handles Ajax. According to ajax simplification announcement it should be possible to get up and running in (almost) no time. We will do a simple web application which will show the current time via Ajax. The directory layout (using maven) should look like this: The web.xml holds no secrets. The Dispatcher servlet is defined and it should react on everything *.html. Since we named our Servlet ‘spring’ we need to add a servlet configuration for that. It uses the component scanning feature of spring and instructs spring to check the package ‘com.maxheapsize.springmvc3.controller’ for controllers.

In WEB-INF/jsp/hello.jsp we define a jQuery snippet for the Ajax request and provide a button which we should push if we want to know the time. How does Spring now know which classes or methods to call? Overall this is a pretty nice integration. Create JSON response for AJAX request in spring 3.0 « Ralf Wehner's Blog. @RequestMapping(value = "/rest/playground/google-barchart", method = RequestMethod.GET) public @ResponseBody GoogleJsonResponse getJsonDataForBarChart(HttpServletResponse response, HttpServletRequest request) { response.setStatus(HttpServletResponse.SC_OK); GoogleJsonResponse ret = new GoogleJsonResponse(); ret.addColl("string", "Year"); ret.addColl("number", "Austria"); ret.addColl("number", "Bulgaria"); ret.addColl("number", "Denmark"); ret.addColl("number", "Greece"); ret.addRow(new GoogleJsonResponse.Cell("2003"), new GoogleJsonResponse.Cell(1336060), new GoogleJsonResponse.Cell(400361), new GoogleJsonResponse.Cell(1001582), new GoogleJsonResponse.Cell( ret.addRow(new GoogleJsonResponse.Cell("2004"), new GoogleJsonResponse.Cell(1538156), new GoogleJsonResponse.Cell(366849), new GoogleJsonResponse.Cell(1119450), new GoogleJsonResponse.Cell( return ret;

Spring Project Annotations. Proxy this method calls. One of the bigest issues of spring aop is, that when you use its proxies for adding some aop functionality (like for example transactions or security), your calls to a method withing the same bean won't trigger the advised aop functionality. So if you have a Service, that has two methods, where method A() HAS NO @Transactional annotation and method B() HAS a @Transactional annotation and non transactional method A() calls during its execution transactional method B() then spring won't start any transaction. This is because the spring proxy will redirect its call for A() to the service object but the call to B() won't be executed on the proxy (that knows how and when to start the transaction) but instead on the actual service object that has not functional code to start the transaction (only a @Transactional annotation on the method B()).

After this you change your code from: into And that is the whole configuration. This is the code of the annotation: EasyMock. Spring Framework provides an easy way to unit test components in isolation and EasyMock provides a quick way to create mock objects with very little effort. Combining them together, Spring can be used to create the mock objects so that beans that use auto wiring to inject dependencies can be tested without having to modify the classes or create complex context configuration files. Creating mock objects using spring is simple, here is a sample beans.xml: This creates a mock object from the given interface. The primary is used to indicate that it should be used for autowiring which is useful when component scanning is used together with @Component tags. When you want to refer to the actual implementation you would use @Qualifier("someService").

If interfaces aren't used then the following beans.xml is used. Performing the test is relatively simple and with autowiring configuration is minimal. Spring In-depth, In Context. JTA & XA Transactions in Spring. While it's common to use the Java Transaction API and the XA protocol for distributed transactions in Spring, you do have other options. The optimum implementation depends on the types of resources your application uses and the trade-offs you're willing to make between performance, safety, reliability, and data integrity. In this JavaWorld feature, SpringSource's David Syer guides you through seven patterns for distributed transactions in Spring applications, three of them with XA and four without. Level: Intermediate The Spring Framework's support for the Java Transaction API (JTA) enables applications to use distributed transactions and the XA protocol without running in a Java EE container.

Even with this support, however, XA is expensive and can be unreliable or cumbersome to administrate. Note that only the first three patterns involve XA, and those might not be available or acceptable on performance grounds. Distributed transactions and atomicity Full XA with 2PC Listing 1.

Tomcat setup

Spring MVC. Reference. Tutorials.