JAVA-3522: Moved spring-mvc-forms-jsp to spring-web-modules
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
## Spring MVC Forms JSP
|
||||
|
||||
This module contains articles about Spring MVC Forms using JSP
|
||||
|
||||
### Relevant Articles
|
||||
- [MaxUploadSizeExceededException in Spring](https://www.baeldung.com/spring-maxuploadsizeexceeded)
|
||||
- [Getting Started with Forms in Spring MVC](https://www.baeldung.com/spring-mvc-form-tutorial)
|
||||
- [Form Validation with AngularJS and Spring MVC](https://www.baeldung.com/validation-angularjs-spring-mvc)
|
||||
- [A Guide to the JSTL Library](https://www.baeldung.com/jstl)
|
||||
@@ -0,0 +1,107 @@
|
||||
<?xml version="1.0"?>
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>spring-mvc-forms-jsp</artifactId>
|
||||
<name>spring-mvc-forms-jsp</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet.jsp</groupId>
|
||||
<artifactId>javax.servlet.jsp-api</artifactId>
|
||||
<version>${javax.servlet.jsp-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>jstl</artifactId>
|
||||
<version>${jstl.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql-connector-java.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-fileupload</groupId>
|
||||
<artifactId>commons-fileupload</artifactId>
|
||||
<version>${fileupload.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<version>${spring-boot-starter-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<!-- Local -->
|
||||
<profile>
|
||||
<id>spring-mvc-forms</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||
<warName>spring-mvc-forms</warName>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<finalName>spring-mvc-forms</finalName>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<hibernate-validator.version>6.0.10.Final</hibernate-validator.version>
|
||||
<fileupload.version>1.3.3</fileupload.version>
|
||||
<hibernate.version>5.2.5.Final</hibernate.version>
|
||||
<mysql-connector-java.version>6.0.6</mysql-connector-java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jstl.bundles;
|
||||
|
||||
import java.util.ListResourceBundle;
|
||||
|
||||
public class CustomMessage_en extends ListResourceBundle {
|
||||
@Override
|
||||
protected Object[][] getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
static final Object[][] contents = {
|
||||
{"verb.go", "go"},
|
||||
{"verb.come", "come"},
|
||||
{"verb.sit", "sit"},
|
||||
{"verb.stand", "stand"}
|
||||
};
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jstl.bundles;
|
||||
|
||||
import java.util.ListResourceBundle;
|
||||
|
||||
public class CustomMessage_fr_FR extends ListResourceBundle {
|
||||
@Override
|
||||
protected Object[][] getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
static final Object[][] contents = {
|
||||
{"verb.go", "aller"},
|
||||
{"verb.come", "venir"},
|
||||
{"verb.sit", "siéger"},
|
||||
{"verb.stand", "se lever"}
|
||||
};
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.jstl.controllers;
|
||||
|
||||
import com.baeldung.jstl.dbaccess.SQLConnection;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.sql.*;
|
||||
import java.util.Calendar;
|
||||
|
||||
@Controller
|
||||
public class JSTLController {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JSTLController.class.getName());
|
||||
|
||||
@Autowired
|
||||
ServletContext servletContext;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
PreparedStatement preparedStatement = null;
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = SQLConnection.getConnection();
|
||||
preparedStatement = connection.prepareStatement("create table IF NOT EXISTS USERS " +
|
||||
"( id int not null primary key AUTO_INCREMENT," +
|
||||
" email VARCHAR(255) not null, first_name varchar (255), last_name varchar (255), registered DATE)");
|
||||
int status = preparedStatement.executeUpdate();
|
||||
preparedStatement = connection.prepareStatement("SELECT COUNT(*) AS total FROM USERS;");
|
||||
ResultSet result = preparedStatement.executeQuery();
|
||||
if(result!=null) {
|
||||
result.next();
|
||||
if (result.getInt("total") == 0) {
|
||||
generateDummy(connection);
|
||||
}
|
||||
} else {
|
||||
generateDummy(connection);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) {
|
||||
preparedStatement.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/core_tags", method = RequestMethod.GET)
|
||||
public ModelAndView coreTags(final Model model) {
|
||||
ModelAndView mv = new ModelAndView("core_tags");
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/core_tags_redirect", method = RequestMethod.GET)
|
||||
public ModelAndView coreTagsRedirect(final Model model) {
|
||||
ModelAndView mv = new ModelAndView("core_tags_redirect");
|
||||
return mv;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/formatting_tags", method = RequestMethod.GET)
|
||||
public ModelAndView formattingTags(final Model model) {
|
||||
ModelAndView mv = new ModelAndView("formatting_tags");
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/sql_tags", method = RequestMethod.GET)
|
||||
public ModelAndView sqlTags(final Model model) {
|
||||
ModelAndView mv = new ModelAndView("sql_tags");
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/xml_tags", method = RequestMethod.GET)
|
||||
public ModelAndView xmlTags(final Model model) {
|
||||
System.out.println("dddddddddddddddddffffffffffffff");
|
||||
ModelAndView mv = new ModelAndView("xml_tags");
|
||||
return mv;
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/items_xml", method = RequestMethod.GET)
|
||||
@ResponseBody public FileSystemResource getFile(HttpServletRequest request, HttpServletResponse response) {
|
||||
response.setContentType("application/xml");
|
||||
return new FileSystemResource(new File(servletContext.getRealPath("/WEB-INF/items.xsl")));
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/function_tags", method = RequestMethod.GET)
|
||||
public ModelAndView functionTags(final Model model) {
|
||||
ModelAndView mv = new ModelAndView("function_tags");
|
||||
return mv;
|
||||
}
|
||||
|
||||
|
||||
private void generateDummy(Connection connection) throws SQLException {
|
||||
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO USERS " +
|
||||
"(email, first_name, last_name, registered) VALUES (?, ?, ?, ?);");
|
||||
preparedStatement.setString(1, "[email protected]");
|
||||
preparedStatement.setString(2, "Patrick");
|
||||
preparedStatement.setString(3, "Frank");
|
||||
preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis()));
|
||||
preparedStatement.addBatch();
|
||||
preparedStatement.setString(1, "[email protected]");
|
||||
preparedStatement.setString(2, "Benjamin");
|
||||
preparedStatement.setString(3, "Franklin");
|
||||
preparedStatement.setDate(4, new Date(Calendar.getInstance().getTimeInMillis()));
|
||||
preparedStatement.executeBatch();
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.jstl.dbaccess;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SQLConnection {
|
||||
private static String userName = "root";
|
||||
private static String password = "";
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SQLConnection.class.getName());
|
||||
|
||||
public static Connection getConnection() throws Exception {
|
||||
LOGGER.error("connecting...");
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
LOGGER.error("class checked...");
|
||||
Connection conn = null;
|
||||
Properties connectionProps = new Properties();
|
||||
connectionProps.put("user", userName);
|
||||
connectionProps.put("password", password);
|
||||
conn = DriverManager.getConnection(
|
||||
"jdbc:mysql://localhost:3306/test?autoReconnect=true&useSSL=false",
|
||||
connectionProps);
|
||||
LOGGER.info("Connected to database");
|
||||
return conn;
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.springmvcforms.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.web.multipart.MultipartResolver;
|
||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = {"com.baeldung.springmvcforms", "com.baeldung.jstl"})
|
||||
public class ApplicationConfiguration implements WebMvcConfigurer {
|
||||
|
||||
@Override
|
||||
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
|
||||
configurer.enable();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ResourceBundleMessageSource resourceBundleMessageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
// switch orders to server views from html over views directory
|
||||
|
||||
@Bean
|
||||
public InternalResourceViewResolver jspViewResolver() {
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setPrefix("/WEB-INF/views/");
|
||||
bean.setSuffix(".jsp");
|
||||
bean.setOrder(1);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public InternalResourceViewResolver htmlViewResolver() {
|
||||
InternalResourceViewResolver bean = new InternalResourceViewResolver();
|
||||
bean.setPrefix("/WEB-INF/html/");
|
||||
bean.setSuffix(".html");
|
||||
bean.setOrder(2);
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MultipartResolver multipartResolver() {
|
||||
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
|
||||
multipartResolver.setMaxUploadSize(5242880);
|
||||
return multipartResolver;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.springmvcforms.configuration;
|
||||
|
||||
import org.springframework.web.WebApplicationInitializer;
|
||||
import org.springframework.web.context.ContextLoaderListener;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRegistration;
|
||||
|
||||
public class WebInitializer implements WebApplicationInitializer {
|
||||
|
||||
public void onStartup(ServletContext container) throws ServletException {
|
||||
|
||||
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
|
||||
ctx.register(ApplicationConfiguration.class);
|
||||
ctx.setServletContext(container);
|
||||
|
||||
// Manage the lifecycle of the root application context
|
||||
container.addListener(new ContextLoaderListener(ctx));
|
||||
|
||||
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
|
||||
|
||||
servlet.setLoadOnStartup(1);
|
||||
servlet.addMapping("/");
|
||||
|
||||
}
|
||||
// @Override
|
||||
// public void onStartup(ServletContext container) {
|
||||
// // Create the 'root' Spring application context
|
||||
// AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
|
||||
// rootContext.register(ServiceConfig.class, JPAConfig.class, SecurityConfig.class);
|
||||
//
|
||||
// // Manage the lifecycle of the root application context
|
||||
// container.addListener(new ContextLoaderListener(rootContext));
|
||||
//
|
||||
// // Create the dispatcher servlet's Spring application context
|
||||
// AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
|
||||
// dispatcherServlet.register(MvcConfig.class);
|
||||
//
|
||||
// // Register and map the dispatcher servlet
|
||||
// ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherServlet));
|
||||
// dispatcher.setLoadOnStartup(1);
|
||||
// dispatcher.addMapping("/");
|
||||
//
|
||||
// }
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.Customer;
|
||||
import com.baeldung.springmvcforms.validator.CustomerValidator;
|
||||
|
||||
@Controller
|
||||
public class CustomerController {
|
||||
|
||||
@Autowired
|
||||
CustomerValidator validator;
|
||||
|
||||
@RequestMapping(value = "/customer", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("customerHome", "customer", new Customer());
|
||||
}
|
||||
|
||||
@PostMapping("/addCustomer")
|
||||
public String submit(@Valid @ModelAttribute("customer") final Customer customer, final BindingResult result, final ModelMap model) {
|
||||
validator.validate(customer, result);
|
||||
if (result.hasErrors()) {
|
||||
return "customerHome";
|
||||
}
|
||||
model.addAttribute("customerId", customer.getCustomerId());
|
||||
model.addAttribute("customerName", customer.getCustomerName());
|
||||
model.addAttribute("customerContact", customer.getCustomerContact());
|
||||
model.addAttribute("customerEmail", customer.getCustomerEmail());
|
||||
return "customerView";
|
||||
}
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.Employee;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Controller
|
||||
public class EmployeeController {
|
||||
|
||||
Map<Long, Employee> employeeMap = new HashMap<>();
|
||||
|
||||
@RequestMapping(value = "/employee", method = RequestMethod.GET)
|
||||
public ModelAndView showForm() {
|
||||
return new ModelAndView("employeeHome", "employee", new Employee());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET)
|
||||
public @ResponseBody Employee getEmployeeById(@PathVariable final long Id) {
|
||||
return employeeMap.get(Id);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
|
||||
public String submit(@Valid @ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
model.addAttribute("name", employee.getName());
|
||||
model.addAttribute("contactNumber", employee.getContactNumber());
|
||||
model.addAttribute("id", employee.getId());
|
||||
employeeMap.put(employee.getId(), employee);
|
||||
return "employeeView";
|
||||
}
|
||||
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class FileUploadController implements HandlerExceptionResolver {
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.GET)
|
||||
public String getImageView() {
|
||||
return "file";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
|
||||
public ModelAndView uploadFile(MultipartFile file) throws IOException{
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
|
||||
InputStream in = file.getInputStream();
|
||||
File currDir = new File(".");
|
||||
String path = currDir.getAbsolutePath();
|
||||
FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ file.getOriginalFilename());
|
||||
int ch = 0;
|
||||
while ((ch = in.read()) != -1) {
|
||||
f.write(ch);
|
||||
}
|
||||
f.flush();
|
||||
f.close();
|
||||
|
||||
modelAndView.getModel().put("message", "File uploaded successfully!");
|
||||
return modelAndView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exc) {
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
if (exc instanceof MaxUploadSizeExceededException) {
|
||||
modelAndView.getModel().put("message", "File size exceeds limit!");
|
||||
}
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.springmvcforms.controller;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.User;
|
||||
import org.springframework.context.support.DefaultMessageSourceResolvable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Controller
|
||||
public class UserController {
|
||||
|
||||
private List<User> users = Arrays.asList(
|
||||
new User("[email protected]", "pass", "Ana", 20),
|
||||
new User("[email protected]", "pass", "Bob", 30),
|
||||
new User("[email protected]", "pass", "John", 40),
|
||||
new User("[email protected]", "pass", "Mary", 30));
|
||||
|
||||
@GetMapping("/userPage")
|
||||
public String getUserProfilePage() {
|
||||
return "user";
|
||||
}
|
||||
|
||||
@PostMapping("/user")
|
||||
@ResponseBody
|
||||
public ResponseEntity<Object> saveUser(@Valid User user, BindingResult result, Model model) {
|
||||
if (result.hasErrors()) {
|
||||
final List<String> errors = result.getAllErrors().stream()
|
||||
.map(DefaultMessageSourceResolvable::getDefaultMessage)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return new ResponseEntity<>(errors, HttpStatus.OK);
|
||||
} else {
|
||||
if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) {
|
||||
return new ResponseEntity<>(Collections.singletonList("Email already exists!"), HttpStatus.CONFLICT);
|
||||
} else {
|
||||
users.add(user);
|
||||
return new ResponseEntity<>(HttpStatus.CREATED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/users")
|
||||
@ResponseBody
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.springmvcforms.domain;
|
||||
|
||||
public class Customer {
|
||||
private String customerId;
|
||||
private String customerName;
|
||||
private String customerContact;
|
||||
private String customerEmail;
|
||||
|
||||
public Customer() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public void setCustomerId(String customerId) {
|
||||
this.customerId = customerId;
|
||||
}
|
||||
|
||||
public String getCustomerName() {
|
||||
return customerName;
|
||||
}
|
||||
|
||||
public void setCustomerName(String customerName) {
|
||||
this.customerName = customerName;
|
||||
}
|
||||
|
||||
public String getCustomerContact() {
|
||||
return customerContact;
|
||||
}
|
||||
|
||||
public void setCustomerContact(String customerContact) {
|
||||
this.customerContact = customerContact;
|
||||
}
|
||||
|
||||
public String getCustomerEmail() {
|
||||
return customerEmail;
|
||||
}
|
||||
|
||||
public void setCustomerEmail(String customerEmail) {
|
||||
this.customerEmail = customerEmail;
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.springmvcforms.domain;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class Employee {
|
||||
|
||||
private long id;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 5)
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 7)
|
||||
private String contactNumber;
|
||||
|
||||
public Employee() {
|
||||
super();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getContactNumber() {
|
||||
return contactNumber;
|
||||
}
|
||||
|
||||
public void setContactNumber(final String contactNumber) {
|
||||
this.contactNumber = contactNumber;
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.springmvcforms.domain;
|
||||
|
||||
import javax.validation.constraints.Digits;
|
||||
import javax.validation.constraints.Email;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class User {
|
||||
|
||||
@NotNull
|
||||
@Email
|
||||
private String email;
|
||||
|
||||
@NotNull
|
||||
@Size(min = 4, max = 15)
|
||||
private String password;
|
||||
|
||||
@NotBlank
|
||||
private String name;
|
||||
|
||||
@Min(18)
|
||||
@Digits(integer = 2, fraction = 0)
|
||||
private int age;
|
||||
|
||||
public User() {
|
||||
|
||||
}
|
||||
|
||||
public User(String email, String password, String name, int age) {
|
||||
super();
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.springmvcforms.interceptor;
|
||||
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import org.springframework.web.multipart.MaxUploadSizeExceededException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class FileUploadExceptionAdvice {
|
||||
|
||||
@ExceptionHandler(MaxUploadSizeExceededException.class)
|
||||
public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response){
|
||||
ModelAndView modelAndView = new ModelAndView("file");
|
||||
modelAndView.getModel().put("message", "File too large!");
|
||||
return modelAndView;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springmvcforms.validator;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
|
||||
import com.baeldung.springmvcforms.domain.Customer;
|
||||
|
||||
@Component
|
||||
public class CustomerValidator implements Validator {
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> clazz) {
|
||||
return Customer.class.isAssignableFrom(clazz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate(Object target, Errors errors) {
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerId", "error.customerId", "Customer Id is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerName", "error.customerName", "Customer Name is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerContact", "error.customerNumber", "Customer Contact is required.");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "customerEmail", "error.customerEmail", "Customer Email is required.");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,68 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="ISO-8859-1">
|
||||
<title>Users</title>
|
||||
<script
|
||||
src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular-messages.js"></script>
|
||||
|
||||
<script src="js/app.js"></script>
|
||||
<link rel="stylesheet" href="css/user.css">
|
||||
</head>
|
||||
<body ng-app="app" ng-controller="UserCtrl">
|
||||
|
||||
<form name="userForm" method="POST" novalidate ng-class="{'form-error':submitted}" ng-submit="saveUser()" >
|
||||
|
||||
<label class="form-label">Email:</label>
|
||||
<input type="email" name="email" required ng-model="user.email" class="form-input"/>
|
||||
<div ng-messages="userForm.email.$error" ng-show="submitted && userForm.email.$invalid" class="error-messages">
|
||||
<p ng-message="email">Invalid email!</p>
|
||||
<p ng-message="required">Email is required!</p>
|
||||
</div>
|
||||
<div class="check" ng-show="submitted && userForm.email.$valid">✓</div>
|
||||
|
||||
<label class="form-label">Password:</label>
|
||||
<input type="password" name="password" required ng-model="user.password" ng-minlength="4" ng-maxlength="15" class="form-input"/>
|
||||
<div ng-messages="userForm.password.$error" ng-show="submitted && userForm.password.$invalid" class="error-messages">
|
||||
<p ng-message="minlength">Password must be at least 4 characters!</p>
|
||||
<p ng-message="maxlength">Password must not be longer than 15 characters!</p>
|
||||
<p ng-message="required">Password is required!</p>
|
||||
</div>
|
||||
<div class="check" ng-show="submitted && userForm.password.$valid">✓</div>
|
||||
|
||||
<label class="form-label">Name:</label>
|
||||
<input type="text" name="name" ng-model="user.name" ng-trim="true" required class="form-input" />
|
||||
<div ng-messages="userForm.name.$error" ng-show="submitted && userForm.name.$invalid" class="error-messages">
|
||||
<p ng-message="required">Name is required!</p>
|
||||
</div>
|
||||
<div class="check" ng-show="submitted && userForm.name.$valid">✓</div>
|
||||
|
||||
<label class="form-label">Age:</label>
|
||||
<input type="number" name="age" ng-model="user.age" ng-min="18" class="form-input" required/>
|
||||
<div ng-messages="userForm.age.$error" ng-show="submitted && userForm.age.$invalid" class="error-messages">
|
||||
<p ng-message="min">Age must be greater than 18!</p>
|
||||
<p ng-message="required">Age must be greater than 18!</p>
|
||||
</div>
|
||||
<div class="check" ng-show="submitted && userForm.age.$valid">✓</div>
|
||||
<br />
|
||||
<button type="submit" class="form-button-save" >Save</button>
|
||||
<button type="reset" ng-click="resetForm()" class="form-button-reset">Reset</button>
|
||||
</form>
|
||||
|
||||
<br />
|
||||
<div style="color:red;font-weight:bold">{{errorMessage}}</div>
|
||||
<div style="color:green;font-weight:bold">{{message}}</div>
|
||||
<br />
|
||||
|
||||
All Users: <br />
|
||||
<table id="users">
|
||||
<tr>
|
||||
<th>Email</th><th>Name</th><th>Age</th>
|
||||
</tr>
|
||||
<tr ng-repeat="usr in users">
|
||||
<td>{{usr.email}}</td> <td>{{usr.name}}</td> <td>{{usr.age}}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version = "1.0"?>
|
||||
<xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" version = "1.0">
|
||||
|
||||
<xsl:output method = "html" indent = "yes"/>
|
||||
<xsl:param name = "bgColor"/>
|
||||
|
||||
<xsl:template match = "/">
|
||||
<html>
|
||||
<body>
|
||||
<xsl:apply-templates/>
|
||||
</body>
|
||||
</html>
|
||||
</xsl:template>
|
||||
|
||||
<xsl:template match = "items">
|
||||
<table border = "1" width = "40%" bgColor = "{$bgColor}">
|
||||
<xsl:for-each select = "item">
|
||||
<tr>
|
||||
<td><i><xsl:value-of select = "name"/></i></td>
|
||||
<td><xsl:value-of select = "category"/></td>
|
||||
<td><xsl:value-of select = "price"/></td>
|
||||
</tr>
|
||||
</xsl:for-each>
|
||||
</table>
|
||||
</xsl:template>
|
||||
|
||||
</xsl:stylesheet>
|
||||
@@ -0,0 +1,92 @@
|
||||
<%@ page import="java.util.Random" %>
|
||||
<%@ page import="java.util.Date" %>
|
||||
<%@ page import="java.util.Calendar" %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<html>
|
||||
<head>
|
||||
<c:set value="JSTL Core Tags Example" var="pageTitle"/>
|
||||
<title>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</h1>
|
||||
<c:remove var="pageTitle"/>
|
||||
<p><c:out value="${pageTitle}"/></p>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="<c:catch> Example"/>
|
||||
</h3>
|
||||
<c:catch var ="exceptionThrown">
|
||||
<% int x = Integer.valueOf("a");%>
|
||||
</c:catch>
|
||||
|
||||
<c:if test = "${exceptionThrown != null}">
|
||||
<p>The exception is : ${exceptionThrown} <br />
|
||||
There is an exception: ${exceptionThrown.message}</p>
|
||||
</c:if>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<c:choose> Example"/>
|
||||
<c:set value="<%= Calendar.getInstance().get(Calendar.SECOND)%>" var="seconds"/>
|
||||
</h3>
|
||||
<p>
|
||||
<c:choose>
|
||||
<c:when test="${seconds le 30 }">
|
||||
<c:out value="${seconds} is less than 30"/>
|
||||
</c:when>
|
||||
<c:when test="${seconds eq 30 }">
|
||||
<c:out value="${seconds} is equal to 30"/>
|
||||
</c:when>
|
||||
<c:otherwise>
|
||||
<c:out value="${seconds} is greater than 30"/>
|
||||
</c:otherwise>
|
||||
</c:choose>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="<c:import> Example"/>
|
||||
</h3>
|
||||
|
||||
<c:import var = "data" url = "http://www.example.com"/>
|
||||
<c:out value = "${data}"/>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<c:forEach> Example"/>
|
||||
</h3>
|
||||
|
||||
<c:forEach var = "i" items="1,4,5,6,7,8,9">
|
||||
Item <c:out value = "No. ${i}"/><p>
|
||||
</c:forEach>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<c:forToken> Example"/>
|
||||
</h3>
|
||||
|
||||
<c:forTokens items = "Patrick:Wilson:Ibrahima:Chris" delims = ":" var = "name">
|
||||
<c:out value = "Name: ${name}"/><p>
|
||||
</c:forTokens>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<c:url> and <c:param> Example"/>
|
||||
</h3>
|
||||
<c:url value = "/core_tags" var = "myURL">
|
||||
<c:param name = "parameter_1" value = "1234"/>
|
||||
<c:param name = "parameter_2" value = "abcd"/>
|
||||
</c:url>
|
||||
<c:out value = "URL: ${myURL}"/>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<%@ page import="java.util.Random" %>
|
||||
<%@ page import="java.util.Date" %>
|
||||
<%@ page import="java.util.Calendar" %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<html>
|
||||
<head>
|
||||
<c:set value="JSTL Core Tags Examples" var="pageTitle"/>
|
||||
<title>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
<c:redirect url="/core_tags"/>
|
||||
</body>
|
||||
</html>
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Add Customer</title>
|
||||
<style>
|
||||
.error {
|
||||
color: #ff0000;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Customer Details</h3>
|
||||
|
||||
<form:form method="POST" action="${pageContext.request.contextPath}/addCustomer" modelAttribute="customer">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="customerId">Customer Id</form:label></td>
|
||||
<td><form:input path="customerId" /></td>
|
||||
<td><form:errors path="customerId" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerName">Customer Name</form:label></td>
|
||||
<td><form:input path="customerName" /></td>
|
||||
<td><form:errors path="customerName" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerContact">Customer Contact</form:label></td>
|
||||
<td><form:input path="customerContact"/></td>
|
||||
<td><form:errors path="customerContact" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="customerEmail">Customer Email</form:label></td>
|
||||
<td><form:input path="customerEmail" /></td>
|
||||
<td><form:errors path="customerEmail" cssClass="error" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Customer Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Customer Id :</td>
|
||||
<td>${customerId}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Name :</td>
|
||||
<td>${customerName}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Contact :</td>
|
||||
<td>${customerContact}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Customer Email :</td>
|
||||
<td>${customerEmail}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Form Example - Register an Employee</title>
|
||||
</head>
|
||||
<body>
|
||||
<h3>Welcome, Enter The Employee Details</h3>
|
||||
|
||||
<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee" modelAttribute="employee">
|
||||
<table>
|
||||
<tr>
|
||||
<td><form:label path="name">Name</form:label></td>
|
||||
<td><form:input path="name" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="id">Id</form:label></td>
|
||||
<td><form:input path="id" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><form:label path="contactNumber">Contact Number</form:label></td>
|
||||
<td><form:input path="contactNumber" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form:form>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Spring MVC Form Handling</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h2>Submitted Employee Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td>Name :</td>
|
||||
<td>${name}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ID :</td>
|
||||
<td>${id}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Contact Number :</td>
|
||||
<td>${contactNumber}</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,20 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>SpringMVCExample</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>Pleas enter the correct details</h3>
|
||||
<table>
|
||||
<tr>
|
||||
<td><a href="employee">Retry</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,23 @@
|
||||
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
|
||||
pageEncoding="ISO-8859-1"%>
|
||||
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Upload file</title>
|
||||
</head>
|
||||
<body>
|
||||
<c:url value="/uploadFile" var="uploadFileUrl" />
|
||||
<form method="post" enctype="multipart/form-data" action="${uploadFileUrl}">
|
||||
<input type="file" name="file"/>
|
||||
<input type="submit" value="Upload file"/>
|
||||
</form>
|
||||
<br />
|
||||
${message }
|
||||
<br /> <br />
|
||||
<form method="GET" action="${uploadFileUrl}" >
|
||||
<input type="submit" value="Reset" />
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
+213
@@ -0,0 +1,213 @@
|
||||
<%@ page import="java.util.Calendar" %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
|
||||
<html>
|
||||
<head>
|
||||
<c:set value="JSTL Formatting Tags Example" var="pageTitle"/>
|
||||
<title>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</h1>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="<fmt:formatNumber> Example"/>
|
||||
</h3>
|
||||
<c:set var="fee" value="35050.1067"/>
|
||||
|
||||
<p>Formatted Number - Currency: <fmt:formatNumber value="${fee}"
|
||||
type="currency"/></p>
|
||||
|
||||
<p>Formatted Number - Maximum Integer Digits: <fmt:formatNumber type="number"
|
||||
maxIntegerDigits="2" value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - Maximum Fraction Digits: <fmt:formatNumber type="number"
|
||||
maxFractionDigits="2" value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - Grouping: <fmt:formatNumber type="number"
|
||||
groupingUsed="false" value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - Percent with Maximum Integer Digits: <fmt:formatNumber type="percent"
|
||||
maxIntegerDigits="3"
|
||||
value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - Percent with Minimum Fraction Digits: <fmt:formatNumber type="percent"
|
||||
minFractionDigits="10"
|
||||
value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number Percent with Minimum Integer Digits: <fmt:formatNumber type="percent"
|
||||
minIntegerDigits="4" value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - with Pattern: <fmt:formatNumber type="number"
|
||||
pattern="###.##E0" value="${fee}"/></p>
|
||||
|
||||
<p>Formatted Number - Currency with Internationalization :
|
||||
<fmt:setLocale value="en_US"/>
|
||||
<fmt:formatNumber value="${fee}" type="currency"/>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:parseNumber> Example"/>
|
||||
</h3>
|
||||
<fmt:parseNumber var="i" type="number" value="${fee}"/>
|
||||
<p>Parsed Number : <c:out value="${i}"/></p>
|
||||
<fmt:parseNumber var="i" integerOnly="true"
|
||||
type="number" value="${fee}"/>
|
||||
<p>Parsed Number - with Integer Only set to True: <c:out value="${i}"/></p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="<fmt:formatDate> Example"/>
|
||||
<c:set var="now" value="<%= new java.util.Date()%>"/>
|
||||
</h3>
|
||||
|
||||
<p>Formatted Date - with type set to time: <fmt:formatDate type="time"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with type set to date: <fmt:formatDate type="date"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with type set to both: <fmt:formatDate type="both"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with short style for both date and time: <fmt:formatDate type="both"
|
||||
dateStyle="short" timeStyle="short"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with medium style for both date and time: <fmt:formatDate type="both"
|
||||
dateStyle="medium" timeStyle="medium"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with long style for both date and time: <fmt:formatDate type="both"
|
||||
dateStyle="long" timeStyle="long"
|
||||
value="${now}"/></p>
|
||||
|
||||
<p>Formatted Date - with pattern: <fmt:formatDate pattern="yyyy-MM-dd"
|
||||
value="${now}"/></p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:parseDate> Example"/>
|
||||
</h3>
|
||||
|
||||
<c:set var="today" value="28-03-2018"/>
|
||||
<fmt:parseDate value="${today}" var="parsedDate" pattern="dd-MM-yyyy"/>
|
||||
<p>Parsed Date: <c:out value="${parsedDate}"/></p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:bundle> and <fmt:message> Example"/>
|
||||
</h3>
|
||||
|
||||
<p>
|
||||
<fmt:bundle basename="com.baeldung.jstl.bundles.CustomMessage" prefix="verb.">
|
||||
<fmt:message key="go"/><br/>
|
||||
<fmt:message key="come"/><br/>
|
||||
<fmt:message key="sit"/><br/>
|
||||
<fmt:message key="stand"/><br/>
|
||||
</fmt:bundle>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:setLocale> Example"/>
|
||||
</h3>
|
||||
|
||||
<p>
|
||||
<fmt:setLocale value="fr_FR"/>
|
||||
<fmt:bundle basename="com.baeldung.jstl.bundles.CustomMessage" prefix="verb.">
|
||||
<fmt:message key="go"/><br/>
|
||||
<fmt:message key="come"/><br/>
|
||||
<fmt:message key="sit"/><br/>
|
||||
<fmt:message key="stand"/><br/>
|
||||
</fmt:bundle>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:setBundle> Example"/>
|
||||
</h3>
|
||||
|
||||
<p>
|
||||
<fmt:setLocale value="En"/>
|
||||
<fmt:setBundle basename="com.baeldung.jstl.bundles.CustomMessage" var="lang"/>
|
||||
<fmt:message key="verb.go" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.come" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.sit" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.stand" bundle="${lang}"/><br/>
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:timeZone> Example"/>
|
||||
</h3>
|
||||
<table border="1" width="40%">
|
||||
<tr>
|
||||
<td width="100%" colspan="2">
|
||||
<p align="center">
|
||||
<b>
|
||||
<font size="4">Time being formatted:
|
||||
<fmt:formatDate value="${now}" type="both"
|
||||
timeStyle="long" dateStyle="long"/>
|
||||
</font>
|
||||
</b>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<c:forEach var="zone"
|
||||
items="<%= java.util.TimeZone.getAvailableIDs()%>" end="4">
|
||||
<tr>
|
||||
<td width="51%">
|
||||
<c:out value="${zone}"/>
|
||||
</td>
|
||||
<td width="49%">
|
||||
<fmt:timeZone value="${zone}">
|
||||
<fmt:formatDate value="${now}" timeZone="${zn}"
|
||||
type="both"/>
|
||||
</fmt:timeZone>
|
||||
</td>
|
||||
</tr>
|
||||
</c:forEach>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:setTimeZone> Example"/>
|
||||
</h3>
|
||||
|
||||
<p>Current Date with Default Time Zone: <fmt:formatDate value="${now}"
|
||||
type="both" timeStyle="long" dateStyle="long"/></p>
|
||||
<p>Change Time Zone to GMT+9</p>
|
||||
<fmt:setTimeZone value="GMT+9"/>
|
||||
<p>Date in Changed Zone: <fmt:formatDate value="${now}"
|
||||
type="both" timeStyle="long" dateStyle="long"/></p>
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<h3>
|
||||
<c:out value="<fmt:requestEncoding> Example"/>
|
||||
</h3>
|
||||
<fmt:requestEncoding value = "UTF-8" />
|
||||
<fmt:setLocale value = "fr_FR"/>
|
||||
<fmt:setBundle basename = "com.baeldung.jstl.bundles.CustomMessage" var = "lang"/>
|
||||
<fmt:message key="verb.go" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.come" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.sit" bundle="${lang}"/><br/>
|
||||
<fmt:message key="verb.stand" bundle="${lang}"/><br/>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
+151
@@ -0,0 +1,151 @@
|
||||
<%@ page import="java.util.Random" %>
|
||||
<%@ page import="java.util.Date" %>
|
||||
<%@ page import="java.util.Calendar" %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ taglib prefix = "fn"
|
||||
uri = "http://java.sun.com/jsp/jstl/functions" %>
|
||||
<html>
|
||||
<head>
|
||||
<c:set value="JSTL Function Tags Example" var="pageTitle"/>
|
||||
<title>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<c:out value="${pageTitle}"/>
|
||||
</h1>
|
||||
<c:set var = "string1" value = "This is first string"/>
|
||||
<c:set var = "string2" value = "This is second string with <tag>test XML</tag>"/>
|
||||
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:contains() Example"/>
|
||||
</h3>
|
||||
<c:if test = "${fn:contains(string1, 'first')}">
|
||||
<p>Found 'first' in string<p>
|
||||
</c:if>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:containsIgnoreCase() Example"/>
|
||||
</h3>
|
||||
<c:if test = "${fn:containsIgnoreCase(string1, 'first')}">
|
||||
<p>Found 'first' string<p>
|
||||
</c:if>
|
||||
|
||||
<c:if test = "${fn:containsIgnoreCase(string1, 'FIRST')}">
|
||||
<p>Found 'FIRST' string<p>
|
||||
</c:if>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:endsWith() Example"/>
|
||||
</h3>
|
||||
<c:if test = "${fn:endsWith(string1, 'string')}">
|
||||
<p>String ends with 'string'<p>
|
||||
</c:if>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:escapeXml() Example"/>
|
||||
</h3>
|
||||
<p>With escapeXml() Function:</p>
|
||||
<p>string (1) : ${fn:escapeXml(string1)}</p>
|
||||
<p>string (2) : ${fn:escapeXml(string2)}</p>
|
||||
|
||||
<p>Without escapeXml() Function:</p>
|
||||
<p>string (1) : ${string1}</p>
|
||||
<p>string (2) : ${string2}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:indexOf() Example"/>
|
||||
</h3>
|
||||
<p>Index (1) : ${fn:indexOf(string1, "first")}</p>
|
||||
<p>Index (2) : ${fn:indexOf(string2, "second")}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:join() and fn:split() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:split(string1, ' ')}" />
|
||||
<c:set var = "string4" value = "${fn:join(string3, '-')}" />
|
||||
<p>Final String : ${string4}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:length() Example"/>
|
||||
</h3>
|
||||
<p>Length of String (1) : ${fn:length(string1)}</p>
|
||||
<p>Length of String (2) : ${fn:length(string2)}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:replace() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:replace(string1, 'first', 'third')}" />
|
||||
<p>Final String : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:startsWith() Example"/>
|
||||
</h3>
|
||||
<c:if test = "${fn:startsWith(string1, 'This')}">
|
||||
<p>String starts with 'This'</p>
|
||||
</c:if>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:substring() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:substring(string1, 5, 15)}" />
|
||||
|
||||
<p>Final sub string : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:substringAfter() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:substringAfter(string1, 'is')}" />
|
||||
|
||||
<p>Final sub string : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:substringBefore() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:substringBefore(string1, 'is')}" />
|
||||
|
||||
<p>Final sub string : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:toLowerCase() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:toLowerCase(string1)}" />
|
||||
|
||||
<p>Final string : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:toUpperCase() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string3" value = "${fn:toUpperCase(string1)}" />
|
||||
|
||||
<p>Final string : ${string3}</p>
|
||||
</div>
|
||||
<div>
|
||||
<h3>
|
||||
<c:out value="fn:trim() Example"/>
|
||||
</h3>
|
||||
<c:set var = "string1" value = "This is first String "/>
|
||||
<p>String (1) Length : ${fn:length(string1)}</p>
|
||||
|
||||
<c:set var = "string2" value = "${fn:trim(string1)}" />
|
||||
<p>String (2) Length : ${fn:length(string2)}</p>
|
||||
<p>Final string : "${string2}"</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user