Home Tutorial Spring Spring Annotation Tutorial

 
 

Share on Google+Share on Google+
Spring Annotation Tutorial
Posted on: November 19, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn about the spring annotation tutorial

Spring Annotation Tutorial

In Spring Framework you can use annotations to write to java classes such as Controller easily. For example making any java class as Controller you can simply write @Controller before the class declaration

@Controller
public class AnnotationController {

You can use RequestMapping annotation before any method or class declartion to handle particular URL request.

@RequestMapping("/loginForm.html")
	public String loadForm(Map model) {
		return "loginForm";
	}

You can also put data in model using annotation as

@ModelAttribute("countryList")
	public List loadCounties() {
		return myService.loadCountry();
	}

You can add attributes in session as

@SessionAttributes("loginForm")

You can autowire as

@Autowired
	public void setMyService(MyService myService) {
		this.myService = myService;
	}

An example of Annotion is given below, please consider the example

loginForm.jsp

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:form commandName="loginForm" name="loginForm" action="display.html">
<table align="center" cellpadding="10" cellspacing="10" bgcolor="#D6D6C2">
<tr>
<td><form:label path="loginID">Your Name</form:label></td>
<td><form:input path="loginID"></form:input></td>
</tr>
<tr>
<td><form:label path="password">Password</form:label></td>
<td><form:password path="password"></form:password></td>

</tr>
<tr>
<td><form:label path="countryName">Your Location</form:label></td>
<td><form:select path="countryName">
<form:option value="0" label="Please Select"></form:option>
<form:options items="${countryList}" itemValue="name" itemLabel="name" />
</form:select></td>
</tr>
<tr>
<td colspan="2" align="Center"><input type="submit" value="Submit" /></td>
</tr>
</table>

</form:form>

Write the form model class as

LoginForm.java

package net.roseindia.model;

import java.util.List;

public class LoginForm {
	private String loginID;
	private String password;
	private List countryList;
	private String countryName;

	public String getLoginID() {
		return loginID;
	}

	public void setLoginID(String loginID) {
		this.loginID = loginID;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List getCountryList() {
		return countryList;
	}

	public void setCountryList(List countryList) {
		this.countryList = countryList;
	}

	public String getCountryName() {
		return countryName;
	}

	public void setCountryName(String countryName) {
		this.countryName = countryName;
	}

}

The Form Controller class by using annotation is given below

AnnotationController.java

package net.roseindia.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import net.roseindia.model.LoginForm;
import net.roseindia.service.MyService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class AnnotationController {

	private MyService myService;

	@Autowired
	public void setMyService(MyService myService) {
		this.myService = myService;
	}

	@RequestMapping("/loginForm.html")
	public String loadForm(Map model) {
		return "loginForm";
	}

	@ModelAttribute("loginForm")
	public LoginForm returnModel() {
		return new LoginForm();
	}

	@ModelAttribute("countryList")
	public List loadCounties() {
		return myService.loadCountry();
	}

	@RequestMapping("/display.html")
	public String responseForm(Map model, @Valid LoginForm loginForm) {
		if (loginForm.getLoginID() != "" && loginForm.getPassword() != ""){
			return "displayDetail";
		}
		return "loginForm";
	}
}

Service class for the above application would be

MyService.java

package net.roseindia.service;

import java.util.ArrayList;
import java.util.List;

import net.roseindia.model.Country;

public class MyService {
	public List loadCountry() {
		List countryList = new ArrayList();

		countryList.add(new Country(1, "India", "Description Of India"));
		countryList.add(new Country(2, "China", "Description Of China"));
		countryList.add(new Country(3, "England", "Description Of England"));
		countryList.add(new Country(4, "Srilanka", "Description Of Srilanka"));
		countryList.add(new Country(5, "Kenya", "Description Of Kenya"));

		return countryList;
	}
}

Country Mopdel for the above class is as

SampleInterfaceImp.java

package net.roseindia.model;

public class Country {
	private int id;
	private String name;
	private String description;

	public Country() {

	}

	public Country(int id, String name, String description) {
		this.id = id;
		this.name = name;
		this.description = description;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
}


When you run this application it will display message as shown below:


 

Download Select Source Code

Advertisement

Related Tags for Spring Annotation Tutorial:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 19, 2011

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com