标签:ssh
在action中继承了ActionSupport和其它一些公共属性,如selectedRow等;可能以后还会产生更多公共的内容,所以应该把这些共有的抽取出来,放入到一个基本action中,我们命名为BaseAction,让它去继承ActionSupport和其它公共属性,其它的action只要继承它就可以了。
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;
}
}注意:String[] selectedRow使用protected修饰符,因为它要在子类中能够访问到。
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、业务数据 *****/
private List<User> userList; //用于显示用户列表
private User user; //用于添加、修改和删除单个用户
//用户头像
private File headImg;
private String headImgContentType;
private String headImgFileName;
//导入Excel
private File userExcel;
private String userExcelContentType;
private String userExcelFileName;
/***** 2、业务实现类 *****/
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
/***** 3、页面响应操作 *****/
//列表页面
public String listUI()
{
userList = userService.findAll();
return "listUI";
}
//跳转到新增页面
public String addUI()
{
return "addUI";
}
//保存新增
public String add()
{
try {
if(user != null)
{
if(headImg != null)
{
//1、保存头像到upload/user
String filePath = ServletActionContext.getServletContext().getRealPath("upload/user/");
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf("."));
FileUtils.copyFile(headImg, new File(filePath,fileName));
//2、设置用户头像路径
user.setHeadImg("user/" + fileName);
}
userService.save(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//跳转到编辑页面
public String editUI()
{
if(user != null && user.getId() != null)
{
user = userService.findById(user.getId());
}
return "editUI";
}
//保存编辑
public String edit()
{
try {
if(user != null)
{
if(headImg != null)
{
//1、保存头像到upload/user
String filePath = ServletActionContext.getServletContext().getRealPath("upload/user/");
String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf("."));
FileUtils.copyFile(headImg, new File(filePath,fileName));
//2、设置用户头像路径
user.setHeadImg("user/" + fileName);
}
userService.update(user);
}
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
//删除
public String delete()
{
if(user != null && user.getId() != null)
{
userService.delete(user.getId());
}
return "list";
}
//批量删除
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;
}
}
////导出用户列表
public void exportExcel()
{
try {
//1、查找用户列表
userList = userService.findAll();
//2、导出
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("用户列表.xls".getBytes(),"ISO-8859-1"));
// String fileName = URLEncoder.encode("用户列表.xls", "UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
// String fileName = URLEncoder.encode("用户列表.xls", "UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename*=\"UTF8‘‘" + fileName + "\"");
// System.out.println("attachment;filename*=\"UTF8‘‘" + fileName + "\"");
String fileName = encodeFilename("用户列表.xls");
response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
// String fileName = URLEncoder.encode("用户列表.xls", "UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename="+new String("中国.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();
}
}
//导入用户列表
public String importExcel(){
//1、获取文件,并判断是否为excel文件
if(userExcel != null && userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){
//2、导入
userService.importExcel(userExcel,userExcelFileName);
}
return "list";
}
public void verifyAccount(){
try {
//1、获取账号
if(user != null && StringUtils.isNotBlank(user.getAccount())){
//2、根据帐号到数据库校验是否存在该帐号对应的用户
List<User> list = userService.findUserByAccountAndId(user.getId(),user.getAccount());
String strResult = "true";
if(list != null && list.size()>0){
//说明该帐号已经存在
strResult = "false";
}
//输出
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("text/plain");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(strResult.getBytes());
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// {{ Properties
public 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
原文地址:http://lsieun.blog.51cto.com/9210464/1837067