Bài 6: Đăng nhập, đăng ký bằng Spring Framework

Đối với mỗi website bán hàng, tin tức… thì đa số nhà quản lý muốn quản lý thành viên của mình một cách tốt nhất, luôn muốn họ cần gì, xem gì… Ở bài viết này, mình sẽ hướng dẫn tạo chức năng đăng nhập đăng ký bằng Spring Framework.

Phần 1: Chức năng đăng ký bằng Spring Framework:

Bước 1: Bước đầu tiên để xây dựng chức năng đăng nhập đăng ký bằng Spring Framework thì yêu cầu bạn cần tạo lớp tài khoản dùng để chứa thông tin của người dùng. Tạo lớp tài khoản dùng để chứa thông tin người dùng có thể bao gồm một số thuộc tính như mã tài khoản, tên đăng nhập, mật khẩu, địa chỉ… tùy vào website mà bạn có thể biến hóa, các thông tin trên nhất định nên có.

Bước 2: Sau đó, ở lớp DAO chúng ta viết phương thức tạo mới tài khoản sau khi người dùng nhập dữ liệu xong và đảm bảo dữ liệu nhập đúng. Nhưng trước hết bạn sẽ viết phương thức kiểm tra xem email người dùng đăng ký đã tồn tại hay chưa, nếu đã tồn tại thì không cho phép đăng ký.

Phương thức tạo mới tài khoản:

Phương thức kiểm tra địa chỉ email đã tồn tại hay chưa:

Bước 3: Bạn tạo trang register.jsp dùng để nhập dữ liệu từ người dùng đăng ký

Như bên dưới mình có tạp form đăng ký bao gồm họ tên, địa chỉ email và mật khẩu cho người dùng nhập.

Chúng ta sẽ viết hàm kiểm tra xem người dùng nhập dữ liệu đúng không, có thể bắt các lỗi như bắt buộc nhập dữ liệu đầy đủ, địa chỉ email nhập đúng định dạng…

Cuối cùng, ở Controller bạn xử lý kiểm tra xem dữ liệu người dùng nhập đúng chưa, nếu đúng thì tạo tài khoản mới cho người dùng. Sau khi tạo chức năng đăng ký bằng Spring Framework chúng ta tiếp tục đến phần tạo chức năng đăng ký bằng Spring Framework.

Phần 2: Chức năng đăng nhập bằng Spring Framework:

Bước 1: Ở phần đăng nhập thì chúng ta sẽ viết phương thức nhận vào địa chỉ email và mật khẩu, sau đó kiểm tra dữ liệu người dùng nhập đúng hay sai, nếu sai thông báo lỗi.

Viết các phương thức kiểm tra đăng nhập:

Bước 2: Ở trang JSP bạn sẽ chỉnh sửa giao diện đăng nhập cho phù hợp, có thể thiết kế như bên dưới đây của mình.

Bước 3: Sau đó tạo controller nhằm để xử lý đăng nhập tài khoản bằng Spring Framework

Download source code tại đây.

Download source code link 1:

Đăng nhập, đăng ký bằng Spring Framework

Download source code link 1:

Đăng nhập, đăng ký bằng Spring Framework

(Tác giả: Tùng Dương)

27 thoughts on “Bài 6: Đăng nhập, đăng ký bằng Spring Framework

        1. Sep 08, 2017 9:35:46 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
          WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.j2ee.server:ShopWeb’ did not find a matching property.
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Server version: Apache Tomcat/7.0.77
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Server built: Mar 28 2017 16:01:48 UTC
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Server number: 7.0.77.0
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: OS Name: Windows 10
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: OS Version: 10.0
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Architecture: amd64
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Java Home: C:\Program Files\Java\jre1.8.0_144
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: JVM Version: 1.8.0_144-b01
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: JVM Vendor: Oracle Corporation
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: CATALINA_BASE: E:\Spring_Framwork\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: CATALINA_HOME: D:\Tomcat\apache-tomcat-7.0.77
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Command line argument: -Dcatalina.base=E:\Spring_Framwork\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Command line argument: -Dcatalina.home=D:\Tomcat\apache-tomcat-7.0.77
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Command line argument: -Dwtp.deploy=E:\Spring_Framwork\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Command line argument: -Djava.endorsed.dirs=D:\Tomcat\apache-tomcat-7.0.77\endorsed
          Sep 08, 2017 9:35:46 PM org.apache.catalina.startup.VersionLoggerListener log
          INFO: Command line argument: -Dfile.encoding=Cp1252
          Sep 08, 2017 9:35:46 PM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
          INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre1.8.0_144\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:/Program Files/Java/jre1.8.0_144/bin/server;C:/Program Files/Java/jre1.8.0_144/bin;C:/Program Files/Java/jre1.8.0_144/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\110\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\;C:\Program Files (x86)\Microsoft SQL Server\110\DTS\Binn\;C:\Users\It’s Me\AppData\Local\Microsoft\WindowsApps;;C:\Users\It’s Me\eclipse\jee-neon\eclipse;;.
          Sep 08, 2017 9:35:47 PM org.apache.coyote.AbstractProtocol init
          INFO: Initializing ProtocolHandler [“http-bio-8080”]
          Sep 08, 2017 9:35:47 PM org.apache.coyote.AbstractProtocol init
          INFO: Initializing ProtocolHandler [“ajp-bio-8009”]
          Sep 08, 2017 9:35:47 PM org.apache.catalina.startup.Catalina load
          INFO: Initialization processed in 2970 ms
          Sep 08, 2017 9:35:47 PM org.apache.catalina.core.StandardService startInternal
          INFO: Starting service Catalina
          Sep 08, 2017 9:35:47 PM org.apache.catalina.core.StandardEngine startInternal
          INFO: Starting Servlet Engine: Apache Tomcat/7.0.77
          Sep 08, 2017 9:35:48 PM org.apache.catalina.util.SessionIdGeneratorBase createSecureRandom
          INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [106] milliseconds.
          Sep 08, 2017 9:35:50 PM org.apache.catalina.loader.WebappClassLoaderBase validateJarFile
          INFO: validateJarFile(E:\Spring_Framwork\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ShopWeb\WEB-INF\lib\el-api-2.2.jar) – jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/el/Expression.class
          Sep 08, 2017 9:35:50 PM org.apache.catalina.deploy.WebXml setVersion
          WARNING: Unknown version string [3.1]. Default version will be used.
          Sep 08, 2017 9:35:53 PM org.apache.catalina.startup.TldConfig execute
          INFO: 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.
          Sep 08, 2017 9:35:53 PM org.apache.catalina.core.ApplicationContext log
          INFO: No Spring WebApplicationInitializer types detected on classpath
          Sep 08, 2017 9:35:54 PM org.apache.catalina.core.ApplicationContext log
          INFO: Initializing Spring FrameworkServlet ‘dispatcher’
          Sep 08, 2017 9:35:54 PM org.springframework.web.servlet.DispatcherServlet initServletBean
          INFO: FrameworkServlet ‘dispatcher’: initialization started
          Sep 08, 2017 9:35:54 PM org.springframework.web.context.support.XmlWebApplicationContext prepareRefresh
          INFO: Refreshing WebApplicationContext for namespace ‘dispatcher-servlet’: startup date [Fri Sep 08 21:35:54 ICT 2017]; root of context hierarchy
          Sep 08, 2017 9:35:54 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
          INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/dispatcher-servlet.xml]
          Sep 08, 2017 9:35:55 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
          INFO: JSR-330 ‘javax.inject.Inject’ annotation found and supported for autowiring
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.handler.SimpleUrlHandlerMapping registerHandler
          INFO: Mapped URL path [/resources/**] onto handler ‘org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0’
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/account/login],methods=[POST]}” onto public java.lang.String controller.AccountController.login(model.Users,org.springframework.ui.ModelMap,javax.servlet.http.HttpSession)
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/account/logout]}” onto public java.lang.String controller.AccountController.logout(javax.servlet.http.HttpSession,org.springframework.ui.ModelMap)
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/account/login],methods=[GET]}” onto public java.lang.String controller.AccountController.accountPage(org.springframework.ui.ModelMap,org.springframework.ui.ModelMap)
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/account/register],methods=[POST]}” onto public java.lang.String controller.AccountController.registerUser(model.Users,org.springframework.ui.ModelMap,javax.servlet.http.HttpSession)
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/home/index]}” onto public java.lang.String controller.HomePagesController.index(org.springframework.ui.ModelMap)
          Sep 08, 2017 9:35:56 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping register
          INFO: Mapped “{[/product/list]}” onto public java.lang.String controller.ProductController.getListProductByCategory(org.springframework.ui.ModelMap,java.lang.String)
          Sep 08, 2017 9:35:57 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
          INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘dispatcher-servlet’: startup date [Fri Sep 08 21:35:54 ICT 2017]; root of context hierarchy
          Sep 08, 2017 9:35:57 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
          INFO: Looking for @ControllerAdvice: WebApplicationContext for namespace ‘dispatcher-servlet’: startup date [Fri Sep 08 21:35:54 ICT 2017]; root of context hierarchy
          Sep 08, 2017 9:35:57 PM org.springframework.web.servlet.DispatcherServlet initServletBean
          INFO: FrameworkServlet ‘dispatcher’: initialization completed in 3341 ms
          Sep 08, 2017 9:35:57 PM org.apache.coyote.AbstractProtocol start
          INFO: Starting ProtocolHandler [“http-bio-8080”]
          Sep 08, 2017 9:35:57 PM org.apache.coyote.AbstractProtocol start
          INFO: Starting ProtocolHandler [“ajp-bio-8009”]
          Sep 08, 2017 9:35:57 PM org.apache.catalina.startup.Catalina start
          INFO: Server startup in 10099 ms
          Sep 08, 2017 9:36:02 PM org.hibernate.Version logVersion
          INFO: HHH000412: Hibernate Core {5.2.2.Final}
          Sep 08, 2017 9:36:02 PM org.hibernate.cfg.Environment
          INFO: HHH000206: hibernate.properties not found
          Sep 08, 2017 9:36:02 PM org.hibernate.cfg.Environment buildBytecodeProvider
          INFO: HHH000021: Bytecode provider name : javassist
          Sep 08, 2017 9:36:02 PM org.hibernate.boot.jaxb.internal.stax.LocalXmlResourceResolver resolveEntity
          WARN: HHH90000012: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/hibernate-configuration. Use namespace http://www.hibernate.org/dtd/hibernate-configuration instead. Support for obsolete DTD/XSD namespaces may be removed at any time.
          Sep 08, 2017 9:36:03 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager
          INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
          Sep 08, 2017 9:36:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
          WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
          Sep 08, 2017 9:36:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
          INFO: HHH10001005: using driver [net.sourceforge.jtds.jdbc.Driver] at URL [jdbc:jtds:sqlserver://localhost:1433;databaseName=shopweb]
          Sep 08, 2017 9:36:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
          INFO: HHH10001001: Connection properties: {}
          Sep 08, 2017 9:36:03 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
          INFO: HHH10001003: Autocommit mode: false
          Sep 08, 2017 9:36:03 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections
          INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
          Sep 08, 2017 9:36:03 PM org.hibernate.dialect.Dialect
          INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
          Sep 08, 2017 9:36:04 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
          INFO: HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4
          Sep 08, 2017 9:36:06 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
          INFO: HHH000397: Using ASTQueryTranslatorFactory
          Hibernate: select category0_.categoryID as category1_0_, category0_.categoryName as category2_0_ from dbo.category category0_
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Sep 08, 2017 9:36:07 PM org.springframework.web.servlet.PageNotFound noHandlerFound
          WARNING: No mapping found for HTTP request with URI [/ShopWeb/home/%3Cc:url%20value=] in DispatcherServlet with name ‘dispatcher’
          Hibernate: select category0_.categoryID as category1_0_, category0_.categoryName as category2_0_ from dbo.category category0_
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select listproduc0_.categoryID as category6_1_0_, listproduc0_.productID as productI1_1_0_, listproduc0_.productID as productI1_1_1_, listproduc0_.categoryID as category6_1_1_, listproduc0_.productDescription as productD2_1_1_, listproduc0_.productImage as productI3_1_1_, listproduc0_.productName as productN4_1_1_, listproduc0_.productPrice as productP5_1_1_ from dbo.product listproduc0_ where listproduc0_.categoryID=?
          Hibernate: select users0_.userID as userID1_2_, users0_.userEmail as userEmai2_2_, users0_.userName as userName3_2_, users0_.userPass as userPass4_2_, users0_.userRole as userRole5_2_ from dbo.users users0_ where users0_.userEmail=?
          Hibernate: insert into dbo.users (userEmail, userName, userPass, userRole, userID) values (?, ?, ?, ?, ?)
          Sep 08, 2017 9:36:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
          WARN: SQL Error: 544, SQLState: 23000
          Sep 08, 2017 9:36:59 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
          ERROR: Cannot insert explicit value for identity column in table ‘Users’ when IDENTITY_INSERT is set to OFF.
          Sep 08, 2017 9:36:59 PM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release
          INFO: HHH000010: On release of batch it still contained JDBC statements
          Sep 08, 2017 9:36:59 PM org.hibernate.internal.ExceptionMapperStandardImpl mapManagedFlushFailure
          ERROR: HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
          javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement
          at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)
          at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)
          at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)
          at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1411)
          at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:475)
          at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3168)
          at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2382)
          at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
          at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)
          at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
          at dao.AccountDao.insertUser(AccountDao.java:19)
          at controller.AccountController.registerUser(AccountController.java:62)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
          at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
          at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
          at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
          at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
          at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
          at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
          at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
          at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
          at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
          at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
          at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
          at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:218)
          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
          at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
          at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
          at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
          at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
          at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
          at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
          at java.lang.Thread.run(Unknown Source)
          Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
          at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:112)
          at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111)
          at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:97)
          at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:207)
          at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:45)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2934)
          at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3434)
          at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:89)
          at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:582)
          at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:456)
          at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
          at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
          at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1405)
          … 47 more
          Caused by: java.sql.SQLException: Cannot insert explicit value for identity column in table ‘Users’ when IDENTITY_INSERT is set to OFF.
          at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
          at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
          at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
          at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:671)
          at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:613)
          at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:572)
          at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeUpdate(JtdsPreparedStatement.java:727)
          at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
          … 56 more
          Sep 08, 2017 9:36:59 PM org.springframework.web.servlet.PageNotFound noHandlerFound
          WARNING: No mapping found for HTTP request with URI [/ShopWeb/account/%3Cc:url%20value=] in DispatcherServlet with name ‘dispatcher’
          đây ạ

          1. trần văn cừ

            - Edit

            model đây ạ:

            package model;

            import javax.persistence.Entity;
            import javax.persistence.Id;
            @Entity(name=”users”)
            public class Users {
            @Id
            private long userID;
            private String userName;
            private String userEmail;
            private String userPass;
            private boolean userRole;
            public long getUserID() {
            return userID;
            }
            public void setUserID(long userID) {
            this.userID = userID;
            }
            public String getUserName() {
            return userName;
            }
            public void setUserName(String userName) {
            this.userName = userName;
            }
            public String getUserEmail() {
            return userEmail;
            }
            public void setUserEmail(String userEmail) {
            this.userEmail = userEmail;
            }
            public String getUserPass() {
            return userPass;
            }
            public void setUserPass(String userPass) {
            this.userPass = userPass;
            }
            public boolean isUserRole() {
            return userRole;
            }
            public void setUserRole(boolean userRole) {
            this.userRole = userRole;
            }
            public Users() {
            super();
            // TODO Auto-generated constructor stub
            }
            public Users(long userID, String userName, String userEmail, String userPass, boolean userRole) {
            super();
            this.userID = userID;
            this.userName = userName;
            this.userEmail = userEmail;
            this.userPass = userPass;
            this.userRole = userRole;
            }

            }

          2. public class AccountDao {
            public boolean insertUser(Users user) {
            Session session = null;
            try {

            session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction transaction = session.beginTransaction();
            session.save(user);

            transaction.commit();
            return true;
            } catch (Exception e) {
            e.printStackTrace();
            }
            return false;
            }

          3. Bạn ơi mình thấy code ban viết ok mỉnh chạy thử cũng ok.
            Bạn thử cho khóa chính của Users tự động tăng coi còn lỗi không nha.

  1. ad giúp em với ạ phần register

    HTTP Status 500 – java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘userForm’ available as request attribute

    type Exception report

    message java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘userForm’ available as request attribute

    description The server encountered an internal error that prevented it from fulfilling this request.

    exception
    org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘userForm’ available as request attribute
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:560)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    root cause
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘userForm’ available as request attribute
    org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:168)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:188)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:154)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:132)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:116)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:422)
    org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:84)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
    org.apache.jsp.WEB_002dINF.pages.register_jsp._jspx_meth_form_005fpassword_005f0(register_jsp.java:338)
    org.apache.jsp.WEB_002dINF.pages.register_jsp._jspService(register_jsp.java:197)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:209)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:266)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225)
    org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    note The full stack trace of the root cause is available in the Apache Tomcat/8.0.45 logs.

    Apache Tomcat/8.0.45

Bình luận

Loading...