Spring Web Mvc with Annotation

Required Jars:

  • spring-core.jar
  • spring-beans.jar
  • spring-web.jar
  • spring-webmvc.jar
  • spring-context.jar


Configuration on WEB.XML :

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>App Name</display-name>

    <servlet>
        <servlet-name>app-name</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>app-name</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>

</web-app>


Configuration Servlet Context “app-name-servlet.xml”, put it on WEB-INF/:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!--Enable Spring Annotation-->
    <context:annotation-config/>
    <context:component-scan base-package="com.springmvc"/>

    <!--Resolver-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="cache" value="true"/>
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>


The Controller Class:

package com.springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TheController{

    @RequestMapping(value = "/helloworld", method = RequestMethod.GET)
    public String helloWorld(){
        return "hello_world";
    }
}


The page “hello_world.jsp”, put it on WEB-INF/pages/:

<html>
	<head>
	<title>Test Spring Web Mvc with Annotation</title>
	</head>

	<body>
		Hello World
	</body>
</html>

Convert to war and deploy on your tomcat and then access it on your browser:
http://yourhost/app-name/service/helloworld

Other things that we can do on Controller :

package com.springmvc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class TheController{
@RequestMapping(value = "/helloworld", method = RequestMethod.GET)
	public String helloWorld() {
		return "hello_world";
	}

	/* USING HttpServletResponse */
	@RequestMapping(value = "/servletresponse", method = RequestMethod.GET)
	public void testServletResponse(final HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		out.print("Hello, this use response print writer");
	}

	/* USING HttpServletRequest */
	@RequestMapping(value = "/servletrequest", method = RequestMethod.GET)
	public void testServletRequest(final HttpServletRequest request, final HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();

		String name = request.getParameter("name");

		out.print("Hello "+name+", test using HttpServletRequest");
	}

	/* USING @SpringParam Required*/
	@RequestMapping(value = "/spring-param-required", method = RequestMethod.GET)
	public void testSpringParamRequired(@RequestParam(value="name") String name, final HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
		out.print("Hello "+name+", test using @SpringParam Required");
	}

	/* USING @SpringParam NOT Required*/
	@RequestMapping(value = "/spring-param-notrequired", method = RequestMethod.GET)
	public void testSpringParamNotRequired(@RequestParam(value="name", required=false) String name, final HttpServletResponse response) throws IOException{
		PrintWriter out = response.getWriter();
		out.print("Hello "+name+", test using @SpringParam Not Required");
	}

	/* USING MODEL and EL
	 * note: you need to create "test_model.jsp" on WEB-INF/pages:
	 <html>
		<head>
			<title>Test Spring Web Mvc with Annotation</title>
		</head>

		<body>
			Hello ${thename}, this page is using model
		</body>
	 </html>
	 */
	@RequestMapping(value = "/usemodel", method = RequestMethod.GET)
	public String testModel(Model model, @RequestParam(value="name") String name){
		model.addAttribute("thename", name);
		return "test_model";
	}

	/* USING MODEL and JSTL
	 * note: you need to create "test_model_jstl.jsp" on WEB-INF/pages:
	 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	 <html>
		<head>
			<title>Test Spring Web Mvc with Annotation</title>
		</head>

		<body>
			Hello ${thename}, this page is using model <br>
			And this is some list generated on controller:
			<c:forEach var="values" items="${thelist}">
		 		<br> ${values}
			</c:forEach>
		</body>
	 </html>
	 */
	@RequestMapping(value = "/usemodel-n-jstl", method = RequestMethod.GET)
	public String testModelnJstl(Model model, @RequestParam(value="name") String name){
		List list = new ArrayList();
		list.add("value 01");
		list.add("value 02");
		list.add("value 03");
		model.addAttribute("thelist", list);
		model.addAttribute("thename", name);
		return "test_model_jstl";
	}
}


Similar Posts:

Bookmark:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • description
  • LinkedIn
  • Slashdot
  • Technorati
  • TwitThis
  • Yahoo! Buzz

2 Comments

limonMarch 11th, 2010 at 2:01 pm

how to encrypt url using spring mvc ? please helm me.
Thanks for very nice post.

Bette CelliMay 16th, 2010 at 9:35 am

Great grit as usual…

Leave a comment

Your comment

Spam Protection by WP-SpamFree