TechnoBuzz

A Techno Blog, mainly about Java

Spring MVC Refresher II

web.xml

browsers currently only support GET and POST, a common technique – is to use a normal POST with an additional hidden form field  (_method) to pass the “real” HTTP method.

< filter >
< filter-name >httpMethodFilter </  filter-name >
< filter-class >org.springframework.web.filter.HiddenHttpMethodFilter < /  filter-class >
< / filter >

accounts-servlet-config.xml:

< context:component-scan base-package=”accounts.web” />

< mvc:annotation-driven/ >

An interceptor which keeps a hibernate session open to allow lazy loading of backing object
< mvc:interceptors >
< bean class=”org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor” >
< property name=”sessionFactory” ref=”sessionFactory”/ >
< /bean >
< /mvc:interceptors >
< bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver” >
< property name=”prefix” value=”/WEB-INF/views/”/ >
< property name=”suffix” value=”.jsp”/ >
< /bean >

When you use a ContentNegotiatingViewResolver your web controllers return ModelAndViews or view names and the ContentNegotiatingViewResolver will, based on various criteria, choose the right data representation strategy, Here for JSON Use Jackson Java JSON-processor

< bean class=”org.springframework.web.servlet.view.ContentNegotiatingViewResolver” >
< property name=”mediaTypes” >
< map >
< entry key=”json” value=”application/json”/ >
< /map >
< /property >
< property name=”defaultViews” >
< bean class=”org.springframework.web.servlet.view.json.MappingJacksonJsonView”/ >
< /property >
< /bean >

Note: if had not have used component scanning would have had to wire up the controller

A Spring MVC @Controller

imports:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

package accounts.web;

@Controller
public class AccountController {

private AccountManager accountManager;

@ Autowired
public AccountController(AccountManager accountManager) {
this.accountManager = accountManager;
}

The method does not explicitly select a view name because the default
view name selected by Spring MVC matches to the incoming URL accountDetails.jsp
@ param id the id of the account
@ param model the “implicit” model created by Spring MVC

@ RequestMapping(value=”/accounts/{accountId}”,  method=RequestMethod.GET)
public void accountDetails(@PathVariable(“accountId”) int id, Model model) {
model.addAttribute(“account”, accountManager.getAccount(id)); return “accountDetails”;
}

The method does not explicitly select a view name because the default
view name selected by Spring MVC matches to the incoming URL accountSummary.jsp

@ param model the “implicit” model created by Spring MVC

@ RequestMapping(value=”/accounts”, method=RequestMethod.GET)
public void accountSummary(Model model) {
model.addAttribute(“accounts”, accountManager.getAllAccounts());return “accountSummary”;
}

index.html:

< a href=”app/accounts” >View Account Summary< /a >

http://localhost:8080/ss/app/accounts

Then on the listing page you click on one of the entries to get details

http://localhost:8080/ss/app/accounts/6

This uses the Path Variable

@ RequestMapping(value=”/accounts/{accountId}”,  method=RequestMethod.GET)
public void accountDetails(@PathVariable(“accountId”) int id, Model model) {

September 16, 2012 Posted by | IoC | Leave a comment