I am new to the spring framework. I am trying to run the simple spring mvc applications . As far i can see the controller has the valid path and i have imported all the required dependency into the project(maven ). Before I run the project I used the command clean install to run it successful but its returning HTTP 404 NOT found error.
Here is my project structure.
project structure is here
Here is the code for Configuration.
Here is the code Configuration.
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.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "net.javaguides.springmvc.form" })
public class MVCConfig implements WebMvcConfigurer{
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
Here is the code for sign up controller controller.
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import net.javaguides.springmvc.form.model.SignUpForm;
@Controller
public class SignUpController {
/**
* Create new signUpForm object for empty from
*
* @return
*/
@ModelAttribute("signUpForm")
public SignUpForm setSignUpForm() {
return new SignUpForm();
}
/**
* Method to show the initial HTML form
*
* @return
*/
@GetMapping("/showSignUpForm")
public String showForm() {
return "signup-form";
}
/**
* Save User sign up form
*
* @param signUpForm
* @param model
* @return
*/
@PostMapping("/saveSignUpForm")
public String saveUser(@ModelAttribute("signUpForm") SignUpForm signUpForm, Model model) {
// Implement business logic to save user details into a database
// .....
System.out.println("FirstName : " + signUpForm.getFirstName());
System.out.println("LastName : " + signUpForm.getLastName());
System.out.println("Username : " + signUpForm.getUserName());
System.out.println("Password : " + signUpForm.getPassword());
System.out.println("Email : " + signUpForm.getEmail());
model.addAttribute("message", "User SignUp successfully.");
model.addAttribute("user", signUpForm);
return "signup-success";
}
}
Here is the code for java class.
public class SignUpForm {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Here is the code for pom.xml.
<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-form-handling</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springmvc5-form-handling 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>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Servlet Dependency -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here is the screenshot when i run the applications on tomcat server.
result of the applications
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…