Skip to content

Commit 5cc820b

Browse files
committed
add spring mvc form basic
1 parent 212c8d6 commit 5cc820b

File tree

8 files changed

+321
-0
lines changed

8 files changed

+321
-0
lines changed

Diff for: spring-mvc-form/pom.xml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.ripjava</groupId>
8+
<artifactId>spring-mvc-form</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
11+
12+
<name>spring-mvc-form</name>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<build>
18+
<plugins>
19+
<plugin>
20+
<groupId>org.eclipse.jetty</groupId>
21+
<artifactId>jetty-maven-plugin</artifactId>
22+
<version>9.4.24.v20191120</version>
23+
</plugin>
24+
<plugin>
25+
<groupId>org.apache.maven.plugins</groupId>
26+
<artifactId>maven-compiler-plugin</artifactId>
27+
<version>3.8.1</version>
28+
<configuration>
29+
<source>11</source>
30+
<target>11</target>
31+
</configuration>
32+
</plugin>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-war-plugin</artifactId>
36+
<version>3.2.3</version>
37+
<configuration>
38+
<!-- not hava web.xml-->
39+
<failOnMissingWebXml>false</failOnMissingWebXml>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
45+
<dependencies>
46+
<dependency>
47+
<groupId>org.springframework</groupId>
48+
<artifactId>spring-webmvc</artifactId>
49+
<version>5.2.1.RELEASE</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>javax.servlet</groupId>
53+
<artifactId>javax.servlet-api</artifactId>
54+
<version>4.0.1</version>
55+
<scope>provided</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.thymeleaf</groupId>
59+
<artifactId>thymeleaf-spring5</artifactId>
60+
<version>3.0.11.RELEASE</version>
61+
</dependency>
62+
<dependency>
63+
<groupId>ch.qos.logback</groupId>
64+
<artifactId>logback-classic</artifactId>
65+
<version>1.2.3</version>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.slf4j</groupId>
69+
<artifactId>slf4j-api</artifactId>
70+
<version>1.7.28</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>com.fasterxml.jackson.core</groupId>
74+
<artifactId>jackson-databind</artifactId>
75+
<version>2.10.1</version>
76+
</dependency>
77+
</dependencies>
78+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.ripjava.spring.mvc.form.bean;
2+
3+
public class Employee {
4+
private String name;
5+
private long id;
6+
private String contactNumber;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public void setName(String name) {
13+
this.name = name;
14+
}
15+
16+
public long getId() {
17+
return id;
18+
}
19+
20+
public void setId(long id) {
21+
this.id = id;
22+
}
23+
24+
public String getContactNumber() {
25+
return contactNumber;
26+
}
27+
28+
public void setContactNumber(String contactNumber) {
29+
this.contactNumber = contactNumber;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.ripjava.spring.mvc.form.config;
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.context.support.GenericWebApplicationContext;
7+
import org.springframework.web.servlet.DispatcherServlet;
8+
9+
import javax.servlet.ServletContext;
10+
import javax.servlet.ServletException;
11+
import javax.servlet.ServletRegistration;
12+
13+
public class AnnotationWebApplicationInitializer implements WebApplicationInitializer {
14+
15+
@Override
16+
public void onStartup(ServletContext sc) throws ServletException {
17+
AnnotationConfigWebApplicationContext root =
18+
new AnnotationConfigWebApplicationContext();
19+
root.register(WebConfig.class);
20+
21+
22+
root.setServletContext(sc);
23+
root.refresh();
24+
25+
sc.addListener(new ContextLoaderListener(root));
26+
DispatcherServlet ds = new DispatcherServlet(new GenericWebApplicationContext());
27+
ds.setEnableLoggingRequestDetails(true);
28+
ServletRegistration.Dynamic appServlet =
29+
sc.addServlet("mvc", ds);
30+
appServlet.setLoadOnStartup(1);
31+
appServlet.addMapping("/");
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.ripjava.spring.mvc.form.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.beans.factory.annotation.Qualifier;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.format.support.FormattingConversionService;
10+
import org.springframework.web.accept.ContentNegotiationManager;
11+
import org.springframework.web.servlet.ViewResolver;
12+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
13+
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
14+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
15+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
16+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
17+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
18+
import org.springframework.web.servlet.resource.ResourceUrlProvider;
19+
import org.springframework.web.servlet.view.InternalResourceViewResolver;
20+
import org.springframework.web.util.UrlPathHelper;
21+
import org.thymeleaf.spring5.SpringTemplateEngine;
22+
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
23+
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
24+
25+
@Configuration
26+
@EnableWebMvc
27+
@ComponentScan(basePackages = {"com.ripjava.spring.mvc.form"})
28+
public class WebConfig implements WebMvcConfigurer {
29+
@Autowired
30+
private ApplicationContext applicationContext;
31+
32+
@Bean
33+
public ViewResolver jspViewResolver() {
34+
InternalResourceViewResolver bean = new InternalResourceViewResolver();
35+
bean.setPrefix("/WEB-INF/views/");
36+
bean.setSuffix(".jsp");
37+
return bean;
38+
}
39+
40+
@Override
41+
public void configurePathMatch(PathMatchConfigurer configurer) {
42+
// 关闭后缀模式匹配
43+
configurer.setUseSuffixPatternMatch(false);
44+
// 矩阵变量绑定参数
45+
UrlPathHelper urlPathHelper = new UrlPathHelper();
46+
urlPathHelper.setRemoveSemicolonContent(false);
47+
configurer.setUrlPathHelper(urlPathHelper);
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ripjava.spring.mvc.form.controller;
2+
3+
4+
import com.ripjava.spring.mvc.form.bean.Employee;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.ui.ModelMap;
7+
import org.springframework.validation.BindingResult;
8+
import org.springframework.validation.annotation.Validated;
9+
import org.springframework.web.bind.annotation.ModelAttribute;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.servlet.ModelAndView;
13+
14+
@Controller
15+
@RequestMapping("/form/employee")
16+
public class EmployeeController {
17+
18+
@RequestMapping(value = "/", method = RequestMethod.GET)
19+
public ModelAndView showForm() {
20+
return new ModelAndView("formDemo", "employee", new Employee());
21+
}
22+
23+
@RequestMapping(value = "/add", method = RequestMethod.POST)
24+
public String submit(@Validated @ModelAttribute("employee") Employee employee,
25+
BindingResult result, ModelMap model) {
26+
if (result.hasErrors()) {
27+
return "error";
28+
}
29+
model.addAttribute("name", employee.getName());
30+
model.addAttribute("contactNumber", employee.getContactNumber());
31+
model.addAttribute("id", employee.getId());
32+
return "formData";
33+
}
34+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: jundongpei
4+
Date: 2020/1/9
5+
Time: 8:22 下午
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<html>
10+
<head>
11+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
12+
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
13+
</head>
14+
<body>
15+
<div class="container">
16+
<h3>请重提交员工信息</h3>
17+
<table class="table">
18+
<tr>
19+
<td><a href="/form/employee/">Retry</a></td>
20+
</tr>
21+
</table>
22+
</div>
23+
</body>
24+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: jundongpei
4+
Date: 2020/1/9
5+
Time: 8:20 下午
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<head>
10+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
11+
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
12+
</head>
13+
<body>
14+
<div class="container">
15+
<h2>提交的社员信息</h2>
16+
<table class="table">
17+
<tr>
18+
<td>Name :</td>
19+
<td>${name}</td>
20+
</tr>
21+
<tr>
22+
<td>ID :</td>
23+
<td>${id}</td>
24+
</tr>
25+
<tr>
26+
<td>Contact Number :</td>
27+
<td>${contactNumber}</td>
28+
</tr>
29+
</table>
30+
</div>
31+
</body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<%--
2+
Created by IntelliJ IDEA.
3+
User: jundongpei
4+
Date: 2020/1/9
5+
Time: 8:00 下午
6+
To change this template use File | Settings | File Templates.
7+
--%>
8+
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
9+
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
10+
<html>
11+
<head>
12+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
13+
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
14+
</head>
15+
<body>
16+
<div class="container">
17+
<h3>请输入社员的信息</h3>
18+
<form:form method="POST"
19+
action="/form/employee/add" modelAttribute="employee">
20+
<table class="table">
21+
<tr>
22+
<td><form:label path="name">Name</form:label></td>
23+
<td><form:input path="name" cssClass="form-control"/></td>
24+
</tr>
25+
<tr>
26+
<td><form:label path="id">Id</form:label></td>
27+
<td><form:input path="id" cssClass="form-control"/></td>
28+
</tr>
29+
<tr>
30+
<td><form:label path="contactNumber">
31+
Contact Number</form:label></td>
32+
<td><form:input path="contactNumber" cssClass="form-control"/></td>
33+
</tr>
34+
<tr>
35+
<td><input type="submit" value="Submit" class="btn btn-primary"/></td>
36+
</tr>
37+
</table>
38+
</form:form>
39+
</div>
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)