User management written in standard Struts2 + mvc
This week's experiment was finally completed. In a word, Sparrow is small and dirty. I believe that students who have just learned struts2 can better understand the struts structure and mvc through this experiment.
I wrote it before login. Here, I will go directly to user management.
To use struts2, you must create a struts. xml file in the src folder. The file name cannot be written incorrectly. The action defined by the entire project can be directly pasted here, and you can understand the syntax. You can change it according to the project in the future.
/Index. jsp
QueryAll
QueryAll
QueryAll
Edit. jsp
WebRoot/WEB-INF has a web. xml, You have to define the struts filter, directly look at the Code clearly. This part is fixed and can be copied directly later.
index.jsp
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
User Model User. java, the model has some attributes and getter and setter methods. If you do not want to manually write all getter and setter methods each time, you can right-click and select Generate Getter and Setters... set the Generation Method
public class User {public int id;public String username;public String password;public int getId() {return id;}public void setId(int id) {this.id = id;}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;}}The next DataBaseConn. java is used to obtain the database connection.
import java.sql.*;public class DataBaseConn {static Connection conn;public static Connection getConnection(){try {Class.forName("com.mysql.jdbc.Driver");conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "686175");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return conn;}public void close(){if(conn!=null){try {conn.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}QueryUpdate. java, business logic layer
import java.util.ArrayList;import java.sql.*;public class QueryUpdate {Connection conn;Statement stmt;ResultSet rs;String sql=null;public QueryUpdate(){conn=DataBaseConn.getConnection();try {stmt=conn.createStatement();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public ArrayList
queryAll(){ArrayList
users=new ArrayList
();try {sql="select * from user";rs=stmt.executeQuery(sql);while(rs.next()){User user=new User();user.setId(rs.getInt(1));user.setUsername(rs.getString(2));user.setPassword(rs.getString(3));users.add(user);}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return users;}public User queryUserById(int id){User user=new User();sql="select * from user where id='"+id+"'";try {rs=stmt.executeQuery(sql);if(rs.next()){user.setId(rs.getInt(1));user.setUsername(rs.getString(2));user.setPassword(rs.getString(3));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return user;}public void delete(int id){sql="delete from user where id='"+id+"'";try {stmt.executeUpdate(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void insert(String username,String password){try {int count=1;sql="select * from user";rs=stmt.executeQuery(sql);if(rs.next()){rs.last();count=rs.getInt(1)+1;}sql="insert into user values('"+count+"','"+username+"','"+password+"')";stmt.executeUpdate(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} public void edit(int id,String username,String password){try {sql="update user set username='"+username+"', password='"+password+"' where id='"+id+"'";stmt.executeUpdate(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
The following is an Action class UserAction. java, which is handled by action in struts.
import java.util.ArrayList;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UserAction extends ActionSupport{int id;String username;String password;public int getId() {return id;}public void setId(int id) {this.id = id;}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 String queryAll(){QueryUpdate qu=new QueryUpdate();ArrayList
users=new ArrayList
();users=qu.queryAll();HttpServletRequest request=ServletActionContext.getRequest();request.setAttribute("all", users);return "success";}public String queryUserById(){QueryUpdate qu=new QueryUpdate();User user=new User();user=qu.queryUserById(this.getId());HttpServletRequest request=ServletActionContext.getRequest();request.setAttribute("user", user);return "success";}public String delete(){QueryUpdate qu=new QueryUpdate();qu.delete(this.getId());return "success";}public String insert(){QueryUpdate qu=new QueryUpdate();qu.insert(this.getUsername(), this.getPassword());return "success";}public String edit(){QueryUpdate qu=new QueryUpdate();qu.edit(this.getId(),this.getUsername(),this.getPassword());return "success";}}
Presentation Layer
Index. jsp
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> My JSP 'index. jsp 'starting page
| No. |
Account |
Password |
Edit |
Delete |
| |
|
|
"> Modify |
"> Delete |
Add new user
Edit update information edit. jsp. I wrote it with the struts tag, but I don't know why it won't be compiled. Later, I heard my colleagues use a common jsp.
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ page isELIgnored="false"%> My JSP 'edit.jsp' starting page
If the number is set to read-only, you can also use disabled = "true". The difference is that the disabled interface is gray and the id value is not clear. Although readonly looks similar to username and password, if you want to delete the id, the program will jump back to index. jsp, da
Add new user insert. java
<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% @ taglib prefix =" s "uri ="/struts-tags "%> My JSP 'HTTP: // blog.csdn.net/lindonglian/article/details/insert.jsp' starting page
Enter http: // localhost: 8080/struts2_mvc/queryAll. action in the browser to go to the homepage.