1. Trang chủ
  2. » Công Nghệ Thông Tin

Thực hành Strust JSF Lab 3

14 123 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 14
Dung lượng 222,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Step 2: Xây dựng Action cho các chức năng register,update,delete,search user - Session2Action.java import ConnectDB.connectDB; import Entity.UserEntity; importstatic com.opensymphony.xwo

Trang 1

Lab 03 Interceptors and Tags

Mục tiêu

Phần I Bài tập step by step

Bài tập: xây dựng các chức năng register,search,update,delete user

Dùng các Interceptor để filter khi user login và giữ lưới dữ liệu khi update, delete user

Phần 1: Xây dựng các chức năng register,search,update,delete User

Step 1: Sử dụng Tags để xây dựng giao diện

<%@ page contentType="text/html; charset=UTF-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<head>

<title><s:text name="Register User"/></title>

<s:head/>

<sx:head/>

</head>

<s:div>REGISTER FORM</s:div>

<s:text name="Please fill in the form below:"/>

<s:form action="register" enctype="multipart/form-data" method="post">

<s:textfield name="userName" label="UserName" size="50" required="true"/>

<s:password name="password" label="Password" size="30" required="true"/>

<s:password name="confirmpassword" label="Confirm Password" size="30" required="true"/> <s:textfield name="firstName" label="FirstName" size="50" required="true"/>

<s:textfield name="lastName" label="LastName" size="50" required="true"/>

<sx:datetimepicker name="birthDate" label="BirthDate" displayFormat="dd-MM-yyyy" value="% {'today'}"/>

<s:radio name="gender" label="Gender" list="{'Male','Female'}" value="'Male'"/>

<s:textarea name="address" label="Address"/>

<s:combobox name="birthPlace" label="BirthPlace" list="{'Ha Noi','TP HCM','Da Nang'}"

headerKey="0" headerValue=" select "/>

<s:textfield name = "phone" label="Phone" size="15" required="true"/>

<s:textfield name = "email" label="Email" size="30" required="true"/>

<s:file name = "image" label="Image"/>

<s:submit value="Register"></s:submit>

</s:form>

- searchuser.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Trang 2

<%@ taglib prefix="s" uri="/struts-tags" %>

<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Search User</title>

<s:head/>

<sx:head/>

</head>

<body>

<s:form action="SearchUser">

<s:textfield name="sr_userName" label="User Name" size="50"/>

<s:textfield name="sr_firstName" label="First Name" size="50"/>

<s:textfield name="sr_lastName" label="Last Name" size="50"/>

<s:textfield name = "sr_email" label="Email" size="30"/>

<s:submit value="Search"></s:submit>

</s:form>

<table border="1">

<thead>

<tr>

<th>No</th>

<th>UserName</th>

<th>Password</th>

<th>FirstName</th>

<th>LastName</th>

<th>Gender</th>

<th>BirthDate</th>

<th>Phone</th>

<th>Email</th>

<th>IsDelete</th>

<th>Delete</th>

<th>Update</th>

</tr>

</thead>

<tbody>

<s:iterator value="listuser" status="listuserStatus">

<tr>

<s:form action="updateUser" method="post" theme="simple">

<td><s:property value="#listuserStatus.count"/></td>

<td><s:property value="userName"/></td>

<td><s:password name="password" value="%{password}" showPassword="true"/></td> <td><s:textfield name="firstName" value="%{firstName}"/></td>

<td><s:textfield name="lastName" value="%{lastName}"/></td>

<td><s:textfield name="gender" value="%{gender}" size="6"/></td>

<td><sx:datetimepicker name="birthDate" displayFormat="dd-MM-yyyy" value="% {birthDate}"/></td>

<td><s:textfield name="phone" value="%{phone}" size="15"/></td>

<td><s:textfield name="email" value="%{email}"/></td>

<td><s:select name="isDelete" list="{'ENABLE','DISABLE','PENDING'}" value="%

{isDelete}"/></td>

<s:url id="delete" action="deleteUser">

<s:param name="userName" value="userName"/>

<s:param name="sr_userName" value="sr_userName"/>

<s:param name="sr_firstName" value = "sr_firstName"/>

<s:param name="sr_lastName" value = "sr_lastName"/>

<s:param name="sr_email" value = "sr_email"/>

</s:url>

Trang 3

<td><s:a href="%{delete}">Delete</s:a></td>

<td>

<s:hidden name="userName" value="%{userName}"/>

<s:hidden name="sr_userName" value="%{sr_userName}"/>

<s:hidden name="sr_firstName" value = "%{sr_firstName}"/>

<s:hidden name="sr_lastName" value = "%{sr_lastName}"/>

<s:hidden name="sr_email" value = "%{sr_email}"/>

<s:submit value="Update"/>

</td>

</s:form>

</tr>

</s:iterator>

</tbody>

</table>

</body>

</html>

- success.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<html>

<head>

<title><s:text name="Registration Successful"/></title>

<s:head/>

</head>

<body>

<h1>Registration Successful.</h1>

<h2>Welcome User: <s:property value="userName"/> Please wait for admin approval!</h2> <h4><s:a href="/Session1/login.jsp">Back</s:a> to login</h4>

</body>

</html>

- userinformation.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>User Information</title>

<s:head/>

</head>

<body>

<s:iterator value="listuser" status="listuserStatus">

<img src="Upload/minhvt11834961_864714880289547_134881636332923872_o.jpg" width="100" height="100"/><br/>

UserName: <s:property value="userName"/><br/>

FirstName: <s:property value="firstName"/><br/>

LastName: <s:property value="lastName"/><br/>

BirthDate: <s:property value="birthdate"/><br/>

Gender: <s:property value="gender"/><br/>

Address: <s:property value="address"/><br/>

BirthPlace: <s:property value="birthPlace"/><br/>

Phone: <s:property value="phone"/><br/>

Email: <s:property value="email"/><br/>

<s:if test="group_id == 1">

<s:form action="SearchUser">

<s:submit value="admin"></s:submit>

Trang 4

</s:form>

</s:if>

</s:iterator>

</body>

</html>

Step 2: Xây dựng Action cho các chức năng register,update,delete,search user

- Session2Action.java

import ConnectDB.connectDB;

import Entity.UserEntity;

importstatic com.opensymphony.xwork2.Action.ERROR;

importstatic com.opensymphony.xwork2.Action.SUCCESS;

import com.opensymphony.xwork2.ActionSupport;

import java.io.File;

import java.util.ArrayList;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import org.apache.struts2.interceptor.ServletRequestAware;

/**

*

* @author DELL

*/

publicclass Session2Action extends ActionSupport implements ServletRequestAware{

private String userName;

private String password;

private String confirmpassword;

private String firstName;

private String lastName;

private Date birthDate;

private String gender;

private String address;

private String birthPlace;

private String phone;

private String email;

private String isDelete;

private File image;

private String imageContentType;

private String imageFileName;

private HttpServletRequest servletRequest;

ArrayList<Entity.UserEntity> listuser = new ArrayList();

private String sr_userName;

private String sr_firstName;

private String sr_lastName;

private String sr_email;

public String getUserName() {

return userName;

}

publicvoid setUserName(String userName) {

this.userName = userName;

}

public String getPassword() {

return password;

Trang 5

}

publicvoid setPassword(String password) {

this.password = password;

}

public String getConfirmpassword() {

return confirmpassword;

}

publicvoid setConfirmpassword(String confirmpassword) {

this.confirmpassword = confirmpassword;

}

public String getFirstName() {

return firstName;

}

publicvoid setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

publicvoid setLastName(String lastName) {

this.lastName = lastName;

}

public Date getBirthDate() {

return birthDate;

}

publicvoid setBirthDate(Date birthDate) {

this.birthDate = birthDate;

}

public String getGender() {

return gender;

}

publicvoid setGender(String gender) {

this.gender = gender;

}

public String getAddress() {

return address;

}

publicvoid setAddress(String address) {

this.address = address;

}

public String getBirthPlace() {

return birthPlace;

}

Trang 6

publicvoid setBirthPlace(String birthPlace) {

this.birthPlace = birthPlace;

}

public String getPhone() {

return phone;

}

publicvoid setPhone(String phone) {

this.phone = phone;

}

public String getEmail() {

return email;

}

publicvoid setEmail(String email) {

this.email = email;

}

public String getIsDelete() {

return isDelete;

}

publicvoid setIsDelete(String isDelete) {

this.isDelete = isDelete;

}

public File getImage() {

return image;

}

publicvoid setImage(File image) {

this.image = image;

}

public String getImageContentType() {

return imageContentType;

}

publicvoid setImageContentType(String imageContentType) {

this.imageContentType = imageContentType;

}

public String getImageFileName() {

return imageFileName;

}

publicvoid setImageFileName(String imageFileName) {

this.imageFileName = imageFileName;

}

public HttpServletRequest getServletRequest() {

return servletRequest;

}

Trang 7

@Override

publicvoid setServletRequest(HttpServletRequest servletRequest) {

this.servletRequest = servletRequest;

}

public ArrayList<UserEntity> getListuser() {

return listuser;

}

publicvoid setListuser(ArrayList<UserEntity> listuser) {

this.listuser = listuser;

}

public String getSr_userName() {

return sr_userName;

}

publicvoid setSr_userName(String sr_userName) {

this.sr_userName = sr_userName;

}

public String getSr_firstName() {

return sr_firstName;

}

publicvoid setSr_firstName(String sr_firstName) {

this.sr_firstName = sr_firstName;

}

public String getSr_lastName() {

return sr_lastName;

}

publicvoid setSr_lastName(String sr_lastName) {

this.sr_lastName = sr_lastName;

}

public String getSr_email() {

return sr_email;

}

publicvoid setSr_email(String sr_email) {

this.sr_email = sr_email;

}

@Override

public String execute() throws Exception{

return SUCCESS;

}

public String register() throws Exception {

String path = "Upload/";

String filePath = this.servletRequest.getSession().getServletContext().getRealPath(path);

File uploadDir = new File(filePath);

FileUtils.copyFile(this.image, new File(uploadDir, this.userName));

String destFilep = uploadDir + this.userName;

ConnectDB.connectDB conn = new connectDB();

Trang 8

Boolean rel = conn.registerUser(getUserName(), getPassword(), getFirstName(), getLastName(), getBirthDate(), getGender(), getAddress(), getBirthPlace(), getPhone(), getEmail(), destFilep);

if (rel) {

return SUCCESS;

} else {

return ERROR;

}

}

public String searchUser() throws Exception {

ConnectDB.connectDB conn = new connectDB();

this.listuser = conn.searchAccount(sr_userName, sr_firstName, sr_lastName, sr_email);

return SUCCESS;

}

public String deleteUser() throws Exception {

ConnectDB.connectDB conn = new connectDB();

boolean result = conn.deleteAccount(userName);

if (result) {

return SUCCESS;

}

return ERROR;

}

public String updateUser() throws Exception {

ConnectDB.connectDB conn = new connectDB();

Boolean rel = conn.UpdateAccount(userName, password, firstName, lastName, birthDate, gender, phone, email, isDelete);

if (rel) {

return SUCCESS;

}

return ERROR;

}

}

publicboolean registerUser(String userName, String password, String firstName,

String lastName, Date birthDate, String gender,String address,String birthPlace,

String phone, String email, String image) throws Exception {

Connection conn = null;

PreparedStatement prest = null;

String sql = "INSERT INTO [dbo].[ST_USER] ([USERNAME],[PASSWORD],[FIRSTNAME],[LASTNAME],

[BIRTHDATE],[GENDER],[ADDRESS],[BIRTHPLACE],[PHONE],[EMAIL],[IMAGE],[ISDELETE],[GROUP_ID]) ";

sql += "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)";

try {

conn = DBconnection();

prest = conn.prepareStatement(sql);

prest.setString(1, userName);

prest.setString(2, password);

prest.setString(3, firstName);

prest.setString(4, lastName);

java.text.SimpleDateFormat sdatefmt = new SimpleDateFormat("yyyy-MM-dd");

String birthsim = sdatefmt.format(birthDate);

birthsim = birthsim.trim();

Date birthfmt = sdatefmt.parse(birthsim);

Trang 9

java.sql.Date sqlbirth = new java.sql.Date(birthfmt.getTime());

prest.setDate(5, sqlbirth);

prest.setString(6, gender);

prest.setString(7, address);

prest.setString(8, birthPlace);

prest.setString(9, phone);

prest.setString(10, email);

prest.setString(11, image);

prest.setString(12, "PENDING");

prest.setInt(12, 2);

int row = prest.executeUpdate();

if (row > 0) {

returntrue;

}

} catch (SQLException ex) {

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("SQLErrors", ex.getMessage());

} finally {

if (conn != null) {

conn.close();

}

if (prest != null) {

prest.close();

}

}

returnfalse;

}

public ArrayList<UserEntity> searchAccount(String userName, String firstName, String lastName, String email)

throws SQLException {

Connection conn = null;

PreparedStatement prest = null;

ArrayList<UserEntity> listuser = new ArrayList();

String sql = "SELECT * FROM [dbo].[ST_USER]";

sql += " WHERE USERNAME LIKE ? AND FIRSTNAME LIKE ? AND LASTNAME LIKE ? AND EMAIL LIKE ?"; try {

conn = DBconnection();

prest = conn.prepareStatement(sql);

prest.setString(1, "%" + userName + "%");

prest.setString(2, "%" + firstName + "%");

prest.setString(3, "%" + lastName + "%");

prest.setString(4, "%" + email + "%");

ResultSet rs = prest.executeQuery();

while (rs.next()) {

Entity.UserEntity user = new UserEntity();

user.setUserName(rs.getString("username"));

user.setPassword(rs.getString("password"));

user.setFirstName(rs.getString("firstname"));

user.setLastName(rs.getString("lastname"));

Date bdate = new java.sql.Date(rs.getDate("birthdate").getTime());

user.setBirthDate(bdate);

user.setGender(rs.getString("gender"));

user.setAddress(rs.getString("address"));

user.setBirthPlace(rs.getString("birthPlace"));

user.setPhone(rs.getString("phone"));

user.setEmail(rs.getString("email"));

Trang 10

user.setImage(rs.getString("image"));

user.setIsDelete(rs.getString("isdelete"));

user.setGroup_id(rs.getInt("group_id"));

listuser.add(user);

}

} catch (SQLException ex) {

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("SQLErrors", ex.getMessage());

} finally {

if (conn != null) {

conn.close();

}

if (prest != null) {

prest.close();

}

}

return listuser;

}

publicboolean deleteAccount(String userName) throws Exception {

Connection conn = null;

PreparedStatement prest = null;

String sql = "UPDATE [dbo].[ST_USER] SET ISDELETE = 'DISABLE' WHERE USERNAME = ?";

try {

conn = DBconnection();

prest = conn.prepareStatement(sql);

prest.setString(1, userName);

int row = prest.executeUpdate();

if (row > 0) {

returntrue;

}

} catch (SQLException ex) {

HttpServletRequest request = ServletActionContext.getRequest();

request.setAttribute("SQLErrors", ex.getMessage());

} finally {

if (conn != null) {

conn.close();

}

if (prest != null) {

prest.close();

}

}

returnfalse;

}

publicboolean UpdateAccount(String userName, String password, String firstName,

String lastName, Date birthDate, String gender, String phone, String email,String isDelete) throws Exception { Connection conn = null;

PreparedStatement prest = null;

String sql = "UPDATE [dbo].[ST_USER] ";

sql += "SET FIRSTNAME=?,LASTNAME=?,BIRTHDATE=?,GENDER=?,EMAIL=?,PHONE=?,ISDELETE=? ";

sql += "WHERE USERNAME = ?";

try {

conn = DBconnection();

prest = conn.prepareStatement(sql);

prest.setString(1, firstName);

prest.setString(2, lastName);

java.text.SimpleDateFormat sdatefmt = new SimpleDateFormat("yyyy-MM-dd");

Ngày đăng: 07/05/2018, 16:04

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w