The article represents steps on what you need to do to get your first Spring MVC Hello World web application project on Google App Engine (GAE). As a pre-requisite, we recommend you to check our earlier article on How to get started with Google App Engine. The article would help you to quickly get your started with GAE based web development using Eclipse IDE.
We shall work with our existing Non Spring MVC project (check this page) and convert it into Spring MVC based web application.
Step 1: Spring MVC Libraries
Get Spring MVC libraries within folder (war/WEB-INF/lib). Also, do not forget to get following two runtime dependent libraries:
- commons-logging-1.1.3.jar
- com.springsource.org.aopalliance-1.0.0.jar
Step 2: Web.XML Configuration
Create following entry in web.xml.
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Step 3: Spring-servlet.xml Configuration
Create a spring-servlet.xml and place it within war/WEB-INF folder. The content of spring-servlet.xml could be as simple as following:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<!-- Make a NOTE of component scan. Create HelloController in package "com.orgname.hello" -->
<context:component-scan base-package="com.orgname.hello" />
<!-- Configuration defining views files -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Step 4: Creating & Configuring Views & Controllers
- Create a “views” folder within war/WEB-INF folder and place all the JSP files within this “views” folder. Create a file index.jsp with “hello world” text. All the request to views shall route through Controller. The file whose content will be displayed will be index.jsp. The way it works is that the request is made to dispatcher servlet which based on the context path, “/” in this case, delegates to the controller (HelloController as shown above) having RequestMapping for “/”. The method then redirects the request to view-resolver with “index” as view name. The view resolver then sends the request appropriately to index.jsp. Thus, we shall later see the RequestMapping entries within Controller class.
- Create a controller class within your custom package (com.orgname.hello) within src folder. The package name (com.orgname.hello) MUST be same as that mentioned inside sprint-servlet.xml within attribute “context:component-scan”. Look at previous step for sample. Create RequestMapping entries for all the views within the controller such as that shown in the code below.If you shall see the compile-time error, add the Spring-related libraries by right clicking on project, then, Properties and adding external libraries to Java Build Path/Libraries tab.
@Controller
public class HelloController {
//
// Create a file, index.jsp within folder war/WEB-INF/views.
// This index.jsp would showup at 127.0.0.1:8888.
//
@RequestMapping(value = "/", method = RequestMethod.GET)
public ModelAndView index( ModelMap model ) {
return new ModelAndView("index");
}
//
// Create a file, java_functional_interface.jsp within folder war/WEB-INF/views.
// This file would be accessed when the URL accessed from website is http://127.0.0.1:8888/java_functional_interface.html
//
@RequestMapping(value = "/java_functional_interface.html", method = RequestMethod.GET)
public ModelAndView getJavaFuncInterface( ModelMap model ) {
return new ModelAndView("java_functional_interface");
}
//
// Create a file, aboutus.jsp within folder war/WEB-INF/views.
// This file would be accessed when the URL accessed from website is http://127.0.0.1:8888/aboutus.html
//
@RequestMapping(value = "/aboutus.html", method = RequestMethod.GET)
public ModelAndView getAboutUs( ModelMap model ) {
return new ModelAndView("aboutus");
}
//
// Create a file, hello.jsp within folder war/WEB-INF/views.
// This file would be accessed when the URL accessed from website is http://127.0.0.1:8888/springmvc/helloworld
//
@RequestMapping(value = "/springmvc/helloworld", method = RequestMethod.GET)
public ModelAndView newUser( ModelMap model ) {
return new ModelAndView("hello");
}
}
Step 5: Run & Deploy on GAE
- Run the Web Application by right-clicking on project and then clicking on Run As…Web Application. Make sure the server starts correctly. Then, check the URL, http://127.0.0.1:8888/. Check the URL http://127.0.0.1:8888/springmvc/helloworld and this should display the content of war/WEB-INF/views/hello.jsp
- Once all is OK at step 6, you are all set. Right-click on project and go to Google…Deploy to App Engine and that is it. Once deployed, check your web application online at URL http://app-id.appspot.com. Also, check http://app-id.appspot.com/springmvc/helloworld. As a reference, check the URL http://quick-code-samples.appspot.com/springmvc/helloworld.
- Agentic Reasoning Design Patterns in AI: Examples - October 18, 2024
- LLMs for Adaptive Learning & Personalized Education - October 8, 2024
- Sparse Mixture of Experts (MoE) Models: Examples - October 6, 2024
Hi,
I was curious if you could state what the “Spring MVC libraries” are. I have been trying to get this to work with no luck.
Hi friend. I try pass this simple app to spring 4.x with no success, do you think can help me with that?.
https://github.com/romelgomez/jqtree-spring-mvc-gae-example
demo: http://jqtree-spring-mvc-gae-example.appspot.com/
Best wishes.
How could I achieve this with out returning views but instead just json data…I can get a spring boot app running gae locally but when I deploy it I have a problem hitting rest end points 🙁