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:
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
@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
What revolutionary technologies and industries will define the future of business in 2025? As we…
For data scientists and machine learning researchers, 2024 has been a landmark year in AI…
ChatGPT Canvas is a cutting-edge, user-friendly platform that simplifies content creation and elevates collaboration. Whether…
Knowing when to use the LLM such as the ChatGPT O1 model is key to…
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
View Comments
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 :(