Pages

Tuesday, June 22, 2010

Using Spring Framework and Google App Engine for a Web Application.

Step by Step Procedure for the Hello World Application:

  • Install the Google Plugin
  • Create a new Web application and untick GWT (Google Web Toolkit)
  • Download the latest Spring Release
  • Add the Jars to the folder WEB-INF/lib

The following JAR files should be present in the folder:

Untitled

Make the Following Changes to the web.xml file

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/a/*</url-pattern>
    </servlet-mapping>
</web-app>

Create a file dispatcher-servler.xml in the WEB-INF Directory

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd"
>

<context:component-scan base-package="ipathshala.controllers" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

</beans>



HelloController.java

package ipathshala.controllers;

import javax.servlet.http.HttpServletRequest;

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

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Controller
@RequestMapping("/hello")
public class HelloController {

private UserService userService = UserServiceFactory.getUserService();

@RequestMapping(method = RequestMethod.GET)
public String hello(HttpServletRequest request, ModelMap model)
{
String url = request.getRequestURI();
String message;

if(request.getUserPrincipal() != null)
{
message = new StringBuilder()
.append("Hello, ")
.append(request.getUserPrincipal().getName())
.append("! You can <a href=\"")
.append(userService.createLogoutURL(url))
.append("\">Sign Out</a>.").toString();
}else{
message = new StringBuilder()
.append("Please ")
.append("<a href=\"")
.append(userService.createLoginURL(url))
.append("\">Sign In</a>.").toString();
}

model.addAttribute("message", message);

return "hello/hello";
}

}
This line above @RequestMapping("/hello") maps all the requests /a/hello to this controller after being examined by the DispatcherServlet.

Create a Folder views under WEB-INF and two Sub folders hello and common.


hello/hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ include file="/WEB-INF/views/common/includes.jsp" %>
<p>${message}</p>



common/includes.jsp

 
<%@ page session="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>





You should now be able to click “Debug” and run the application by visiting “localhost:8888/a/hello”.


References:



 

 

No comments:

Post a Comment