Struts2 easyui implements the crud and struts2crud of the datagrid

Source: Internet
Author: User

Struts2 easyui implements the crud and struts2crud of the datagrid
In the last two days, easyui was used to implement crud using its datagrid because of project requirements. For the first time, it took one day to complete all the functions. Yesterday I made another module. The same function took only two hours. Now we can record the problems we encountered during the first datagrid operation to help you remember them. It also helps other friends who first came into contact with easyui.

Problem

How many types of json are there?

At that time, I copied the json jar package from another project. The following statement returns an error.
resultObj = JSONObject.fromObject(json);
That is, the JSONObject method in my json package does not contain the forObject method.
What should we do? Change the jar package.
I switched
Json-lib-2.3-jdk15.jar
Then the strange problem arises. The program does not move at that step. It just stops running and stops there.
I am still talking about the jar problem on the Internet. At last, I added the following packages to solve the problem.

Why are there two common-lang statements?
Because the problem above requires common-lang2, and struts2.3.16 needs common-lang3.


Code
Okay. Now let's look at the code.
This is the front-end jsp code
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> Introduced in jsp
userAdmin.js

Let's take a look.

$ (Function () {$ ('# tt '). datagrid ({title: 'user management', iconCls: 'icon-edit', // icon width: 530, height: 300, nowrap: false, striped: true, border: true, toolbar: '# toolbar', // you try not to see what effect this line has. // fit: true, // automatic size url: 'userfindall', // sortName: 'code', // sortOrder: 'desc', remoteSort: false, idField: 'id', singleSelect: true, // select rownumbers: true // row number}) ;}); var url; function newUser () {$ ('# dlg '). dialog ('open '). d Ialog ('settitle', 'new user'); $ ('# fm '). form ('clear'); url = 'adduser'; // $ ('# dlg '). dialog ('close')} function editUser () {var row = $ ('# tt '). datagrid ('getselected'); if (row) {$ ('# dlg '). dialog ('open '). dialog ('settitle', 'edit user'); $ ('# fm '). form ('load', row); url = 'edituser';} function saveUser () {$ ('# fs '). form ('submit ', {url: url, onSubmit: function () {return $ (this ). form ('validate '); }, Success: function (result) {var result = eval ('+ result +'); if (result. errorMsg) {// check whether the json contains errorMsg $. messager. show ({title: 'error', msg: result. errorMsg});} else {$ ('# dlg '). dialog ('close'); // close the dialog $ ('# tt '). datagrid ('reload'); // reload the user data }});} function destroyUser () {var row = $ ('# tt '). datagrid ('getselected'); // get the row I selected if (row) {$. messager. Confirm ('Confirm', 'Are you sure you want to destroy this user? ', Function (r) {if (r) {$. post ('deleteuser', {id: row. userId // this userid is the field = "userId"}, function (result) {if (result. success) {// check whether the success domain $ ('# tt') in json is found during deletion '). datagrid ('reload'); // reload the user data} else {$. messager. show ({// show error message title: 'error', msg: result. errorMsg}) ;}}, 'json ');}});}}

Let's take a look at the configuration in struts. xml.

        <package name="user" namespace="/module/user" extends="json-default" >                        <action name="userFindAll" class="userAction" method="findAll">            <result type="json">                <param name="root">resultObj</param>              </result>        </action>        </package>
There are two key points: resultObj and json-default.
Let's take a look at userAction.
Package com. module. user; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import javax. annotation. resource; import net. sf. json. JSONObject; import org. springframework. context. annotation. scope; import org. springframework. stereotype. component; import com. core. baseAction; import com. core. dao. utilDAO; import com. core. model. user; @ Component @ Scope ("prototype") public c Lass UserAction extends BaseAction {/*****/private static final long serialVersionUID =-4070056523032278260L; private JSONObject resultObj; private List <User> userList; private User user; private int userId; private String role; private String status; private String passWord; private String userName; private int id; @ Resource private UtilDAO utilDAO; public String update () {user = getMyUser (); u TilDAO. update (user); Map <String, Object> json = new HashMap <String, Object> (); // json. put ("error MSG", "error 444"); resultObj = JSONObject. fromObject (json); // formatted result must be JSONObject return SUCCESS;} public User getMyUser () {User user User = new user (); User. setPassWord (passWord); user. setRole (role); user. setStatus (status); user. setUserId (userId); user. setUserName (userName); return user;} @ SuppressWarning S ("unchecked") public String findAll () {userList = (List <User>) utilDAO. findAllList ("User"); ArrayList <Map <String, Object> al = new ArrayList <Map <String, Object> (); for (User u: userList) {Map <String, Object> m = new HashMap <String, Object> (); m. put ("userId", u. getUserId (); m. put ("userName", u. getUserName (); m. put ("passWord", u. getPassWord (); m. put ("status", u. getStatus (); m. put ("role", u. get Role (); al. add (m) ;}map <String, Object> json = new HashMap <String, Object> (); json. put ("total", 88); // total number of records stored by the total key, required json. put ("page", 4); // the current fourth page of json. put ("rows", al); // The rows key stores the list resultObj = JSONObject. fromObject (json); // formatted result must be JSONObject return SUCCESS;} public String add () {user = getMyUser (); user. setStatus ("good"); utilDAO. save (user); Map <String, Object> json = new HashMap <String, Object> (); resultObj = JSONObject. fromObject (json); // formatted result must be JSONObject return SUCCESS;} @ SuppressWarnings ("unchecked") public String delete () {userList = (List <User>) utilDAO. findListByProperty ("User", "userId", id, ""); user = userList. get (0); utilDAO. delete (user); Map <String, Object> json = new HashMap <String, Object> (); json. put ("success", "success !!! "); ResultObj = JSONObject. fromObject (json); // The result must be JSONObject return SUCCESS ;}}
You will surely think about what getMyUser does?
And
    private int userId;    private String role;    private String status;    private String passWord;    private String userName;
This is the parameter in the User class. Why do I need to write it again?

Ah... learning struts, we all know that when passing parameters to the id in the user, it is OK to write the input name in the front-end form as user. id.

But the problem is here.
Let's take a look at the destroyUser method in js.
There is a sentence in it
Id: row. userId
Row. userId is The userId field of the selected row.
If you delete an object, you must know its identifier.
Function editUser () {var row = $ ('. Tt '). datagrid ('getselected'); if (row) {$ ('. Dlg'). dialog ('open'). dititle ('settitle', 'edit user'); $ ('. Fm '). form ('load', row); // pass the row data to the edit window url = 'edituser ';}}
If the input name in the edit window is user. id. According to the unified transmission feature of edit, the table field must also be in the form of user. id.
However, when you want to destory, You need to obtain the id of the selected row.
How to write?
Row. user. userId?

Therefore, I can only use the method of passing a single value. This is why there are so many parameters in my action.

Finally, let's take a look at the overall




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.