struts2采用annotation配置时
我们采用params来传递参数
params使用的时类似于键值对的方式传递
params = {"mgs", "${yudingurl}","a","a1" }
package com.yidian.web.frontuser; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.springframework.beans.factory.annotation.Autowired; import org.springside.modules.orm.Page; import org.springside.modules.orm.PropertyFilter; import org.springside.modules.utils.web.struts2.Struts2Utils; //import com.yidian.common.email.Mail; import com.yidian.common.email.Mail; import com.yidian.common.util.AjaxUtil; import com.yidian.common.util.Constants; import com.yidian.common.util.Constants_Yidian; import com.yidian.common.util.LoginStatus; import com.yidian.common.util.MathUtils; import com.yidian.common.util.Md5Util; import com.yidian.common.util.PageSize; import com.yidian.common.util.RandomUtil; import com.yidian.common.util.StringUtils; import com.yidian.dao.mybatis.information.misc.po.miscPo; import com.yidian.dao.mybatis.vipcustomers.vipcustomers.po.customerInfoPo; import com.yidian.entity.account.ForgPassInfo; import com.yidian.entity.car.ReservationInfo; import com.yidian.entity.car.CustomerInfo; //import com.yidian.entity.car.WebsiteBaseinfo; import com.yidian.entity.temp.IndexShow; import com.yidian.service.ServiceException; import com.yidian.service.helpsetting.HelpSettingManager; import com.yidian.service.information.misc.MiscManager; //import com.yidian.service.information.websitebase.WebsiteBaseinfoManager; //import com.yidian.service.order.longtermrental.ReserInfoManager; import com.yidian.service.systemuser.user.AccountManager; import com.yidian.service.vipcustomers.vipcustomers.CustomerInfoManager; import com.yidian.service.vipcustomers.vipcustomers.ForgPassManager; import com.yidian.web.CrudActionSupport; @Namespace("/frontuser") @Results( { @Result(name = CrudActionSupport.RELOAD, location = "frontuser.action", type = "redirect"), @Result(name = "register", location = "frontuser!input.action", type = "redirect"), @Result(name = "tologin", location = "/WEB-INF/content/frontuser/login.jsp"), @Result(name = "save", location = "/WEB-INF/content/frontuser/login.jsp"), @Result(name = "reserlist", location = "/WEB-INF/content/person/shortrent-order.jsp"), @Result(name = "order", location = "/person/reservation!member.action", type = "redirect"), @Result(name = "zhmm", location = "/WEB-INF/content/frontuser/pw_default.jsp"), @Result(name = "shixiao", location = "/WEB-INF/content/frontuser/pw_wuxiao.jsp"), @Result(name = "zccg", location = "frontuser!login.action", type = "redirect"), @Result(name = "forgetpasscom", location = "/WEB-INF/content/frontuser/forgetpass.jsp"), @Result(name = "yididenglu", location = "/WEB-INF/content/frontuser/yididenglu.jsp"), @Result(name = "updateinfo", location = "/person/reservation!member.action", type = "redirect", params = { "mgs", "${yudingurl}" }) }) public class FrontuserAction extends CrudActionSupport<CustomerInfo> { private static final long serialVersionUID = -4052047494894591406L; private Long id; private String url; private CustomerInfo entity; private ForgPassInfo forgPassInfo; private Page<ReservationInfo> page = new Page<ReservationInfo>(PageSize .getPagesize()); private CustomerInfoManager customerInfoManager; private MiscManager miscManager; private ForgPassManager forgPassManager; private AccountManager accountManager; private String prompt = null;// 提示 private IndexShow indexShow = new IndexShow(); private HelpSettingManager helpManager; private String yudingurl; public String getPrompt() { return prompt; } public void setPrompt(String prompt) { this.prompt = prompt; } private List<miscPo> zjlxlist; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Page<ReservationInfo> getPage() { return page; } public void setPage(Page<ReservationInfo> page) { this.page = page; } public List<miscPo> getZjlxlist() { return zjlxlist; } public void setZjlxlist(List<miscPo> zjlxlist) { this.zjlxlist = zjlxlist; } @Autowired public void setMiscManager(MiscManager miscManager) { this.miscManager = miscManager; } public AccountManager getAccountManager() { return accountManager; } @Autowired public void setAccountManager(AccountManager accountManager) { this.accountManager = accountManager; } public ForgPassManager getForgPassManager() { return forgPassManager; } @Autowired public void setForgPassManager(ForgPassManager forgPassManager) { this.forgPassManager = forgPassManager; } public ForgPassInfo getForgPassInfo() { return forgPassInfo; } public void setForgPassInfo(ForgPassInfo forgPassInfo) { this.forgPassInfo = forgPassInfo; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public CustomerInfo getEntity() { return entity; } public void setEntity(CustomerInfo entity) { this.entity = entity; } public CustomerInfoManager getCustomerInfoManager() { return customerInfoManager; } public void setCustomerInfoManager(CustomerInfoManager customerInfoManager) { this.customerInfoManager = customerInfoManager; } public IndexShow getIndexShow() { return indexShow; } public void setIndexShow(IndexShow indexShow) { this.indexShow = indexShow; } public HelpSettingManager getHelpManager() { return helpManager; } @Autowired public void setHelpManager(HelpSettingManager helpManager) { this.helpManager = helpManager; } public String getYudingurl() { return yudingurl; } public void setYudingurl(String yudingurl) { this.yudingurl = yudingurl; } @Override public String delete() throws Exception { try { customerInfoManager.deleteCustomerInfo(id); addActionMessage("删除成功"); } catch (ServiceException e) { logger.error(e.getMessage(), e); addActionMessage("删除失败"); } return RELOAD; } /** * 支持使用Jquery.validate Ajax检验用户名是否重复. */ public String checkLoginName() { // HttpServletRequest request = ServletActionContext.getRequest(); // String newLoginName = request.getParameter("loginName"); // String oldLoginName = request.getParameter("oldLoginName"); // // if (customerInfoManager.isEmailNameUnique(newLoginName, // oldLoginName)) { // Struts2Utils.renderText("true"); // } else { // Struts2Utils.renderText("false"); // } // 因为直接输出内容而不经过jsp,因此返回null. return null; } @Override public String input() throws Exception { zjlxlist = miscManager.find("证件类型"); entity = new CustomerInfo(); Struts2Utils.getRequest().setAttribute("url", Struts2Utils.getParameter("url")); indexShow.setHelps(helpManager.findAll()); return INPUT; } // -- CRUD Action 函数 --// @Override public String list() throws Exception { List<PropertyFilter> filters = PropertyFilter .buildFromHttpRequest(Struts2Utils.getRequest()); // 设置默认排序方式 if (!page.isOrderBySetted()) { page.setOrderBy("id"); page.setOrder(Page.ASC); } indexShow.setHelps(helpManager.findAll()); return "reserlist"; } @Action(value = "/register") public String cart_empty() { indexShow.setHelps(helpManager.findAll()); return "register"; } @Action(value = "/tologin") public String tologin() { // readerCookie(); Struts2Utils.getRequest().setAttribute("appid", Constants.APP_ID); indexShow.setHelps(helpManager.findAll()); return "tologin"; } @Override protected void prepareModel() throws Exception { if (id != null) { entity = customerInfoManager.getCustomerInfo(id); } else { entity = new CustomerInfo(); } } @Override public String save() throws Exception { try { // entity.setI_cust_level(websiteBaseinfoManager // .getWebsiteBaseinfo(1l).getI_webreg_memlevel()); if ("13596439671".equals(entity.getS_cust_phone()) || (entity.getS_cust_phone() == "12345678910")) { entity.setS_cust_phone(""); } entity.setS_photo("/com.yidian/yzc_admin_images/yzc_admin1.png");// 注册时给用户设置默认的头像信息 customerInfoManager.saveCustomerInfo(entity); addActionMessage("保存成功"); LoginStatus.loginRepeat(Struts2Utils.getSession().getId(), entity .getId()); // clearcompany(); Struts2Utils.getSession().setAttribute("username", entity.getS_cust_name()); Struts2Utils.getSession().setAttribute("nickname", entity.getS_cust_name());// 修改呢称未设置时出现的null错误 Struts2Utils.getSession().setAttribute("userid", entity.getId()); // Struts2Utils.getSession().setAttribute("usertype", "customer");// // 添加用户类型 Struts2Utils.getSession().setMaxInactiveInterval(60 * 15); String checkbox = Struts2Utils.getParameter("checkbox"); Cookie(entity.getS_cust_email(), entity.getS_password(), checkbox); if (null != url && !"".equals(url)) { try { String kurl = ""; String[] itemStrArr = url.split(","); for (int i = 0; i < itemStrArr.length - 1; i++) { kurl += itemStrArr[i] + "&"; } kurl = kurl + itemStrArr[itemStrArr.length - 1]; kurl = new String(kurl.getBytes("UTF-8"), "ISO-8859-1"); Struts2Utils.getRequest().setAttribute("url", kurl); Struts2Utils.getResponse().sendRedirect(kurl); } catch (IOException e) { e.printStackTrace(); } return null; } indexShow.setHelps(helpManager.findAll()); return "order"; // return "zccg"; } catch (Exception e) { e.printStackTrace(); indexShow.setHelps(helpManager.findAll()); return INPUT; } } @Override public CustomerInfo getModel() { return entity; } public String login() { if (null == entity) { if (StringUtils.isEmpty(Struts2Utils.getParameter("url"))) { prompt = "用户名和密码不能为空"; } putKurl(url); Struts2Utils.getRequest().setAttribute("appid", Constants.APP_ID); indexShow.setHelps(helpManager.findAll()); return "save"; } // entity.setS_cust_phone(entity.getS_cust_email()); String username = entity.getS_cust_email(); String password = entity.getS_password(); List<customerInfoPo> cus = customerInfoManager.chicklogin(entity); entity.setS_password(password); if (cus.size() > 0) { if (cus.get(0).getI_record_status() == 3) {// 黑名单 prompt = "黑名单"; Struts2Utils.getRequest().setAttribute("appid", Constants.APP_ID); return "save"; } else { LoginStatus.loginRepeat(Struts2Utils.getSession().getId(), cus .get(0).getId()); // clearcompany(); Struts2Utils.getSession().setAttribute("username", cus.get(0).getS_cust_name()); Struts2Utils.getSession().setAttribute("userid", cus.get(0).getId()); // Struts2Utils.getSession().setAttribute("usertype", // "customer"); String nickname = cus.get(0).getS_nickname(); if (StringUtils.isEmpty(nickname)) { nickname = cus.get(0).getS_cust_name(); } Struts2Utils.getSession().setAttribute("nickname", nickname); Struts2Utils.getSession().setMaxInactiveInterval(60 * 15); String checkbox = Struts2Utils.getParameter("checkbox"); Cookie(username, password, checkbox); if (null != url && !"".equals(url)) { if (cus.get(0).getS_cust_email() == null || cus.get(0).getS_cust_phone() == null || cus.get(0).getS_cust_phone().trim().equals("") || cus.get(0).getS_cust_email().trim().equals("")) { AjaxUtil.ajaxResponseAlert(Struts2Utils.getResponse(), "请先完善个人的联系电话和邮箱!"); Struts2Utils.getRequest().setAttribute("mgs", Constants_Yidian.kurl); return "updateinfo"; } else { try { Struts2Utils.getResponse().sendRedirect( Constants_Yidian.kurl); Constants_Yidian.kurl = null; } catch (IOException e) { e.printStackTrace(); } return null; } } else { indexShow.setHelps(helpManager.findAll()); return "order"; } } } else { // 用户名或密码错误 prompt = "用户名或密码错误"; // websiteBaseinfo = websiteBaseinfoManager.getWebsiteBaseinfo(1l); Struts2Utils.getRequest().setAttribute("appid", Constants.APP_ID); indexShow.setHelps(helpManager.findAll()); return "save"; } } private void clearcompany() { Struts2Utils.getSession().removeAttribute("companyname"); Struts2Utils.getSession().removeAttribute("companyid"); Struts2Utils.getSession().removeAttribute("nickcompany"); } public void putKurl(String url) { String kurl = ""; if (url == null) { return; } String[] itemStrArr = url.split(","); for (int i = 0; i < itemStrArr.length - 1; i++) { kurl += itemStrArr[i] + "&"; } kurl = kurl + itemStrArr[itemStrArr.length - 1]; try { kurl = new String(kurl.getBytes("UTF-8"), "ISO-8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } Constants_Yidian.kurl = kurl; } /** * 添加删除cookie * * @param customerInfoPo * @param password * @param checkbox */ private void Cookie(String username, String password, String checkbox) { Cookie cookieusername = new Cookie(username, password); Cookie cookie = new Cookie("loginusername", username); // Cookie cookieusertype = new Cookie("usertype", "customer"); cookieusername.setPath("/"); cookie.setPath("/"); // cookie.setPath("/"); if (checkbox != null) { cookieusername.setMaxAge(365 * 24 * 60 * 60); cookie.setMaxAge(365 * 24 * 60 * 60); // cookie.setMaxAge(365 * 24 * 60 * 60); } else { cookieusername.setMaxAge(0); cookie.setMaxAge(0); // cookie.setMaxAge(0); } HttpServletResponse response = ServletActionContext.getResponse(); response.addCookie(cookieusername); response.addCookie(cookie); // response.addCookie(cookieusertype); } /** * 读取kookie的内容 */ /* * public void readerCookie() { HttpServletRequest request = * ServletActionContext.getRequest(); Cookie[] cookies = * request.getCookies(); String username = null; String loginmsg = null; if * (cookies != null && cookies.length > 0) { * * for (int i = 0; i < cookies.length; i++) { if * (cookies[i].getName().equals("loginusername")) { username = * cookies[i].getValue(); break; } } * * for (int i = 0; i < cookies.length; i++) { if * (cookies[i].getName().equals(username)) { loginmsg = * cookies[i].getValue(); } } } if (username != null && loginmsg != null) { * entity = new CustomerInfo(); entity.setS_cust_email(username.replace("#", * "@")); entity.setS_password(loginmsg); } * * } */ // 验证码 @Action(value = "/checkValidateCode") public void checkValidateCode() { String code = Struts2Utils.getParameter("code"); String vc = (String) Struts2Utils.getSession().getAttribute( com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); if (null != vc && !vc.equals(code)) { AjaxUtil.ajaxResponse(Struts2Utils.getResponse(), "0"); } else { AjaxUtil.ajaxResponse(Struts2Utils.getResponse(), "1"); } } /** * 支持使用Jquery.validate Ajax检验email是否重复. */ public String check() { HttpServletRequest request = ServletActionContext.getRequest(); String key = request.getParameter("k"); String value = request.getParameter("v"); String id = request.getParameter("i"); Map<String, String> map = new HashMap<String, String>(); map.put(key, value); map.put("id", id); if (customerInfoManager.check(map)) { Struts2Utils.renderText("true"); } else { Struts2Utils.renderText("false"); } return null; } @Action(value = "/forgetpasscom") public String forgetpass() { indexShow.setHelps(helpManager.findAll()); return "forgetpasscom"; } /** * 忘记密码 * * @return */ @Action("/forgetpasswordcom") public void forgetpassword() { String email = Struts2Utils.getParameter("email"); CustomerInfo cus = customerInfoManager.findUserByLoginName(email); if (cus == null) { Struts2Utils.renderText("您的账户不存在!"); return; } String path = Struts2Utils.getRequest().getContextPath(); String basePath = Struts2Utils.getRequest().getScheme() + "://" + Struts2Utils.getRequest().getServerName() + ":" + Struts2Utils.getRequest().getServerPort() + path + "/"; Date date = new Date(); Long time = date.getTime(); String code = RandomUtil.getRandomCode(); String html = " " + cus.getS_cust_name() + ":<br> 您好,<a href='" + basePath + "frontuser/frontuser!zhmm.action?id=" + cus.getId() + "&code=" + Md5Util.encode(code) + "&time=" + time + "' style='color: blue; font-weight: bold;'>点击此处</a>对您的用户登录密码进行设置,为了您账户的安全,请在24小时之内进行密码修改,否则本链接将会失效<br><span style='float:right;margin-right: 50px;'>————云租车(www.yunzuche.com)</span>"; forgPassInfo = new ForgPassInfo(); forgPassInfo.setS_verification_code(code); forgPassInfo.setD_insert_time(date); forgPassInfo.setI_cust_id(cus.getId()); forgPassManager.save(forgPassInfo); Mail.sendHtmlEmail(html, "云租车会员取回密码验证", cus.getS_cust_email(), cus .getS_cust_name()); Struts2Utils.renderText("邮箱验证已发到您邮箱,请到您的邮箱查收"); return; } public String zhmm() { String id = Struts2Utils.getParameter("id"); String code = Struts2Utils.getParameter("code"); String time = Struts2Utils.getParameter("time"); ForgPassInfo fp = new ForgPassInfo(); fp.setD_insert_time(new Date(Long.parseLong(time))); fp.setI_cust_id(Long.parseLong(id)); fp.setS_verification_code(code); ForgPassInfo f = forgPassManager.findForgPassInfo(fp); CustomerInfo cus = customerInfoManager.getCustomerInfo(MathUtils .StringToLong(id)); if (f != null) { Long l = (new Date()).getTime() - f.getD_insert_time().getTime(); if (l < 1000 * 60 * 60 * 24 && l > 0) { if (Md5Util.encode(f.getS_verification_code()).equals(code)) { Struts2Utils.getRequest().setAttribute("id", fp.getI_cust_id()); Struts2Utils.getSession().setAttribute("fgid", f.getId().toString()); Struts2Utils.getSession().setAttribute("zhmmcusid", cus.getId()); try { Struts2Utils.getResponse().sendRedirect( "redirectpasscom.action"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } } indexShow.setHelps(helpManager.findAll()); return "shixiao"; } @Action(value = "changepass", results = { @Result(name = "error", location = "/WEB-INF/content/frontuser/pw_cuowu.jsp") }) public String changePass() { CustomerInfo cus = customerInfoManager.getCustomerInfo(entity.getId()); boolean t1 = true; boolean t2 = true; String fgid = Struts2Utils.getParameter("fgid"); if (cus != null) { if (cus.getS_cust_phone() != null) { if (cus.getS_cust_phone().equals(entity.getS_cust_phone())) { t1 = true; } else { t1 = false; } } if (cus.getS_id_code() != null) { if (cus.getS_cust_phone().equals(entity.getS_cust_phone()) && cus.getS_id_code().equals(entity.getS_id_code())) { t2 = true; } else { t2 = false; } } } if (t1 && t2) { if (fgid == null) { indexShow.setHelps(helpManager.findAll()); return "error"; } try { Struts2Utils.getSession() .setAttribute("zhmmcusid", cus.getId()); Struts2Utils.getSession().setAttribute("fgid", fgid); Struts2Utils.getResponse().sendRedirect( "redirectpasscom.action"); } catch (IOException e) { e.printStackTrace(); } return null; } indexShow.setHelps(helpManager.findAll()); return "error"; } @Action(value = "redirectpasscom", results = { @Result(name = "success", location = "/WEB-INF/content/frontuser/pw_newmm.jsp") }) public String redirectPass() { Long userid = (Long) Struts2Utils.getSession() .getAttribute("zhmmcusid"); String fgid = (String) Struts2Utils.getSession().getAttribute("fgid"); Struts2Utils.getSession().removeAttribute("zhmmcusid"); Struts2Utils.getRequest().setAttribute("userid", userid); Struts2Utils.getRequest().setAttribute("fgid", fgid); indexShow.setHelps(helpManager.findAll()); return SUCCESS; } @Action(value = "updatepasscom", results = { @Result(name = "chenggong", location = "/WEB-INF/content/frontuser/pw_xgcg.jsp") }) public String updatepass() { customerInfoManager.update(entity); Long fgid = Long.parseLong(Struts2Utils.getParameter("fgid")); forgPassManager.updateStateById(fgid); indexShow.setHelps(helpManager.findAll()); return "chenggong"; } }