in theActioninherited from theActionsupportand some other public properties, such asSelectedrowAnd so on, it is possible that more public content will be generated in the future, so it should be extracted into a basicAction, we are namedbaseaction, let it inheritActionsupportand other public properties, the otherActionas long as you inherit it.
Baseaction.java
Package Com.rk.core.action;import Com.opensymphony.xwork2.actionsupport;public abstract class Baseaction extends Actionsupport {protected string[] selectedrow;public string[] Getselectedrow () {return selectedrow;} public void Setselectedrow (string[] selectedrow) {this.selectedrow = Selectedrow;}}
Note:string[] Selectedrow uses the protected modifier because it is accessible in subclasses .
Useraction.java
package com.rk.tax.action;import java.io.file;import java.io.ioexception;import Java.io.unsupportedencodingexception;import java.net.uri;import java.net.urisyntaxexception;import java.net.URLEncoder;import java.util.List;import java.util.UUID;import javax.servlet.servletoutputstream;import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import org.apache.commons.io.fileutils;import org.apache.commons.io.ioutils;import org.apache.commons.lang3.stringutils;import org.apache.struts2.servletactioncontext;import org.slf4j.logger;import org.slf4j.loggerfactory; import com.opensymphony.xwork2.actionsupport;import com.rk.core.action.baseaction;import com.rk.tax.entity.user;import com.rk.tax.service.userservice;public class useraction extends baseaction {/***** 1, Business Data *****/private List<User> userList;Used to display the list of users private user user;//for adding, modifying, and deleting individual users//user avatars private file headimg;private string headimgcontenttype;private string headimgfilename;//Import excelprivate file userexcel; PRIVATE&NBSP;STRING&NBSP;USEREXCELCONTENTTYPE;PRIVATE&NBSP;STRING&NBSP;USEREXCELFILENAME;/*****&NBSP;2, Business implementation class *****/private userservice userservice;public void setuserservice (UserService UserService) {this.userservice = userservice;} /*****&NBSP;3, page Response Action *****///List page Public string listui () {userlist = Userservice.findall ();return "Listui";} Jump to New Page Public string addui () {return "Addui";} Save New Public string add () {try {if (user != null) {if (headimg != null) {///1, Save Avatar to Upload/userstring filepath = servletactioncontext.getservletcontext (). GetRealPath ("upload/ user/"); String filename = uuid.randomuuid (). toString (). ReplaceAll ("-", "") + headimgfilename.substring (Headimgfilename.lastindexof (".")); Fileutils.copyfile (Headimg, new file (Filepath,filename));//2, set user Avatar path User.setheadimg ("user/" + filename); }userservice.save (user);}} catch (exception e) {e.printstacktrace ();} return "List";} Jump to edit page Public string editui () {if (User != null && user.getid () != null) {User = userservice.findbyid (User.getid ());} return "Editui";} Save Edit Public string edit () {try {if (user != null) {if (headimg != null) {///1, Save Avatar to Upload/userstring filepath = servletactioncontext.getservletcontext (). GetRealPath ("upload/ user/"); String filename = uuid.randomuuid (). toString (). ReplaceAll ("-", "") + Headimgfilename.substring (Headimgfilename.lastindexof (".")); Fileutils.copyfile (Headimg, new file (Filepath,filename));//2, set user Avatar path User.setheadimg ("user/" + filename); }userservice.update (user);}} catch (exception e) {e.printstacktrace ();} return "List";} Delete Public string delete () {if (User != null && user.getid () != NULL) {Userservice.delete (User.getid ());} return "List";} Bulk Delete public string deleteselected () {if (selectedrow != null) {for (string id : selectedrow) {userservice.delete (id);}} return "List";} Private string encodefilename (Final string filename) { try{ uri uri = new uri (Null, null, filename, null); string encodedname = uri.toasciistring (); return encodedname; } catch (Urisyntaxexception ex) { return filename; }}////Export user List public void exportexcel () {try {//1, find user list userlist = Userservice.findall ();//2, Export httpservletrequest request = servletactioncontext.getrequest (); String useragent = request.getheader ("User-agent"); System.out.println (useragent); Httpservletresponse response = servletactioncontext.getresponse (); Response.setContentType (" Application/octet-stream ");//response.setheader (" Content-disposition ", " Attachment;filename= " + new string ("user list. xls". GetBytes (), "iso-8859-1"));//string filename = Urlencoder.encode ("user list. xls", "UTF-8");//response.setheader ("Content-disposition", "attachment; Filename= "+ filename);//string filename = urlencoder.encode (" User list. xls ", " UTF-8 ");// Response.setheader ("Content-disposition", "attachment;filename*=\" UTF8 "" + filename + "\" ");//system.out.println (" Attachment;filename*=\ "UTF8 " + fileName + " \ ""); String filename = encodefilename ("user list. xls"); Response.setheader ("Content-disposition", " Attachment;filename= "+ filename);//string filename = urlencoder.encode (" User list. xls ", " UTF-8 "),//response.setheader (" Content-disposition ", " Attachment;filename= "+new string (" China. xls "). GetBytes (), "iso-8859-1") + "; Filename*=utf8" " + filename); Servletoutputstream outputstream = response.getoutputstream (); Userservice.exportexcel (UserList, OutputStream); if (outputstream != null) {outputstream.close ();}} catch (exception e) {e.printstacktrace ();}} Import user list Public string importexcel () {///1, get the file and determine if it is an Excel file if (userexcel != null & & userexcelfilename.matches ("^.+\\. i) ((xls) | ( xlsx)) {//2, Import userservice.importexcel (userexcel,userexcelfilename);} return "List";} Public void verifyaccount () {TRY&NBSP;{//1,Get an account if (User != null && stringutils.isnotblank (User.getaccount ())) {//2, According to the account to the database check whether there is a user List<user> list = userservice.finduserbyaccountandid (User.getid (), User.getaccount ()); string strresult = "true"; if (List != null && list.size () >0) {// Indicates that the account already exists strresult = "false";} Output Httpservletresponse response = servletactioncontext.getresponse (); Response.setContentType (" Text/plain "); Servletoutputstream outputstream = response.getoutputstream (); Outputstream.write ( Strresult.getbytes ()); Outputstream.close ();}} catch (ioexception e) {e.printstacktrace ();}} {{ propertiespublic list<user> getuserlist () {return userlist;} Public void setuserlist (List<user> userlist) {this.userlist = userlist;} Public user getuser () {return user;} Public void setuser (User user) {this.user = user;} Public file getheadimg () {return headimg;} Public void setheadimg (file headimg) {this.headimg = headimg;} Public string getheadimgcontenttype () {return headimgcontenttype;} Public void setheadimgcontenttype (String headimgcontenttype) {this.headimgcontenttype = headimgcontenttype;} Public string getheadimgfilename () {return headimgfilename;} Public void setheadimgfilename (String headimgfilename) {this.headImgFileName = Headimgfilename;} Public file getuserexcel () {return userexcel;} Public void setuserexcel (File userexcel) {this.userexcel = userexcel;} Public string getuserexcelcontenttype () {return userexcelcontenttype;} Public void setuserexcelcontenttype (String userexcelcontenttype) {this.userExcelContentTypE = userexcelcontenttype;} Public string getuserexcelfilename () {return userexcelfilename;} Public void setuserexcelfilename (String userexcelfilename) {this.userexcelfilename = userexcelfilename;} }}}
SSH series: (13) Extract Baseaction