[Spring] Simple tutorial
How to start with java spring framework
prerequisites
- Maven (v 3)
- configured connection to Nexus;
2.
configured connection via proxy;
Maven settings.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
c:\REPO\MVN\main\
optional
true
http
10.0.5.232
8080
10.*|112.*
nexus
*
Nordea Nexus repo
http://10.0.52.97:9001/nexus/content/groups/nordea-repos/
nexus-plug
*
Nordea Nexus repo
http://10.0.52.97:9001/nexus/content/groups/nordea-repos/
- optionally you can set Environment/User variable
parameter for M2_REPO (used by eclipse):

- and also MAVEN_HOME param:

- Eclipse environment with Maven plugin (m2e - Maven
Integration for Eclipse) or VMware Spring Tool Suite (based on Eclipse);
- Configured Local repository (don't know why
Remote repo doesn't work
) for
Maven Archetype:
- go to url: http://repo1.maven.org/maven2/archetype-catalog.xml
- store xml on local disk
- configure the Archetypes on Maven menu under project
Preferences:
- Catalog file: /archetype-catalog.xml
- Description:

creating
project
- Create new Maven project structure


- find Archetype with spring-mvc pattern. Choose co.ntier
(GroupId) with artifactId: spring-mvc-archetype

- Provide Archetype parameters

- After confirming Finish button, you'll get the new
project created:

Runing project
§
in the controller code you can see that main, root path of project
request is set to test() method:
@RequestMapping(value="/")
HomeController.java
package com.nordea.spring.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@RequestMapping(value="/")
public ModelAndView test(HttpServletResponse response) throws IOException{
return new ModelAndView("home");
}
}
- method test() returns the "home" as a view
and model which should be refilled (in this case model is empty);
§
going to the MvcConfiguration class, where there is configured the
views prefix and suffix, you can see that "home" value of the
HomeController.test method result will give
the "/WEB-INF/views/home.jsp" path;
MvcConfiguration.java
package com.nordea.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@ComponentScan(basePackages="com.nordea.spring")
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
}
the same configuration can be done with use of xml config file.