JAVA-2416 Move/rename module spring-dispatcher-servlet
- Moved all the contents from spring-dispatcher-servlet to spring-mvc-basics
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.model;
|
||||
|
||||
|
||||
public class User {
|
||||
private long id;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
|
||||
public User(long id, String firstName, String lastName) {
|
||||
this.id = id;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
|
||||
public User fetchUserByFirstName(String firstName) {
|
||||
return new User(1, firstName, "Everyperson");
|
||||
}
|
||||
|
||||
public User exampleUser() {
|
||||
return new User(1, "Example", "Everyperson");
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,22 @@
|
||||
package com.baeldung.spring.web.config;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.ui.context.support.ResourceBundleThemeSource;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.resource.PathResourceResolver;
|
||||
import org.springframework.web.servlet.theme.CookieThemeResolver;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.JstlView;
|
||||
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
|
||||
@@ -24,6 +33,14 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
.setViewName("index");
|
||||
}
|
||||
|
||||
/** Multipart file uploading configuratioin */
|
||||
@Bean
|
||||
public CommonsMultipartResolver multipartResolver() throws IOException {
|
||||
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
|
||||
resolver.setMaxUploadSize(10000000);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver viewResolver() {
|
||||
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
@@ -34,6 +51,47 @@ public class WebConfig implements WebMvcConfigurer {
|
||||
return bean;
|
||||
}
|
||||
|
||||
/** Static resource locations including themes*/
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**/*")
|
||||
.addResourceLocations("/", "/resources/")
|
||||
.setCachePeriod(3600)
|
||||
.resourceChain(true)
|
||||
.addResolver(new PathResourceResolver());
|
||||
}
|
||||
|
||||
/** BEGIN theme configuration */
|
||||
@Bean
|
||||
public ResourceBundleThemeSource themeSource() {
|
||||
ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
|
||||
themeSource.setDefaultEncoding("UTF-8");
|
||||
themeSource.setBasenamePrefix("themes.");
|
||||
return themeSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CookieThemeResolver themeResolver() {
|
||||
CookieThemeResolver resolver = new CookieThemeResolver();
|
||||
resolver.setDefaultThemeName("default");
|
||||
resolver.setCookieName("example-theme-cookie");
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ThemeChangeInterceptor themeChangeInterceptor() {
|
||||
ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor();
|
||||
interceptor.setParamName("theme");
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(themeChangeInterceptor());
|
||||
}
|
||||
|
||||
/** END theme configuration */
|
||||
|
||||
@Bean
|
||||
public ViewResolver resourceBundleViewResolver() {
|
||||
final ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
@Controller
|
||||
public class MultipartController {
|
||||
|
||||
@Autowired
|
||||
ServletContext context;
|
||||
|
||||
@RequestMapping(value = "/upload", method = RequestMethod.POST)
|
||||
public ModelAndView FileuploadController(@RequestParam("file") MultipartFile file) {
|
||||
ModelAndView modelAndView = new ModelAndView("index");
|
||||
try {
|
||||
InputStream in = file.getInputStream();
|
||||
String path = new File(".").getAbsolutePath();
|
||||
FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + "/uploads/" + file.getOriginalFilename());
|
||||
try {
|
||||
int ch;
|
||||
while ((ch = in.read()) != -1) {
|
||||
f.write(ch);
|
||||
}
|
||||
modelAndView.getModel().put("message", "File uploaded successfully!");
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception uploading multipart: " + e);
|
||||
} finally {
|
||||
f.flush();
|
||||
f.close();
|
||||
in.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception uploading multipart: " + e);
|
||||
}
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import com.baeldung.services.UserService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@RequestMapping(value = "/example", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public User fetchUserExample() {
|
||||
return userService.exampleUser();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/name", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public User fetchUserByFirstName(@RequestParam(value = "firstName") String firstName) {
|
||||
return userService.fetchUserByFirstName(firstName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import com.baeldung.model.User;
|
||||
import com.baeldung.services.UserService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/rest/user")
|
||||
public class UserRestController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@RequestMapping(value = "/example", method = RequestMethod.GET)
|
||||
public User fetchUserExample() {
|
||||
return userService.exampleUser();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/name", method = RequestMethod.GET)
|
||||
public User fetchUserByFirstName(@RequestParam(value = "firstName") String firstName) {
|
||||
return userService.fetchUserByFirstName(firstName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
styleSheet=/resources/css/default.css
|
||||
@@ -0,0 +1 @@
|
||||
styleSheet=/resources/css/example.css
|
||||
Reference in New Issue
Block a user