Mô tả
Tạo ứng dụng
login với cấu trúc như sau
Ứng dụng cho
phép hiển thị form đăng nhập gồm username, password. Khi người dùng nhập bất cứ
thông tin nào về username, password sẽ điều hướng thông tin username sang trang
khác. Trên trang này hiển thị thông tin username vừa nhập.
Thực hiện
Bước 1: Tạo
project đặt tên app-login-simple
Bước 2: Cấu
hình các file
file
dispatcher-servlet.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="codelean.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Lưu ý: <url-pattern>/</url-pattern> để DispatcherServlet duyệt toàn bộ url
Bước 3: Tạo
form đăng nhập
Tạo file
index.jsp
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<html>
<head>
<title>Spring MVC Login</title>
</head>
<body>
<form>
Username: <input name = "username" placeholder="enter your
username"/>
<hr/>
Password: <input name = "password" type="password" placeholder="enter your
password"/>
<hr/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
Bước 4: Tạo
package model với file Account.java
Tạo file Account.java
trong package model
package codelean.model;
public class Account {
private String email;
private String fullname;
private String password;
public Account(){}
public Account(String email, String fullname, String password){
this.email = email;
this.fullname = fullname;
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public String getFullname() {
return fullname;
}
public void setEmail(String email) {
this.email = email;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
}
Bước 5: Điều
chỉnh thuộc tính action của form trong file index.jsp
<form action="/login" method="post">
Bước 6: Tạo package controller với file AccountController.java
package codelean.controller;
import codelean.model.Account;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public
class AccountController
{
@GetMapping("/home")
public String login(){
return "index";
}
@PostMapping("/login")
public ModelAndView login(Model model, @RequestParam("username") String username, @RequestParam("password") String password){
Account account = new Account(username, password);
model.addAttribute("message", "Dang nhap thanh
cong");
ModelAndView modelAndView = new ModelAndView("success", "account", account);
return modelAndView;
}
}
Bước 7: Tạo file success.jsp
<%@ page contentType="text/html;charset=UTF-8"
language="java" %>
<html>
<head>
<title>Success</title>
</head>
<body>
${message}
<br/>
Welcome to
<span style="color: blue">${account.email}</span>
</body>
</html>
Bước 8: Chạy ứng dụng
Nhập username, password bất kỳ và click vào Login.
Xem mã nguồn tại đây: https://github.com/CodeLean-VN/login-simple
إرسال تعليق