Implement crud in struts2

Source: Internet
Author: User

Crud is the abbreviation of create, read, update, and delete. It is a microcosm of common applications.

View instances:

Employee. Java class:

Package com. LLP. Crud;

Public class employee {
Private string employeeid;
Private string firstname;
Private string lastname;
Public String getemployeeid (){
Return employeeid;
}
Public void setemployeeid (string employeeid ){
This. employeeid = employeeid;
}
Public String getfirstname (){
Return firstname;
}
Public void setfirstname (string firstname ){
This. firstname = firstname;
}
Public String getlastname (){
Return lastname;
}
Public void setlastname (string lastname ){
This. lastname = lastname;
}
@ Override
Public String tostring (){
Return "Employee [employeeid =" + employeeid + ", firstname ="
+ Firstname + ", lastname =" + lastname + "]";
}
 
}

 

Operation class: Dao. Java, the operation set for data in this class: This class does not implement database connection operations, but only uses the set to simulate;

Package com. LLP. Crud;
Import java. util .*;
Public class Dao {
Private Static Map <string, employee> EMPs = new hashmap <string, employee> ();
 
/* The static initiator initializes the class, that is, the member variables that belong to the class,
* Automatically implemented by the system when the class is loaded such as memory
*
**/
Static {
Long id = system. currenttimemillis ();

Employee EMP = new employee ();
EMP. setemployeeid (ID + "");
EMP. setfirstname ("AA ");
EMP. setlastname ("AA ");

EMPs. Put (EMP. getemployeeid (), EMP );

EMP = new employee ();
EMP. setemployeeid (ID + 1 + "");
EMP. setfirstname ("BB ");
EMP. setlastname ("BB ");

EMPs. Put (EMP. getemployeeid (), EMP );

EMP = new employee ();
EMP. setemployeeid (ID + 2 + "");
EMP. setfirstname ("cc ");
EMP. setlastname ("cc ");

EMPs. Put (EMP. getemployeeid (), EMP );


}
 
/* Return an existing set */
Public list <employee> findall (){

Return new arraylist <employee> (EMPs. Values ());
}
 
/* Save */
Public void save (employee EMP ){
String id = system. currenttimemillis () + "";
EMP. setemployeeid (ID );
EMPs. Put (ID, EMP );
}
 
/* Update */
Public void Update (employee EMP ){
EMPs. Put (EMP. getemployeeid (), EMP );
}
 
/* Get */
Public Employee get (string employeeid ){
Employee EMP = NULL;
EMP = EMPs. Get (employeeid );
Return EMP;
}
 
/* Delete */
Public void Delete (string employeeid ){
EMPs. Remove (employeeid );
}
}

1. Do not use the modeldriven_preparable interceptor crud
Action class, employeeaction. Java:

Package com. LLP. Crud;

Import com. opensymphony. xwork2.actionsupport;
Import java. util .*;
Import org. Apache. struts2.interceptor. requestaware;

Public class employeeaction extends actionsupport implements requestaware {
 
Private Dao = New Dao ();
 
Private string employeeid;
 
Private string firstname;
 
Private string lastname;
 
Public void setemployeeid (string employeeid) {// to obtain the Relative ID value passed from the page
This. employeeid = employeeid;
}
 
 
Public String getemployeeid (){
Return employeeid;
}

Public String getfirstname (){
Return firstname;
}

Public void setfirstname (string firstname ){
This. firstname = firstname;
}

Public String getlastname (){
Return lastname;
}

Public void setlastname (string lastname ){
This. lastname = lastname;
}
 
Public String Update (){
// 1. Obtain the form information
// 2. encapsulate form information as an employee object
Employee EMP = Dao. Get (employeeid );
EMP. setemployeeid (employeeid );
EMP. setfirstname (firstname );
EMP. setlastname (lastname );

// 3. Call the update method of Dao
Dao. Update (EMP );
Return success;
}
Public string edit (){
// 1. Get the object corresponding to the employee ID from the database
Employee EMP = Dao. Get (employeeid );
// 2. Assign the attribute of the object obtained in 1 to the corresponding attribute of the current action.
Firstname = EMP. getfirstname ();
Lastname = EMP. getlastname ();

Return "edit-sucess ";
}
Public String add (){
// 1. Obtain the value of the firstname and lastname Request Parameters
// 2. encapsulate firstname and lastname as an employee object
Employee EMP = new employee ();
EMP. setfirstname (firstname );
EMP. setlastname (lastname );
// 3. Call the Save method of DAO to perform the save operation.
Dao. Save (EMP );

Return success;
}
Public String Delete (){
// 1. Get the employee ID
// 2. Call the delete method of Dao
Dao. Delete (employeeid );
Return success;
}
 
Public String list (){
// Obtain information about all the employees
// Put the employee information in the request
Requestmap. Put ("emplist", Dao. findall ());
Return "list-sucess ";
}
 
Private Map <string, Object> requestmap = NULL;
 
Public void setrequest (Map <string, Object> arg0 ){
Requestmap = arg0;
}
}

 

2. modeldriven interceptor

Package com. LLP. Crud;

Import com. opensymphony. xwork2.actionsupport;
Import com. opensymphony. xwork2.modeldriven;

Import java. util .*;
Import org. Apache. struts2.interceptor. requestaware;

Public class employeeaction extends actionsupport
Implements requestaware, modeldriven <employee> {
 
Private Dao = New Dao ();
 
Public Employee GetModel (){
EMP = new employee ();
Return EMP;
}

Public String Update (){
// 1. Obtain the form information
// 2. encapsulate form information as an employee object
// Employee EMP = Dao. Get (employeeid );
// EMP. setemployeeid (employeeid );
// EMP. setfirstname (firstname );
// EMP. setlastname (lastname );

// 3. Call the update method of Dao
Dao. Update (EMP );
Return success;
}
Public string edit (){
/// 1. Obtain the object corresponding to the employee ID from the database
// Employee EMP = Dao. Get (employeeid );
/// 2. Assign the attribute of the object obtained in 1 to the corresponding attribute of the current action.
// Firstname = EMP. getfirstname ();
// Lastname = EMP. getlastname ();
Employee emp2 = Dao. Get (EMP. getemployeeid ());
EMP. setfirstname (emp2.getfirstname ());
EMP. setlastname (emp2.getlastname ());

Return "edit-sucess ";
}

Private employee EMP = NULL;
 
Public String add (){
/// 1. Obtain the value of the firstname and lastname Request Parameters
/// 2. encapsulate firstname and lastname as an employee object
//
// EMP. setfirstname (firstname );
// EMP. setlastname (lastname );
/// 3. Call the Save method of DAO to perform the save operation.
Dao. Save (EMP );

Return success;
}
Public String Delete (){
// 1. Get the employee ID
// 2. Call the delete method of Dao
// Dao. Delete (employeeid );
Dao. Delete (EMP. getemployeeid ());
Return success;
}
 
Public String list (){
// Obtain information about all the employees
// Put the employee information in the request
Requestmap. Put ("emplist", Dao. findall ());
Return "list-sucess ";
}
 
Private Map <string, Object> requestmap = NULL;
 
Public void setrequest (Map <string, Object> arg0 ){
Requestmap = arg0;
}

}

 

3. preparable interceptor and paramsprepareparamsstack interceptor Stack

Package com. LLP. Crud;

Import com. opensymphony. xwork2.actionsupport;
Import com. opensymphony. xwork2.modeldriven;
Import com. opensymphony. xwork2.preparable;

Import java. util .*;
Import org. Apache. struts2.interceptor. requestaware;

Public class employeeaction extends actionsupport
Implements requestaware, modeldriven <employee>, preparable {
 
Private Dao = New Dao ();
 
Public void prepare () throws exception {
// Add a new employee or modify an employee
// EMP = new employee ();
// When querying an object from a database for displaying a form
// EMP = Dao. Get (EMP. getemployeeid ());
System. Out. println ("prepare ");

}
Public Employee GetModel (){
Return EMP;
}
 
Public void prepareupdate (){
EMP = new employee ();
}
Public String Update (){
// 1. Obtain the form information
// 2. encapsulate form information as an employee object
// Employee EMP = Dao. Get (employeeid );
// EMP. setemployeeid (employeeid );
// EMP. setfirstname (firstname );
// EMP. setlastname (lastname );

// 3. Call the update method of Dao
Dao. Update (EMP );
Return success;
}
 
Public void prepareedit (){
EMP = Dao. Get (employeeid );
}
Public string edit (){
/// 1. Obtain the object corresponding to the employee ID from the database
// Employee EMP = Dao. Get (employeeid );
/// 2. Assign the attribute of the object obtained in 1 to the corresponding attribute of the current action.
// Firstname = EMP. getfirstname ();
// Lastname = EMP. getlastname ();
Employee emp2 = Dao. Get (EMP. getemployeeid ());
EMP. setfirstname (emp2.getfirstname ());
EMP. setlastname (emp2.getlastname ());

Return "edit-sucess ";
}

Private employee EMP = NULL;
 
Public void prepareadd (){
EMP = new employee ();
}
Public String add (){
/// 1. Obtain the value of the firstname and lastname Request Parameters
/// 2. encapsulate firstname and lastname as an employee object
//
// EMP. setfirstname (firstname );
// EMP. setlastname (lastname );
/// 3. Call the Save method of DAO to perform the save operation.
Dao. Save (EMP );

Return success;
}
 
Private string employeeid;
 
Public void setemployeeid (string employeeid ){
This. employeeid = employeeid;
}
 
Public String Delete (){
// 1. Get the employee ID
// 2. Call the delete method of Dao
// Dao. Delete (employeeid );
Dao. Delete (employeeid );
Return success;
}
 
Public String list (){
// Obtain information about all the employees
// Put the employee information in the request
Requestmap. Put ("emplist", Dao. findall ());
Return "list-sucess ";
}
 
Private Map <string, Object> requestmap = NULL;
 
Public void setrequest (Map <string, Object> arg0 ){
Requestmap = arg0;
}
}

 

 

 

Struts. xml configuration:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype struts public
"-// Apache Software Foundation // DTD struts configuration 2.3 // en"
Http://struts.apache.org/dtds/struts-2.3.dtd>
<Struts>
<Constant name = "struts. UI. Theme" value = "simple"> </constant>
<Package name = "test_result" extends = "struts-Default">

<! -- Configure paramsprepareparamsstack as the default interceptor -->
<Default-interceptor-ref name = "paramsprepareparamsstack"> </default-interceptor-ref>

<Action name = "Employee _ *" class = "com. LLP. Crud. employeeaction" method = "{1}">
<! -- The value of the alwaysinvokeprepare parameter that overwrites the prepare interceptor is false -->
<Interceptor-ref name = "paramsprepareparamsstack">
<Param name = "prepare. alwaysinvokeprepare"> false </param>
</Interceptor-ref>

<Result name = "list-sucess">/WEB-INF/JSP/list. jsp </result>
<Result name = "edit-sucess">/WEB-INF/JSP/edit. jsp </result>
<! -- <Result name = "Update-success">/WEB-INF/JSP/list. jsp </result> -->
<Result name = "success" type = "redirectaction">
<Param name = "actionname"> employee_list </param>
</Result>
</Action>
</Package>
</Struts>

 

 

JSP page:

Index. jsp:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> my JSP 'index. jsp 'starting page </title>
</Head>
<Body>
<A href = "employee_list"> employee list </a>
</Body>
</Html>

 

Display list. jsp:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "UTF-8" %>

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> my JSP 'list. jsp 'starting page </title>
</Head>
<Body>
<H4> Add new employee </H4>
<S: Form Action = "employee_add">
<S: textfield name = "firstname" label = "firstname"> </S: textfield>
<S: textfield name = "lastname" label = "lastname"> </S: textfield>
<S: Submit> </S: Submit>
</S: Form>
<S: If test = "# request. Employees! = NULL ">
<Br/>
<HR/>
<Br/>
<S: If test = "# request. Employees. size! = 0 ">
Display Information
<Table>
<Tr>
<TH> employeeid </Th>
<TH> firstname </Th>
<TH> lastname </Th>
<TH> edit </Th>
<TH> Delete </Th>
</Tr>
<S: iterator value = "# request. Employees">
<Tr>
<TD >$ {employeeid} </TD>
<TD >$ {firstname} </TD>
<TD >$ {lastname} </TD>
<TD> <a href = "employee_edit? Employeeid =$ {employeeid} "> edit </a> </TD>
<TD> <a href = "employee_delete? Employeeid =$ {employeeid} "> Delete </TD>
</Tr>
</S: iterator>
</Table>
</S: If>
<S: else>
No employee information
</S: else>
</S: If>
</Body>
</Html>

 

Edit. jsp:

<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "ISO-8859-1" %>
<% @ Taglib prefix = "S" uri = "/Struts-tags" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">
<HTML>
<Head>
<Title> my JSP 'edit. jsp 'starting page </title>
</Head>
<Body>
<H4> edit employee </H4>
<S: Form Action = "employee_update">
<S: hidden name = "employeeid"> </S: hidden>
<S: textfield name = "firstname" label = "firstname"> </S: textfield>
<S: textfield name = "lastname" label = "lastname"> </S: textfield>
<S: Submit> </S: Submit>
</S: Form>
</Body>
</Html>

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.