I. Regional data manipulation
Bulk Import Feature
1. Front page logic
First Ajax cannot do file uploads. A jquery plugin is required.
The first step: introduce the plugin's JS file in the JSP page
<script type= "Text/javascript" src= "${pagecontext.request.contextpath}/js/jquery.ocupload-1.1.2.js" ></ Script>
Step two: Provide an element in the page for marking.
{
ID: ' Button-import ',
text: ' Import ',
iconcls: ' Icon-redo '
}
Step three: Invoke the Upload method provided by the plugin to dynamically modify the page HTML code
Call the Ocupload plug-in method
$ ("#button-import"). Upload ({
action: ' ${pagecontext.request.contextpath}/regionaction _importxls.action ',
name: ' MyFile ',
oncomplete:function (data) {
if (data = = ' 1 ') {
//upload successful
$. Messager.alert ("Prompt Information", "zone data import succeeded.") "," info ");
} else{
//Failure
$.messager.alert ("Prompt message", "Zone data import failed.") "," Warning ");}}
);
2. Back-Office system logic
Pojo base class: Region
Package com.crm.bos.domain;
Import Java.util.HashSet;
Import Java.util.Set;
/** * Area Entity * */public class Region implements Java.io.Serializable {//field private String ID;
Private String Province;
Private String City;
Private String District;
Private String postcode;
Private String Shortcode;
Private String Citycode;
Public String GetName () {return province+city + district;
} private Set subareas = new HashSet (0); Constructors/** Default Constructor */Public region () {}/** minimal constructor * * Public Reg
ION (String ID) {this.id = ID; /** Full Constructor */public region (string ID, String province, String city, String district, Str
ING postcode, string shortcode, String citycode, Set subareas) {this.id = ID;
this.province = Province;
this.city = City;
this.district = District;
This.postcode = postcode; ThiS.shortcode = Shortcode;
This.citycode = Citycode;
This.subareas = subareas;
}//Property accessors Public String getId () {return this.id;
} public void SetId (String id) {this.id = ID;
} public String Getprovince () {return this.province;
} public void Setprovince (String province) {this.province = province;
} public String getcity () {return this.city;
} public void Setcity (String city) {this.city = city;
} public String Getdistrict () {return this.district;
} public void Setdistrict (String district) {this.district = district;
} public String Getpostcode () {return this.postcode;
} public void Setpostcode (String postcode) {This.postcode = postcode;
} public String Getshortcode () {return this.shortcode; } public void Setshortcode (String shortcode) {This.shoRtcode = Shortcode;
} public String Getcitycode () {return this.citycode;
} public void Setcitycode (String citycode) {this.citycode = Citycode;
} public Set Getsubareas () {return this.subareas;
} public void Setsubareas (Set subareas) {this.subareas = subareas; }
}
Corresponding Hibernate mapping file
<?xml version= "1.0" encoding= "Utf-8"?> <! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "Http://www.hibernate.org/dtd/hiber Nate-mapping-3.0.dtd ">
Action Processing class:
@Controller @Scope ("prototype") public class Regionaction extends Baseaction<region> {//Receive uploaded files private Fi
Le MyFile;
public void Setmyfile (File myFile) {this.myfile = MyFile; }/** * Bulk Import * * @throws IOException * @throws filenotfoundexception * * Public String I
Mportxls () throws Exception {String flag = "1"; Use POI to parse Excel file try {Hssfworkbook workbook = new Hssfworkbook (New Fileinputstr
EAM (MyFile));
Get the first sheet page hssfsheet sheet = workbook.getsheetat (0);
list<region> list = new arraylist<region> ();
for (Row row:sheet) {int rowNum = Row.getrownum ();
if (RowNum = = 0) {//First line, header row, ignore continue;
The String id = Row.getcell (0). Getstringcellvalue (); String province = Row.getcell (1). getstRingcellvalue ();
String City = Row.getcell (2). Getstringcellvalue ();
String district = Row.getcell (3). Getstringcellvalue ();
String postcode = string.valueof ((long) Row.getcell (4). Getnumericcellvalue ());
Region region = new Region (ID, province, city, district, postcode, NULL, NULL, NULL);
City = city.substring (0, City.length ()-1);
string[] Stringtopinyin = Pinyin4jutils.stringtopinyin (city);
String Citycode = Stringutils.join (Stringtopinyin, "");
Jane Code---->>hbsjzca province = province.substring (0, Province.length ()-1);
district = district.substring (0, District.length ()-1); String info = province + City + district;//Hebei Shijiazhuang Changan string[] headbystring = pinyin4jutils.getheadbystring (
info); String Shortcode = Stringutils.join (heaDbystring, "");
Region.setcitycode (Citycode);
Region.setshortcode (Shortcode);
List.add (region);
} regionservice.savebatch (list);
} catch (Exception e) {e.printstacktrace ();
Flag = "0";
} servletactioncontext.getresponse (). setContentType ("Text/html;charset=utf-8");
Servletactioncontext.getresponse (). Getwriter (). print (flag);
return NONE; }
Use pinyin4j to generate a simple code and a city code here
public class Pinyin4jutils {/** * Converts a string into a phonetic array * * @param src * @return */public static
String[] Stringtopinyin (String src) {return Stringtopinyin (SRC, false, NULL); /** * Convert string to phonetic array * * @param src * @return */public static string[] Stringtopinyin (S
Tring SRC, String separator) {return Stringtopinyin (SRC, true, separator); /** * Converts a string into a phonetic array * * @param src * @param ispolyphone * Whether to find out all pinyin of Polyphone * @p Aram Separator * polyphone Pinyin delimiter * @return */public static string[] Stringtopinyin (String src, Boolean ispolyphone, String separator) {//Determines whether the string is empty if ("". Equals (src) | |
Null = = src) {return null;
} char[] Srcchar = Src.tochararray ();
int srccount = Srcchar.length;
string[] Srcstr = new String[srccount]; for (int i = 0; i < Srccount; i++) {Srcstr[i] = Chartopinyin (Srcchar[i], ispolyphone, separator);
} return SRCSTR; /** * Converts a single character to pinyin * * @param src * @return */public static String Chartopinyin (char s RC, Boolean ispolyphone, String separator) {//Create Pinyin processing class Hanyupinyinoutputformat Defaultforma
t = new Hanyupinyinoutputformat ();
Output settings, case, phonetic transcription mode defaultformat.setcasetype (hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
StringBuffer temppinying = new StringBuffer (); If it is Chinese if (src > string[) {try {//convert result (s) STRs = Pinyinhel
Per.tohanyupinyinstringarray (SRC, defaultformat); If Polyphone is detected, the default is to find out the first character of Polyphone if (ispolyphone && null! = separator) {for (int i = 0; i < strs.length;
i++) { Temppinying.append (Strs[i]); if (strs.length! = (i + 1)) {//Polyphone are separated by special symbols temppinying.append
(separator);
}}}} else {temppinying.append (strs[0]);
}} catch (Badhanyupinyinoutputformatcombination e) {e.printstacktrace ();
}} else {temppinying.append (SRC);
} return temppinying.tostring ();
} public static string Hanzitopinyin (String Hanzi) {return Hanzitopinyin (Hanzi, ""); }/** * Convert kanji to Pinyin * * @param hanzi * @param separator * @return */public static St Ring Hanzitopinyin (String Hanzi, string separator) {//Create Pinyin processing class Hanyupinyinoutputformat DefaultFormat =
New Hanyupinyinoutputformat (); Output settings, case, phonetic transcription mode Defaultformat.setcasetype (Hanyupinyincasetype.lowercase);
Defaultformat.settonetype (Hanyupinyintonetype.without_tone);
String pinyingstr = "";
try {pinyingstr = pinyinhelper.tohanyupinyinstring (Hanzi, DefaultFormat, separator); } catch (Badhanyupinyinoutputformatcombination e) {//TODO auto-generated catch block E.prin
Tstacktrace ();
} return PINYINGSTR; /** * Convert string array to String * * @param str * @param separator * delimiter between each string * @ret URN */public static string stringarraytostring (string[] str, string separator) {StringBuffer sb = new S
Tringbuffer ();
for (int i = 0; i < str.length; i++) {sb.append (str[i]);
if (str.length! = (i + 1)) {sb.append (separator);
}} return sb.tostring (); }/** * Simple linking of individual character arrays * * @param str * @return */public static String stringarraytostring (string[] str) {return stringarraytostring (str, ""); /** * Converts a character array to a string * * @param str * @param separator * delimiter between each string * @retu RN */public static string chararraytostring (char[] ch, string separator) {StringBuffer sb = new STRINGB
Uffer ();
for (int i = 0; i < ch.length; i++) {sb.append (ch[i]);
if (ch.length! = (i + 1)) {sb.append (separator);
}} return sb.tostring (); /** * Converts a character array to a string * * @param str * @return */public static string Chararraytostring (
char[] ch) {return chararraytostring (ch, "");
}/** * Take the first letter of the Chinese character * * @param src * @param iscapital * is uppercase * @return */ public static char[] Getheadbychar (char src, boolean iscapital) {//If the kanji is not directly returned if (SRC <= 1{return new char[] {src};
}//Get all pinyin string[] Pinyingstr = Pinyinhelper.tohanyupinyinstringarray (src);
Create return object int polyphonesize = Pinyingstr.length;
char[] Headchars = new Char[polyphonesize];
int i = 0;
Intercepts the first character for (String s:pinyingstr) {char Headchar = s.charat (0);
If the initials are uppercase, the default is lowercase if (iscapital) {Headchars[i] = character.touppercase (Headchar);
} else {headchars[i] = Headchar;
} i++;
} return headchars; }/** * Take the first letter of Chinese characters (default is uppercase) * * @param src * @return */public static char[] Getheadbychar (c
Har src) {return Getheadbychar (SRC, true); /** * Find string First letter * * @param src * @return */public static string[] Getheadbystring (Str
ing src) {return getheadbystring (SRC, true);
} /** * Find string First letter * * @param src * @param iscapital * UPPERCASE * @return */ public</