SpringMVC steps
SpringMVC steps
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.javaguides.springmvc</groupId>
<artifactId>springmvc5-helloworld-exmaple</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc5-helloworld-exmaple Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<!--
https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
package net.javaguides.springmvc.helloworld.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "net.javaguides.springmvc.helloworld" })
//controller,model
public class AppConfig {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new
InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
package net.javaguides.springmvc.helloworld.config;
import
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherSer
vletInitializer;
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Controller:
package net.javaguides.springmvc.helloworld.controller;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import net.javaguides.springmvc.helloworld.model.HelloWorld;
@Controller
public class HelloWorldController {
@RequestMapping("/helloworld")
public String handler(Model model) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Welcome to UST SpringMVC session");
helloWorld.setDateTime(LocalDateTime.now().plusHours(1).toString());
model.addAttribute("helloWorld", helloWorld);
return "helloworld";
}
@RequestMapping("/home")
public String message() {
return "home";
}
}
Model:
package net.javaguides.springmvc.helloworld.model;
public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
}
Views:
PathVariable,REquestParam:
package net.javaguides.springmvc.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class ProductController {
@RequestMapping("/product")
public String message(@RequestParam("id") Integer id) {
System.out.println("request param: "+id);
return "home";
}
@RequestMapping("/product1/{id1}")
public String message1(@PathVariable("id1") Integer id) {
System.out.println("path param :"+id);
return "home";
}
package net.javaguides.springmvc.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class ProductController {
@RequestMapping("/product")
public String message(@RequestParam("id") Integer id) {
System.out.println("request param: "+id);
return "home";
}
@RequestMapping("/product1/{id1}")
public String message1(@PathVariable("id1") Integer id) {
System.out.println("path param :"+id);
return "home";
}
@GetMapping("/read")
public String get() {
System.out.println(" get- for read()");
return "home";
}
@PostMapping("/add")
public String add() {
System.out.println(" post- for add()");
return "home";
}
@PutMapping("/update")
public String update() {
System.out.println(" put- for update()");
return "home";
}
@DeleteMapping("/delete")
public String delete() {
System.out.println(" delete- for delete()");
return "home";
}