Trong VD này chúng ta không validate bước 2 bằng checkUser
mà bắt lỗi đã tồn tại username bằng exception tạo message
mà bắt lỗi đã tồn tại username bằng exception tạo message
register.jsp
Java Struts 2 Framework 2016
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <s:head/> //show required validate XML </head> <body> <center> <h1>Regster Page</h1> <s:if test="#request.error != ''"> <font color="red"> <s:property value="%{#request.error}"/> </font> //show register fail </s:if> <s:form action="register" method="POST"> <s:textfield name="username" label="Username"/> <s:textfield name="email" label="Email"/> <s:password name="password" label="Password"/> <s:password name="confirm" label="Confirm Password"/> <s:submit value="Register"/> </s:form> </center> </body> </html>
RegisterActionSupport.java
Java Struts 2 Framework 2016
package controller;
import DAO.RegisterDAO;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import model.Register;
/**
* Controller
* @author Lonely
*/
public class RegisterActionSupport extends ActionSupport {
private String username;
private String email;
private String password;
private String confirm;
private List<Register> list;
public List<Register> getList() {
RegisterDAO r = new RegisterDAO();
list = r.getAllRegister();
return list;
}
public void setList(List<Register> list) {
this.list = list;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm() {
return confirm;
}
public void setConfirm(String confirm) {
this.confirm = confirm;
}
public String getDisplay() {
return "success";
}
@Override
public String execute() throws Exception {
RegisterDAO r = new RegisterDAO();
list = r.getAllRegister();
if (r.register(username, email, password, confirm)) {
return "success";
} else {
return "fail";
}
}
}
struts.xml
Java Struts 2 Framework 2016
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="register" class="controller.RegisterActionSupport">
<result name="success">success.jsp</result>
<result name="fail">register.jsp</result>
<result name="input">register.jsp</result>
</action>
<action name="view" class="controller.RegisterActionSupport" method="getDisplay">
<result name="input">index.jsp</result>
<result name="success">index.jsp</result>
</action>
</package>
</struts>
success.jsp
Java Struts 2 Framework 2016
<%--
Document : success
Created on : Nov 2, 2016, 10:23:46 PM
Author : Lonely
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
//Go to list account
Click<s:a action="view" >here</s:a>go to view acount!
</body>
</html>
index.jsp
Java Struts 2 Framework 2016
<%--
Document : index
Created on : Nov 2, 2016, 10:23:39 PM
Author : Lonely
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
table, td, th{
border: 1px solid black;
}
</style>
</head>
<body>
<table style="border: 1px solid black" >
<td>Username</td>
<td>Email</td>
<td>Password</td>
<td>Confirm</td>
</th>
//Show list
<s:iterator value="list" var="in">
<tr>
<td> <s:property value="%{username}"/> </td>
<td> <s:property value="%{email}"/> </td>
<td> <s:property value="%{password}"/> </td>
<td> <s:property value="%{confirm}"/> </td>
</tr>
</s:iterator>
</table>
</body>
</html>
RegisterActionSupport-validation.xml
Java Struts 2 Framework 2016
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>User name is required.</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>Username must be between 6 to 10 characters long</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Password is required</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">20</param>
<param name="trim">true</param>
<message>Password must be between 6 to 20 characters long</message>
</field-validator>
</field>
<field name="confirm">
<field-validator type="requiredstring">
<message>cPassword is required</message>
</field-validator>
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[password == confirm]]></param>
<message>Passwords must match.</message>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<message>Email address not valid.</message>
</field-validator>
<field-validator type="email">
<message>Must provide a valid email.</message>
</field-validator>
</field>
</validators>
RegisterDAO.java
Java Struts 2 Framework 2016
/*
* Model
*/
package DAO;
import DBconnect.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import model.Register;
import org.apache.struts2.ServletActionContext;
public class RegisterDAO {
public boolean register(String u, String e, String p, String cp) {
try {
String sql = "INSERT INTO register VALUES (?, ?,?,?);";
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, u);
stmt.setString(2, e);
stmt.setString(3, p);
stmt.setString(4, cp);
return stmt.executeUpdate() > 0;
} catch (SQLException ex) {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("error", "Đã tồn tại Username này!");
}
return false;
}
public List<Register> getAllRegister() {
List<Register> list = new ArrayList();
try {
String sql = "Select * from register";
Connection conn = DBUtil.getConnection();
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Register r = new Register(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
list.add(r);
}
} catch (SQLException ex) {
Logger.getLogger(RegisterDAO.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
}
DBUtil.java
Java Struts 2 Framework 2016
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package DBconnect;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
/**
* Model
* @author Lonely
*/
public class DBUtil {
private static Connection connection = null;
public static Connection getConnection() {
if (connection != null) {
return connection;
} else {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/struts", "root", "1234567");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
}
Register.java
Java Struts 2 Framework 2016
package model;
/**
* Model
* @author Lonely
*/
public class Register {
private String username;
private String email;
private String password;
private String confirm;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getConfirm() {
return confirm;
}
public void setConfirm(String confirm) {
this.confirm = confirm;
}
public Register() {
}
public Register(String username, String email, String password, String confirm) {
this.username = username;
this.email = email;
this.password = password;
this.confirm = confirm;
}
}
1. Cách tạo file XML xem
Download (import NetBeans)
0 nhận xét:
Post a Comment