viernes, 18 de julio de 2014

Creating a simple MVC App with Spring 4.0 and WebMvcConfigurerAdapter

1. Download STS(Spring Tool Suite) from http://spring.io/tools/sts

2. Create a new Spring Starter Project

3. Check the Web Options and enter a group(org.demo), artefact(exampleweb), and package name(org.demo.exampleweb).


4. Add the following dependences to pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

Note: you may add the dependencies with the use of the Dependeces tab such as

For tomcat-embed-jasper:

For jstl


5. Add ViewResolver properties to application.properties in the src/main/resources directory
##View Resolver Configuration
spring.view.prefix=/WEB-INF/jsp
spring.view.suffix=.jsp

6. Create the following directories in your projects


7. Add a new jsp page named home.jsp. Use the following code for the JSP page
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

This is an example


8. Create a new class named MVCConfiguration in the package org.demo.exampleweb.config
and extend WebMvcConfigurerAdapter class


















9. Override addViewControllers(ViewControllerRegistry registry) and add the following code


package org.demo.exampleweb.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MVCConfiguration extends WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}

}

10. Run your App with Spring Boot App


11. Open a browser and enter localhost:8080 as URL




















Creating a simple MVC App with Spring 4.0 and Controller Annotation

1. Download STS(Spring Tool Suite) from http://spring.io/tools/sts

2. Create a new Spring Starter Project

3. Check the Web Options and enter a group(org.demo), artefact(exampleweb), and package name(org.demo.exampleweb).


4. Add the following dependences to pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.54</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>

Note: you may add the dependencies with the use of the Dependeces tab such as

For tomcat-embed-jasper:

For jstl


5. Add ViewResolver properties to application.properties in the src/main/resources directory
##View Resolver Configuration
spring.view.prefix=/WEB-INF/jsp
spring.view.suffix=.jsp

6. Create the following directories in your projects


7. Add a new jsp page named home.jsp. Use the following code for the JSP page
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

This is an example


8.Create a new class named RootController in the package org.demo.exampleweb.controllers

9. Add the following code

package org.demo.exampleweb.controllers;

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

@Controller
public class RootController {
@RequestMapping("/")
public String showHomePage(){
return "home";
}
}

10. Run your App with Spring Boot App

11. Open a browser and enter localhost:8080 as URL