CRUD Login Show Insert Update Delete Struts2 Framework
struts.xml
Download Project
Create Database mysql workbench
CREATE DATABASE `struts` /*!40100 DEFAULT CHARACTER SET utf8 */;
CREATE TABLE `login` ( `username` varchar(25) NOT NULL, `password` varchar(25) NOT NULL, PRIMARY KEY (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `product` ( `id` int(11) DEFAULT NULL, `name` varchar(25) DEFAULT NULL, `price` float DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Product.java
Java Struts 2 Framework 2016
package model; /** * * @author Lonely */ public class Product { private int id; private String name; private float price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public float getPrice() { return price; } public void setPrice(float price) { this.price = price; } public Product(int id, String name, float price) { this.id = id; this.name = name; this.price = price; } public Product() { } }
DataProcess.java
Java Struts 2 Framework 2016
package model;
import java.sql.Connection;
import java.sql.DriverManager;
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;
/**
*
* @author Lonely
*/
public class DataProcess {
private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;
public DataProcess() {
conn = getConnection();
}
//NOTE: import library connection mysql
//C:\Program Files\MySQL\Connector.J 5.1\mysql-connector-java-5.1.38-bin.jar
public Connection getConnection() {
if (conn != null) {
return conn;
} else {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/struts", "root", "1234567");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
public boolean checkLogin(String username, String password) {
String sql = "Select * from login Where username=? and password=?";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
int count = 0;
while (rs.next()) {
count++;
}
if (count == 1) {
return true;
}
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return false;
}
public boolean insertData(int id, String name, float price) {
int result = 0;
String sql = "insert into product values (?,?,?);";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.setString(2, name);
stmt.setFloat(3, price);
result = stmt.executeUpdate();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return result > 0;
}
public boolean deleteData(int id) {
int result = 0;
String sql = "delete from product where id=?;";
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
result = stmt.executeUpdate();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return result > 0;
}
public boolean updateData(int id, String name, float price) {
int result = 0;
String sql = "update product Set name=?, price=? where id=?";
try {
stmt = conn.prepareStatement(sql);
stmt.setString(1, name);
stmt.setFloat(2, price);
stmt.setInt(3, id);
result = stmt.executeUpdate();
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return result > 0;
}
public List<Product> getList() {
List<Product> list = new ArrayList();
String sql = "select * from product";
try {
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while (rs.next()) {
Product P = new Product(rs.getInt(1), rs.getString(2), rs.getFloat(3));
list.add(P);
}
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
public Product getByID(int id) {
String sql = "select * from product where id=?";
Product p= null;
try {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
rs = stmt.executeQuery();
while (rs.next()) {
p = new Product(rs.getInt(1), rs.getString(2), rs.getFloat(3));
}
stmt.close();
} catch (SQLException ex) {
Logger.getLogger(DataProcess.class.getName()).log(Level.SEVERE, null, ex);
}
return p;
}
}
model
LoginAction.java
Java Struts 2 Framework 2016
package app;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import model.DataProcess;
import model.Product;
/**
*
* @author Lonely
*/
public class LoginAction extends ActionSupport {
private String username;
private String password;
private List<Product> list;
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public LoginAction() {
}
@Override
public String execute() throws Exception {
DataProcess DP = new DataProcess();
if (DP.checkLogin(username, password)) {
list = DP.getList();
return "success";
}
return "fail";
}
}
CrudAction.java
Java Struts 2 Framework 2016
package app;
import java.util.List;
import model.DataProcess;
import model.Product;
/**
*
* @author Lonely
*/
public class CrudAction {
private int id;
private String name;
private float price;
private List<Product> list;
private Product p;
public Product getP() {
return p;
}
public void setP(Product p) {
this.p = p;
}
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public CrudAction() {
}
public String delete() throws Exception {
DataProcess DP = new DataProcess();
if (DP.deleteData(id)) {
return "success";
}
return "fail";
}
public String insert() throws Exception {
DataProcess DP = new DataProcess();
if (DP.insertData(id, name, price)) {
return "success";
}
return "fail";
}
public String update() throws Exception {
DataProcess DP = new DataProcess();
if (DP.getByID(id) != null) {
p = DP.getByID(id);
return "success";
}
return "fail";
}
public String doUpdate() throws Exception {
DataProcess DP = new DataProcess();
if (DP.updateData(id, name, price)) {
return "success";
}
return "fail";
}
public String view() throws Exception {
DataProcess DP = new DataProcess();
if (DP.getList() != null) {
list = DP.getList();
return "success";
}
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="login" class="app.LoginAction">
<result name="success">welcome.jsp</result>
<result name="fail">invalid.jsp</result>
</action>
<action name="view" class="app.CrudAction" method="view">
<result name="success">welcome.jsp</result>
<result name="fail">fail.jsp</result>
</action>
<action name="delete" class="app.CrudAction" method="delete">
<result name="success" type="chain">view</result>
<result name="fail">fail.jsp</result>
</action>
<action name="insert" class="app.CrudAction" method="insert">
<result name="success" type="chain">view</result>
<result name="fail">fail.jsp</result>
</action>
<action name="update" class="app.CrudAction" method="update">
<result name="success">update.jsp</result>
<result name="fail">fail.jsp</result>
</action>
<action name="doUpdate" class="app.CrudAction" method="doUpdate">
<result name="success" type="chain">view</result>
<result name="fail">fail.jsp</result>
</action>
</package>
</struts>
Web Pages
index.jsp
Java Struts 2 Framework 2016
<%--
Document : index
Created on : Oct 24, 2016, 10:33:14 AM
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>Login Page</title>
</head>
<body>
<center>
<h1>Login Page</h1>
<s:form action="login" method="POST">
<s:textfield name="username" label="Username:" placeholder="Enter your username"></s:textfield>
<s:password name="password" label="Password:" placeholder="Enter your password"></s:password>
<s:submit value="Login"/>
<s:reset value="Reset"/>
</s:form>
//Viết css cho struts cssClass="button"
</center>
</body>
</html>
welcome.jsp
Java Struts 2 Framework 2016
<%--
Document : welcome
Created on : Oct 24, 2016, 10:33:25 AM
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>Welcome</title>
</head>
<body>
<center>
<font color="red">Welcome to page</font>
<table border="1">
<tr>
<th>No</th>
<th>ID</th>
<th>Name</th>
<th>Price</th>
<th colspan="3">Action</th>
</tr>
<s:iterator var="L" value="list" status="number">
<tr>
<td><s:property value="#number.count"/></td>
<td><s:property value="%{id}"/></td>
<td><s:property value="#L.name"/></td>
<td><s:property value="#L.price"/></td>
<td><s:a href="insert.jsp"> <input type="button" value="Insert" /></s:a></td>
<td><a href="update?id=<s:property value="#L.id"/>"><input type="button" value="Update" /> </a></td>
<td><a href="delete?id=<s:property value="#L.id"/>"><input type="button" value="Delete" /></a></td>
</tr>
</s:iterator>
</table>
</center>
</body>
</html>
invalid.jsp
Java Struts 2 Framework 2016
<%--
Document : newjspinvalid
Created on : Oct 24, 2016, 10:33:30 AM
Author : Lonely
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Invalid</title>
</head>
<body>
<center>
<font color="red">Invalid please check username or password! </font>
</center>
</body>
</html>
insert.jsp
Java Struts 2 Framework 2016
<%--
Document : insert
Created on : Oct 26, 2016, 8:26:12 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>Insert Page</title>
</head>
<body>
<center>
<h1>Insert Pages</h1>
<s:form action="insert" method="POST">
<s:textfield name="id" />
<s:textfield name="name" />
<s:textfield name="price" />
<s:submit value="Add"/>
</s:form>
</center>
</body>
</html>
update.jsp
Java Struts 2 Framework 2016
<%--
Document : insert
Created on : Oct 26, 2016, 8:26:12 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>Insert Page</title>
</head>
<body>
<center>
<h1>Insert Pages</h1>
<s:form action="doUpdate" method="POST">
<s:textfield name="id" value="%{p.id}" />
<s:textfield name="name" value="%{p.name}"/>
<s:textfield name="price" value="%{p.price}"/>
<s:submit value="Add"/>
</s:form>
</center>
</body>
</html>
fail.jsp
Java Struts 2 Framework 2016
<%--
Document : newjspinvalid
Created on : Oct 24, 2016, 10:33:30 AM
Author : Lonely
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error</title>
</head>
<body>
<center>
<font color="red">Error page </font>
</center>
</body>
</html>
Images project
Hiển thi bằng <s:property value="#L.name"/> tránh gặp lỗi XSS
Download Project
Download (Zip import NetBeans)
This comment has been removed by a blog administrator.
ReplyDeleteBạn giải thích giúp mình tag này được không : <s:property value="#L.name"
ReplyDeletedấu # sử dụng truy cập giá trị ngoài value stack chứ nhỉ?