Issue
I am trying to create a simple Web application to register user. I am facing a problem that I can't get the data from the HTML form in my controller method. I have tried a lot of stuffs but gone in vain
Main class:
package main;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
@SpringBootApplication
public class Web1Application {
public static void main(String[] args) {
SpringApplication.run(Web1Application.class, args);
}
}
Controller class
package main.controller;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import main.models.Test;
@Component
@Controller
public class WebController {
@RequestMapping("/register")
public ModelAndView getRegistrationPage() {
ModelAndView mv = new ModelAndView();
mv.setViewName("register.jsp");
return mv;
}
@RequestMapping("/saveuser")
public void handle(Test user) {
System.out.println(user);
}
}
Here is my Test class I was using some other class but is was testing so ..
Entity class
package main.models;
import javax.persistence.Entity;
import javax.persistence.Id;
import org.springframework.stereotype.Component;
@Component
@Entity
public class Test {
@Id
private int id;
private String name;
private String username;
private String password;
//getters + setters + toString
}
Here is my html form tag. I am using this code in jasper file.
register HTML file
<form action="/saveuser" method="post">
<label for="id">Name: </label>
<input type="number" name="id">
<label for="name">Name: </label>
<input type="text" name="name">
<label for="username">User Name: </label>
<input type="text" name="username">
<label for="password">Password: </label>
<input type="password" name="password">
<input type="submit" name="submit" value="Submit">
</form>
POM dependencies
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.35</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Eclipse IDE console log
2020-06-03 12:15:46.301 INFO 8260 --- [ main] main.Web1Application : Starting Web1Application on DESKTOP-1FI2H1T with PID 8260 (C:\Users\Pancham Goyal\git\Web-Repository\Web-1\target\classes started by Pancham Goyal in C:\Users\Pancham Goyal\git\Web-Repository\Web-1)
2020-06-03 12:15:46.306 INFO 8260 --- [ main] main.Web1Application : No active profile set, falling back to default profiles: default
2020-06-03 12:15:47.455 INFO 8260 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-06-03 12:15:47.555 INFO 8260 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 87ms. Found 1 JPA repository interfaces.
2020-06-03 12:15:48.376 INFO 8260 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-06-03 12:15:48.389 INFO 8260 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-06-03 12:15:48.389 INFO 8260 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-06-03 12:15:48.801 INFO 8260 --- [ main] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2020-06-03 12:15:48.807 INFO 8260 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-06-03 12:15:48.808 INFO 8260 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2436 ms
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
2020-06-03 12:15:49.152 INFO 8260 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-03 12:15:49.214 INFO 8260 --- [ task-1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-03 12:15:49.261 WARN 8260 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-06-03 12:15:49.302 INFO 8260 --- [ task-1] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.15.Final
2020-06-03 12:15:49.538 INFO 8260 --- [ task-1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-03 12:15:49.547 INFO 8260 --- [ main] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@7f92b990, org.springframework.security.web.context.SecurityContextPersistenceFilter@2f1ea80d, org.springframework.security.web.header.HeaderWriterFilter@34cf294c, org.springframework.security.web.csrf.CsrfFilter@4f4c789f, org.springframework.security.web.authentication.logout.LogoutFilter@c808207, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@778db7c5, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@f1a45f8, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1ffcf674, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@31e04b13, org.springframework.security.web.session.SessionManagementFilter@604b1e1d, org.springframework.security.web.access.ExceptionTranslationFilter@21618fa7, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7a8136b3]
2020-06-03 12:15:49.840 INFO 8260 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2020-06-03 12:15:49.841 WARN 8260 --- [ task-1] com.zaxxer.hikari.util.DriverDataSource : Registered driver with driverClassName=com.mysql.jdbc.Driver was not found, trying direct instantiation.
2020-06-03 12:15:50.030 INFO 8260 --- [ task-1] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-06-03 12:15:50.049 INFO 8260 --- [ task-1] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-06-03 12:15:51.001 INFO 8260 --- [ task-1] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-06-03 12:15:51.013 INFO 8260 --- [ task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-06-03 12:15:51.815 INFO 8260 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-06-03 12:15:51.817 INFO 8260 --- [ main] DeferredRepositoryInitializationListener : Triggering deferred initialization of Spring Data repositories…
2020-06-03 12:15:51.917 INFO 8260 --- [ main] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-06-03 12:15:51.932 INFO 8260 --- [ main] main.Web1Application : Started Web1Application in 6.27 seconds (JVM running for 8.203)
After submitting form on page i get this white label error on screen Error After submit press
Error on chrome After submit press
file pattern
Thank You :)
Solution
I didn't use POST on the controller that's why I was getting this error and there were many other mistakes. Thanks!
Answered By - Naveed Khan
Answer Checked By - Clifford M. (JavaFixing Volunteer)