Skip to content

Commit 07c0e84

Browse files
joshiparthinpivovarit
authored andcommitted
Initial commit for merging modules spring-mvc-forms into (eugenp#1222)
spring-mvc-simple.
1 parent b5ef48a commit 07c0e84

22 files changed

Lines changed: 661 additions & 80 deletions

‎spring-mvc-forms/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
### Relevant Articles
44
- [MaxUploadSizeExceededException in Spring](http://www.baeldung.com/spring-maxuploadsizeexceeded)
5+
- [Getting Started with Forms in Spring MVC](http://www.baeldung.com/spring-mvc-form-tutorial)

‎spring-mvc-simple/pom.xml‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<springframework.version>4.3.4.RELEASE</springframework.version>
1313
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
1414
<maven-war-plugin.version>2.6</maven-war-plugin.version>
15+
<jstl.version>1.2</jstl.version>
16+
<javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
17+
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
18+
<maven-compiler-plugin.source>1.8</maven-compiler-plugin.source>
19+
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
20+
<deploy-path>enter-location-of-server</deploy-path>
21+
<fileupload.version>1.3.2</fileupload.version>
1522
</properties>
1623

1724
<dependencies>
@@ -40,6 +47,33 @@
4047
<artifactId>spring-core</artifactId>
4148
<version>${springframework.version}</version>
4249
</dependency>
50+
<dependency>
51+
<groupId>javax.servlet.jsp</groupId>
52+
<artifactId>javax.servlet.jsp-api</artifactId>
53+
<version>${javax.servlet.jsp-api.version}</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>javax.servlet</groupId>
58+
<artifactId>jstl</artifactId>
59+
<version>${jstl.version}</version>
60+
</dependency>
61+
62+
<dependency>
63+
<groupId>org.hibernate</groupId>
64+
<artifactId>hibernate-validator</artifactId>
65+
<version>${hibernate-validator.version}</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.springframework</groupId>
69+
<artifactId>spring-webmvc</artifactId>
70+
<version>${springframework.version}</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>commons-fileupload</groupId>
74+
<artifactId>commons-fileupload</artifactId>
75+
<version>${fileupload.version}</version>
76+
</dependency>
4377
</dependencies>
4478
<build>
4579
<pluginManagement>
@@ -61,6 +95,7 @@
6195
<warSourceDirectory>src/main/webapp</warSourceDirectory>
6296
<warName>springMvcSimple</warName>
6397
<failOnMissingWebXml>false</failOnMissingWebXml>
98+
<outputDirectory>${deploy-path}</outputDirectory>
6499
</configuration>
65100
</plugin>
66101
</plugins>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.multipart.MultipartResolver;
7+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
8+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
12+
13+
@Configuration
14+
@EnableWebMvc
15+
@ComponentScan(basePackages = "com.baeldung.springmvcforms")
16+
class ApplicationConfiguration extends WebMvcConfigurerAdapter {
17+
18+
@Override
19+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
20+
configurer.enable();
21+
}
22+
23+
@Bean
24+
public InternalResourceViewResolver jspViewResolver() {
25+
InternalResourceViewResolver bean = new InternalResourceViewResolver();
26+
bean.setPrefix("/WEB-INF/views/");
27+
bean.setSuffix(".jsp");
28+
return bean;
29+
}
30+
31+
@Bean
32+
public MultipartResolver multipartResolver() {
33+
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
34+
multipartResolver.setMaxUploadSize(5242880);
35+
return multipartResolver;
36+
}
37+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import org.springframework.web.WebApplicationInitializer;
4+
import org.springframework.web.context.ContextLoaderListener;
5+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
6+
import org.springframework.web.servlet.DispatcherServlet;
7+
8+
import javax.servlet.ServletContext;
9+
import javax.servlet.ServletException;
10+
import javax.servlet.ServletRegistration;
11+
12+
public class WebInitializer implements WebApplicationInitializer {
13+
14+
public void onStartup(ServletContext container) throws ServletException {
15+
16+
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
17+
ctx.register(ApplicationConfiguration.class);
18+
ctx.setServletContext(container);
19+
20+
// Manage the lifecycle of the root application context
21+
container.addListener(new ContextLoaderListener(ctx));
22+
23+
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
24+
25+
servlet.setLoadOnStartup(1);
26+
servlet.addMapping("/");
27+
28+
}
29+
// @Override
30+
// public void onStartup(ServletContext container) {
31+
// // Create the 'root' Spring application context
32+
// AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
33+
// rootContext.register(ServiceConfig.class, JPAConfig.class, SecurityConfig.class);
34+
//
35+
// // Manage the lifecycle of the root application context
36+
// container.addListener(new ContextLoaderListener(rootContext));
37+
//
38+
// // Create the dispatcher servlet's Spring application context
39+
// AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
40+
// dispatcherServlet.register(MvcConfig.class);
41+
//
42+
// // Register and map the dispatcher servlet
43+
// ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
44+
// dispatcher.setLoadOnStartup(1);
45+
// dispatcher.addMapping("/");
46+
//
47+
// }
48+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.spring.controller;
2+
3+
import javax.validation.Valid;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestMethod;
13+
import org.springframework.web.servlet.ModelAndView;
14+
15+
import com.baeldung.spring.domain.Customer;
16+
import com.baeldung.spring.validator.CustomerValidator;
17+
18+
@Controller
19+
public class CustomerController {
20+
21+
@Autowired
22+
CustomerValidator validator;
23+
24+
@RequestMapping(value = "/customer", method = RequestMethod.GET)
25+
public ModelAndView showForm() {
26+
return new ModelAndView("customerHome", "customer", new Customer());
27+
}
28+
29+
@PostMapping("/addCustomer")
30+
public String submit(@Valid @ModelAttribute("customer") final Customer customer, final BindingResult result, final ModelMap model) {
31+
validator.validate(customer, result);
32+
if (result.hasErrors()) {
33+
return "customerHome";
34+
}
35+
model.addAttribute("customerId", customer.getCustomerId());
36+
model.addAttribute("customerName", customer.getCustomerName());
37+
model.addAttribute("customerContact", customer.getCustomerContact());
38+
model.addAttribute("customerEmail", customer.getCustomerEmail());
39+
return "customerView";
40+
}
41+
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.spring.controller;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.validation.Valid;
7+
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.ModelMap;
10+
import org.springframework.validation.BindingResult;
11+
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.PathVariable;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RequestMethod;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
import org.springframework.web.servlet.ModelAndView;
17+
18+
import com.baeldung.spring.domain.Employee;
19+
20+
@Controller
21+
public class EmployeeController {
22+
23+
Map<Long, Employee> employeeMap = new HashMap<>();
24+
25+
@RequestMapping(value = "/employee", method = RequestMethod.GET)
26+
public ModelAndView showForm() {
27+
return new ModelAndView("employeeHome", "employee", new Employee());
28+
}
29+
30+
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
31+
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
32+
return employeeMap.get(Id);
33+
}
34+
35+
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
36+
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
37+
if (result.hasErrors()) {
38+
return "error";
39+
}
40+
model.addAttribute("name", employee.getName());
41+
model.addAttribute("contactNumber", employee.getContactNumber());
42+
model.addAttribute("id", employee.getId());
43+
employeeMap.put(employee.getId(), employee);
44+
return "employeeView";
45+
}
46+
47+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.spring.controller;
2+
3+
import java.io.File;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import javax.servlet.http.HttpServletRequest;
8+
import javax.servlet.http.HttpServletResponse;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
13+
import org.springframework.web.multipart.MultipartFile;
14+
import org.springframework.web.servlet.HandlerExceptionResolver;
15+
import org.springframework.web.servlet.ModelAndView;
16+
17+
@Controller
18+
public class FileUploadController implements HandlerExceptionResolver {
19+
20+
@RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
21+
public String getImageView() {
22+
return "file";
23+
}
24+
25+
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
26+
public ModelAndView uploadFile(MultipartFile file) throws IOException{
27+
ModelAndView modelAndView = new ModelAndView("file");
28+
29+
InputStream in = file.getInputStream();
30+
File currDir = new File(".");
31+
String path = currDir.getAbsolutePath();
32+
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename());
33+
int ch = 0;
34+
while ((ch = in.read()) != -1) {
35+
f.write(ch);
36+
}
37+
f.flush();
38+
f.close();
39+
40+
modelAndView.getModel().put("message", "File uploaded successfully!");
41+
return modelAndView;
42+
}
43+
44+
@Override
45+
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
46+
ModelAndView modelAndView = new ModelAndView("file");
47+
if (exc instanceof MaxUploadSizeExceededException) {
48+
modelAndView.getModel().put("message", "File size exceeds limit!");
49+
}
50+
return modelAndView;
51+
}
52+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.spring.domain;
2+
3+
public class Customer {
4+
private String customerId;
5+
private String customerName;
6+
private String customerContact;
7+
private String customerEmail;
8+
9+
public Customer() {
10+
super();
11+
}
12+
13+
public String getCustomerId() {
14+
return customerId;
15+
}
16+
17+
public void setCustomerId(String customerId) {
18+
this.customerId = customerId;
19+
}
20+
21+
public String getCustomerName() {
22+
return customerName;
23+
}
24+
25+
public void setCustomerName(String customerName) {
26+
this.customerName = customerName;
27+
}
28+
29+
public String getCustomerContact() {
30+
return customerContact;
31+
}
32+
33+
public void setCustomerContact(String customerContact) {
34+
this.customerContact = customerContact;
35+
}
36+
37+
public String getCustomerEmail() {
38+
return customerEmail;
39+
}
40+
41+
public void setCustomerEmail(String customerEmail) {
42+
this.customerEmail = customerEmail;
43+
}
44+
45+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.spring.domain;
2+
3+
import javax.validation.constraints.NotNull;
4+
import javax.validation.constraints.Size;
5+
6+
public class Employee {
7+
8+
private long id;
9+
10+
@NotNull
11+
@Size(min = 5)
12+
private String name;
13+
14+
@NotNull
15+
@Size(min = 7)
16+
private String contactNumber;
17+
18+
public Employee() {
19+
super();
20+
}
21+
22+
public String getName() {
23+
return name;
24+
}
25+
26+
public void setName(final String name) {
27+
this.name = name;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(final long id) {
35+
this.id = id;
36+
}
37+
38+
public String getContactNumber() {
39+
return contactNumber;
40+
}
41+
42+
public void setContactNumber(final String contactNumber) {
43+
this.contactNumber = contactNumber;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)