📄 addonetocartaction.java
字号:
/*
* OPIAM Suite
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package opiam.admin.applis.demo.actions;
import opiam.admin.applis.demo.beans.Person;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.SortService;
import opiam.admin.faare.struts.actions.SecureAction;
import opiam.admin.faare.struts.utils.SessionContext;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* This action allows to add in the temporary list the current entry.
*/
public class AddOneToCartAction extends SecureAction
{
/** logger de log4j */
private static Logger _logger = Logger.getLogger(AddOneToCartAction.class);
/**
* This method is called to execute the action once the checks have been
* performed by the SecureAction class.
*
* @param mapping Struts mapping data.
* @param actionForm FormBean associated with the action.
* @param request HTTP request.
* @param httpServletResponse HTTP response.
*
* @return An ActionForward.
*
* @throws IOException An I/O exception if failed or interrupted I/O operations occurs.
* @throws ServletException A ServletException if the servlet has a problem.
*/
public org.apache.struts.action.ActionForward secureExecute(org.apache.struts.action.ActionMapping mapping,
org.apache.struts.action.ActionForm actionForm,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse httpServletResponse
)
throws java.io.IOException, javax.servlet.ServletException
{
SessionContext sessionContext = null;
UserContext userContext = null;
ActionMessages errors = new ActionMessages();
try
{
// Gets the session.
HttpSession session = request.getSession();
// Gets the session context.
sessionContext = SessionContext.getInstance(session);
// Gets the user context.
userContext = sessionContext.getUserContext();
// Gets the current entry
Person person = (Person)session.getAttribute("currentEntry");
// Gets the temporary list
String cartname = "panier1";
List currentCart = (List) session.getAttribute(cartname);
// Creates the list if it doesn't exist and puts it in session
if (currentCart == null)
{
// nouveau panier
_logger.debug("cart " + cartname + " is null, will be created");
currentCart = new ArrayList();
// Adds the person to the temporary list
currentCart.add(person);
_logger.debug("Add " + person.getDn() + " to the temmpary list.");
}
else
{
HashMap map = new HashMap();
for (int i = 0; i < currentCart.size(); i++)
{
Person pers = (Person)currentCart.get(i);
map.put(pers.getDn(), pers);
}
if (!map.containsKey(person.getDn()))
{
currentCart.add(person);
}
// Sorts the list
SortService.sort(currentCart, Person.class, "name",
SessionContext.getInstance(session).getUserContext()
);
}
session.setAttribute(cartname, currentCart);
// Manages the display of the tempList icon
boolean indic = userContext.getParameters().containsKey("cartIndic");
if (!indic)
{
userContext.getParameters().put("cartIndic", "cartname");
}
return (mapping.findForward("success"));
}
catch (Exception se)
{
_logger.debug("Trace", se);
_logger.error(se.getMessage());
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("error.service.unknown")
);
saveErrors(request, errors);
return (mapping.findForward("service_error"));
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -