Tags: Tax Service System project


preface

To get a better handle on SSH, practice using a tax service….. Setting up the SSH framework environment was explained in detail in the previous article. Blog.csdn.net/hon_3y/arti…

User module requirements

Add, delete, change, check, import and export to EXCEL:

Add user: With this interface, we know what the attributes of the entity table are.


Adding a Configuration File

Each module should have its own configuration file, so that we can manage functions between modules easily without having to write all the configuration in the general file.

Therefore, we created a user package in the User module, followed by a Config package to manage configuration files

Javabeans and mapping files

JavaBean

Add user attributes according to the above requirements, just write.


public class User implements Serializable {

    private String id;
    private String dept;
    private String account;
    private String name;
    private String password;

    private String headImg;
    private boolean gender;
    private String state;
    private String mobile;
    private String email;
    private Date birthday;
    private String memo;

    public static String USER_STATE_VALID = "1";/ / is valid,
    public static String USER_STATE_INVALID = "0";/ / is invalid

    public User(a) {}public User(String id, String dept, String account, String name, String password, String headImg, boolean gender, String state, String mobile, String email, Date birthday, String memo) {
        this.id = id;
        this.dept = dept;
        this.account = account;
        this.name = name;
        this.password = password;
        this.headImg = headImg;
        this.gender = gender;
        this.state = state;
        this.mobile = mobile;
        this.email = email;
        this.birthday = birthday;
        this.memo = memo;
    }

	// Various setters and getters
}
Copy the code

User.hbm.xml

Mapping files are also very simple, because there are no related fields and you just write properties.


<?xml version="1.0" encoding="utf-8"? >

      

<hibernate-mapping>
	<class name="zhongfucheng.user.entity.User" table="user">
		<id name="id" type="java.lang.String">
			<column name="id" length="32" />
			<generator class="uuid.hex" />
		</id>
		<property name="name" type="java.lang.String">
			<column name="name" length="20" not-null="true" />
		</property>
		<property name="dept" type="java.lang.String">
			<column name="dept" length="20" not-null="true" />
		</property>		
		<property name="account" type="java.lang.String">
			<column name="account" length="50" not-null="true" />
		</property>
		<property name="password" type="java.lang.String">
			<column name="password" length="50" not-null="true" />
		</property>
		<property name="headImg" type="java.lang.String">
			<column name="headImg" length="100" />
		</property>
		<property name="gender" type="java.lang.Boolean">
			<column name="gender" />
		</property>
		<property name="email" type="java.lang.String">
			<column name="email" length="50" />
		</property>
		<property name="mobile" type="java.lang.String">
			<column name="mobile" length="20" />
		</property>
		<property name="birthday" type="java.util.Date">
			<column name="birthday" length="10" />
		</property>
		<property name="state" type="java.lang.String">
			<column name="state" length="1" />
		</property>
		<property name="memo" type="java.lang.String">
			<column name="memo" length="200" />
		</property>
	</class>

</hibernate-mapping>
	
Copy the code

After writing the mapping file, remember to read the mapping file in Spring’s general configuration file… Note that the user module uses a user package to manage the following code, which is easy to manage!


Write the Dao

UserDao interface


/** * UserDao interface, inheriting UserDao ** /
public interface UserDao extends BaseDao<User> {}Copy the code

UserDaoImpl implementation class

Add the UserDaoImple object to the IOC container for management

Note: Since we use the HibernateDaoSupport API in BaseDao, we need to inject the SessionFactory in the XML configuration in the UserDao.

In the general configuration file there is a bean configuration specifically for injecting SessionFactory


    <! -- Parent of all business DAOs
    <bean id="baseDao" abstract="true">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
Copy the code


/** * inherit the BaseDaoImpl implementation class, and have the CRUD method * and implement the UserDao interface, so the UserDao interface can have a corresponding complement to the User module ** */

public class UserDaoImpl extends BaseDaoImpl<User> implements UserDao {}Copy the code
    <bean id="userDaoImpl" class="zhongfucheng.user.dao.impl.UserDaoImpl" parent="baseDao"></bean>
    <context:component-scan base-package="zhongfucheng.user"/>

Copy the code
  • Add the Spring configuration file for the User module to the total configuration file

    <! -- This is the configuration file of the user module -->
    <import resource="classpath:zhongfucheng/user/config/user-bean.xml"/>
Copy the code

Write the Service

The Service interface

/** * Created by ozc on 2017/5/23. */
    public interface UserService {
    
        / / new
        public void save(User user);
        / / update
        public void update(User user);
        // Delete O based on id
        public void delete(Serializable id);
        // Find by id
        public User findObjectById(Serializable id);
        // Find the list
        public List<User> findObjects(a);
    
    }


Copy the code

UserServiceImpl


package zhongfucheng.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import zhongfucheng.user.dao.UserDao;
import zhongfucheng.user.entity.User;
import zhongfucheng.user.service.UserService;

import java.io.Serializable;
import java.util.List;

/** * Created by ozc on 2017/5/23. */

@Service
public class UserServiceImpl implements UserService {


    @Qualifier("userDaoImpl")
    @Autowired
    private UserDao userDaoImpl;


    @Override
    public void save(User user) {
        userDaoImpl.save(user);
    }

    @Override
    public void update(User user) {
        userDaoImpl.update(user);
    }

    @Override
    public void delete(Serializable id) {

        userDaoImpl.delete(id);
    }

    @Override
    public User findObjectById(Serializable id) {
        return userDaoImpl.findObjectById(id);
    }

    @Override
    public List<User> findObjects(a) {
        returnuserDaoImpl.findObjects(); }}Copy the code

UserAction

UserAction should have several methods for adding, deleting, and checking:


/** * 1. Provide a new page * 2. Determine the method of adding a user * 3. Provide modify page * 4. Determine how to modify a user * 5. Delete a user * 6. Delete users in batches * 7. Provide a list display page * * */
Copy the code
package zhongfucheng.user.action;

import com.opensymphony.xwork2.ActionSupport;

/** * Created by ozc on 2017/5/23. */

/** * 1. Provide a new page * 2. Determine the method of adding a user * 3. Provide modify page * 4. Determine how to modify a user * 5. Delete a user * 6. Delete users in batches * 7. Provide a list page * * * */
public class UserAction extends ActionSupport {

    public String listUI(a) {

        return null;
    }

    public String addUI(a) {

        return null;
    }
    public String editUI(a) {

        return null;
    }
    public String edit(a) {

        return null;
    }
    public String delete(a) {

        return null;
    }
    public String add(a) {

        return null;
    }

    public String deleteSelect(a) {
        
        return null; }}Copy the code

In the Struts configuration file



<?xml version="1.0" encoding="UTF-8"? >


      

<struts>
    <package name="user-action" extends="struts-default" namespace="/user">
        <action name="user_*" class="zhongfucheng.user.action.UserAction" method="{1}">

        </action>

    </package>

</struts>
Copy the code
  • Add the Struts configuration file for the user module to the overall configuration file

    <! - the User module -- -- >
    <include file="zhongfucheng/user/config/user-struts.xml"/>

Copy the code

Complete the art design page display

Make adjustments to the page given by the artist

Import into the project:

We found that the following code is common in JSP pages, so we wrapped it:

Create a public file that encapsulates frequently used JSP pages:

Where you want to use it, just import it directly:


<head>
    <%@include file="/common/header.jsp"% >
    <title>User management</title>
    
</head>
Copy the code

The next step is to fill in the logic code for each function.

Adding a User UI


    public String addUI(a) {
        return "addUI";
    }
Copy the code

This is what it looks like: let’s leave the avatar and character alone and do the rest first.

Confirm adding a User

Write the path we requested:

    / * * * * * * * * * * * * * injection Service * * * * * * * * * * * * * * * * * * * * * * * * /
    @Qualifier("userServiceImpl")
    @Autowired
    private UserService userServiceImpl;


    / * * * * * * * * * * * * data automatic encapsulation, setter and getter is * * * * * * * * * * * * * * * * * * * * * * * * * /
    private User user;
    public User getUser(a) {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    / * * * * * * * * * * * * to get the Service data returned by the * * * * * * * * * * * * * * * * * * * * * * * * * /
    private List<User> userList;

    public String add(a) {
    userServiceImpl.save(user);

    // Jump to the list display page
    return "list";
    }

Copy the code
  • Configure the result to jump to the list page.


            <! Return to the list display page, redirect to list display
            <result name="list" type="redirectAction">
                <param name="actionName">user_listUI</param>
            </result>
Copy the code
  • Effects: The list below is not done yet, but the effect of adding users is done.


The list shows

    / * * * * * * * * * * * * to get the Service data returned by the * * * * * * * * * * * * * * * * * * * * * * * * * /
    // Be sure to give the setter and getter methods so that the JSP can get the properties. Otherwise you won't get it!! I've been here a long time !!!!
    private List<User> userList;
    public List<User> getUserList(a) {
        return userList;
    }
    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public String listUI(a) {
        userList = userServiceImpl.findObjects();

        return "listUI";

    }

Copy the code

The JSP gets the data from iterator and just writes it. Because the Action property has a getter method:


        <s:iterator value="userList">
        <tr bgcolor="f8f8f8">
            <td align="center"><input type="checkbox" name="selectedRow" value="<s:property value="id"/ >"/ ></td>
            <td align="center"><s:property value="name"/></td>
            <td align="center"><s:property value="account"/></td>
            <td align="center"><s:property value="dept"/></td>
            <td align="center"><s:property value="gender? Male: Female"/></td>
            <td align="center"><s:property value="email"/></td>
            <td align="center">
                <a href="javascript:doEdit(<s:property value="id"/ >) "> edit</a>
                <a href="javascript:doDelete(<s:property value="id"/ >) "> delete</a>
            </td>
        </tr>
        </s:iterator>
Copy the code


Modify the function



    public String editUI(a) {

        // We need to find the User that corresponds to the id
        if(user.getId() ! =null&& user ! =null) {

            // The User has a getter to read the corresponding information!
            user = userServiceImpl.findObjectById(this.user.getId());
    }
        return "editUI";
    }

Copy the code
  • JSP modify user interface is not to display ID, but we modify the user, according to the ID to modify. So we add a hidden domain

   <s:hidden name="user.id"/>

Copy the code
  • Specify a path for processing:

action="${basePath}user/user_edit.action"
Copy the code
  • Modification method:
    public String edit(a) {
        //Struts2 automatically encapsulates the data brought by the JSP into the User object
        if(user.getId() ! =null&& user ! =null) {

            userServiceImpl.update(user);
        }
		
		// Jump back to the list display
        return "list";
    }


Copy the code
  • Effect:


Delete function

The delete method is very simple:


        function doDelete(id) {
            document.forms[0].action = "${basePath}user/user_delete.action? user.id="+id;
            document.forms[0].submit();
        }
Copy the code

    public String delete(a) {

        if(user.getId() ! =null&& user ! =null) {
            userServiceImpl.delete(user.getId());
        }

        return "list";
    }
Copy the code


Batch delete

Response to click events:

        function doDeleteAll() {
            document.forms[0].action = "${basePath}user/user_deleteSelect.action";
            document.forms[0].submit();

        }

Copy the code
  • Action uses a String[] to receive data from checkboxes

    private String[] selectedRow;
    public String[] getSelectedRow() {
        return selectedRow;
    }
    public void setSelectedRow(String[] selectedRow) {
        this.selectedRow = selectedRow;
    }

    public String deleteSelect() {
        for (String s : selectedRow) {
            userServiceImpl.delete(s);
        }
        return "list";
    }
Copy the code
  • Effect:


conclusion

This article is almost all the knowledge points we often use, just CRUD.

  • If there are many places where some code can be used in JSP, then we can extract it.

If you find this article helpful, give the author a little encouragement