1 16 17 18 package org.apache.struts.webapp.example2; 19 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.util.Locale ; 23 import javax.servlet.ServletException ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 import javax.servlet.http.HttpSession ; 27 import org.apache.commons.beanutils.PropertyUtils; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.struts.action.Action; 31 import org.apache.struts.action.ActionForm; 32 import org.apache.struts.action.ActionForward; 33 import org.apache.struts.action.ActionMapping; 34 35 36 44 45 public final class EditRegistrationAction extends Action { 46 47 48 50 51 54 private Log log = 55 LogFactory.getLog("org.apache.struts.webapp.Example"); 56 57 58 60 61 76 public ActionForward execute(ActionMapping mapping, 77 ActionForm form, 78 HttpServletRequest request, 79 HttpServletResponse response) 80 throws Exception { 81 82 Locale locale = getLocale(request); 84 HttpSession session = request.getSession(); 85 String action = request.getParameter("action"); 86 if (action == null) 87 action = "Create"; 88 if (log.isDebugEnabled()) { 89 log.debug("EditRegistrationAction: Processing " + action + 90 " action"); 91 } 92 93 User user = null; 95 if (!"Create".equals(action)) { 96 user = (User) session.getAttribute(Constants.USER_KEY); 97 if (user == null) { 98 if (log.isDebugEnabled()) { 99 log.debug(" User is not logged on in session " 100 + session.getId()); 101 } 102 return (mapping.findForward("logon")); 103 } 104 } 105 106 if (form == null) { 108 if (log.isTraceEnabled()) { 109 log.trace(" Creating new RegistrationForm bean under key " 110 + mapping.getAttribute()); 111 } 112 form = new RegistrationForm(); 113 if ("request".equals(mapping.getScope())) 114 request.setAttribute(mapping.getAttribute(), form); 115 else 116 session.setAttribute(mapping.getAttribute(), form); 117 } 118 RegistrationForm regform = (RegistrationForm) form; 119 if (user != null) { 120 if (log.isTraceEnabled()) { 121 log.trace(" Populating form from " + user); 122 } 123 try { 124 PropertyUtils.copyProperties(regform, user); 125 regform.setAction(action); 126 regform.setPassword(null); 127 regform.setPassword2(null); 128 } catch (InvocationTargetException e) { 129 Throwable t = e.getTargetException(); 130 if (t == null) 131 t = e; 132 log.error("RegistrationForm.populate", t); 133 throw new ServletException ("RegistrationForm.populate", t); 134 } catch (Throwable t) { 135 log.error("RegistrationForm.populate", t); 136 throw new ServletException ("RegistrationForm.populate", t); 137 } 138 } 139 140 if (log.isTraceEnabled()) { 142 log.trace(" Setting transactional control token"); 143 } 144 saveToken(request); 145 146 if (log.isTraceEnabled()) { 148 log.trace(" Forwarding to 'success' page"); 149 } 150 if ("Create".equals(action)) { 151 return (mapping.findForward("register")); 152 } else { 153 return (mapping.findForward("success")); 154 } 155 156 } 157 158 159 } 160 | Popular Tags |