Some important actions

原创 2004年08月25日 11:17:00

LoginAction.java

package vincent.actions;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


public class LoginAction extends Action{

 
public ActionForward perform(ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response){
 //can logon?
 boolean isValidate = false;
 //get the LoginForm attribute
 String userName = ((LoginForm)form).getUsername();
 String passWord = ((LoginForm)form).getPassword();
 //validate
 if(userName.equals("Admin") && passWord.equals("Admin")){
  isValidate = true;
 }
 else if(userName.equals("Customer") && passWord.equals("Customer")){
  isValidate = true;
 }
 //add to ActionErrors
 if(isValidate == false){
  ActionErrors errors = new ActionErrors();
  errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionError("error.login.failed"));
  this.saveErrors(request,errors);
  return mapping.findForward("Logon"); 
 } 
 //get the session
 HttpSession session = request.getSession();
 session.setAttribute("userName",userName);
 session.setAttribute("passWord",passWord);
 session.setAttribute("LoginForm",(LoginForm)form);//Put it in the session cause the jsp may use it
 return (mapping.findForward("View"));
  
  
}

}

LoginForm.java

package vincent.actions;


import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.ValidatorForm;


public class LoginForm extends ValidatorForm{
 //declare the fields of the form here
 private String username = null;
 private String password = null;

 
 public void setUsername(String username){
  this.username = username;
 }
 public void setPassword(String password){
  this.password = password;
 }
 public String getUsername(){
  return (this.username);
 }
 public String getPassword(){
  return (this.password);
 }
 /**
  * This is the validation
  * It must not be omitted.It MUST not be implemented cause the ValidationPlugin will do that.
  */
 public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){
  return null;
 }
}

ViewAction.java

package vincent.actions;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

import vincent.CMP.ProductLocal;
import vincent.SLSBean.*;
import vincent.util.PageControl;

public final class ViewAction extends Action{
 //cache the accessManagerHome here
 AccessManagerHome accessManagerHome = null;
 ArrayList productList = null;
 ArrayList array = new ArrayList();
 
 /**
  * Initializing the accessManagerHome
  *
  */
 public void initAccessManager(){
  System.out.println("Loading AccessManager in the servlet");
  try {
   accessManagerHome = getHome();
  } catch (NamingException e) {
   //Have to add something later
   e.printStackTrace();
  }
 }
 
 /**
  * Get the AccessManagerHome
  * @return
  * @throws NamingException
  */
 public AccessManagerHome getHome() throws NamingException {
  Object result = getContext().lookup(AccessManagerHome.JNDI_NAME);
  return (AccessManagerHome)PortableRemoteObject.narrow(result,AccessManagerHome.class);
 }
 
 /**
  * Get the Context for JNDI
  * @return
  * @throws NamingException
  */
 public InitialContext getContext() throws NamingException {
  Hashtable props = new Hashtable();

  props.put(
    InitialContext.INITIAL_CONTEXT_FACTORY,
  "org.jnp.interfaces.NamingContextFactory");
  props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099");

  // This establishes the security for authorization/authentication
  // props.put(InitialContext.SECURITY_PRINCIPAL,"username");
  // props.put(InitialContext.SECURITY_CREDENTIALS,"password");

  InitialContext initialContext = new InitialContext(props);
  return initialContext;
 }
 
 public void setArrayList(ArrayList arrayList,PageControl pageControl){
  int startRow = pageControl.getPageStartRow();
  int endRow = pageControl.getPageEndRow();
  Iterator iterator = arrayList.iterator();
  for(int i=1;i<startRow;i++){
   iterator.next();
  }
  for(int i=startRow;i<=endRow;i++){
   ProductLocal element = (ProductLocal) iterator.next();
   ProductForm productform = new ProductForm();
   //add the info to productform
   productform.setId(element.getID().toString());
   //test whether it can add
   productform.setName(element.getName());
   productform.setRetailPrice(element.getRetailPrice().toString());
   productform.setWholeSalePrice(element.getWholeSalePrice().toString());
   productform.setQuantity(element.getQuantity().toString());
   productform.setDescription(element.getDescription());
   productform.setDiscountPrice(element.getDiscountPrice().toString());
   //add it to the list
   array.add(productform);
  }
 }
 /**
  * perform the main function here
  */
 public ActionForward perform(ActionMapping mapping,
   ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response)
  throws IOException,ServletException{
  //At first I get the session
  HttpSession session = request.getSession();
  PageControl pageControl = (PageControl) session.getAttribute("pageControl");
  //get the username and password from the session.
     //if the IE has been restarted,return to the first index page
     String userName = (String) session.getAttribute("userName");
  String passWord = (String) session.getAttribute("passWord");
  //Validate here
  /*if(userName == null || passWord == null)
  {
   return (mapping.findForward("Logon"));
  }*/
  /*else if(!userName.equals("Admin") || !userName.equals("Customer"))
  {
   return (mapping.findForward("Logon"));
  }
  */
  
  //get the accessManagerHome initialized
  initAccessManager();
   //reset your array first
   array = new ArrayList();
   try {
    accessManagerHome.create().calculateDiscount((float)0.75);
   } catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (CreateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   try {
    productList = accessManagerHome.create().getAllRecords();
   } catch (RemoteException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   } catch (CreateException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
   //get the pages information from request
   String pageInfo = request.getParameter("pageInfo");
   //if null create the new PageControl
   if(pageInfo == null){
    pageControl = new PageControl(productList.size());
    session.setAttribute("pageControl",pageControl);
   }
   else if(pageInfo.equals("next")){
    pageControl.setCurrentPage(pageControl.getNextPage());
   }
   else if(pageInfo.equals("previous")){
    pageControl.setCurrentPage(pageControl.getPreviousPage());
   }
   //transfer the productlist to the array
   //in array the element in "ProductForm"
   setArrayList(productList,pageControl);
     //add the PageForm info
   PageForm pageForm = new PageForm();
   String hasNext = null;
   if(pageControl.hasNext){
    hasNext = "yes";
   }
   else{
    hasNext = "no";
   }
   String hasPrevious = null;
   if(pageControl.hasPrevious){
    hasPrevious = "yes";
   }
   else{
    hasPrevious = "no";
   }
  pageForm.setHasNext(hasNext);
  pageForm.setHasPrevious(hasPrevious);
     //add it to the session
  request.setAttribute("array",array);
  session.setAttribute("PageForm",pageForm);
  
  
  //redirect to ProductList.jsp with the same request(I will verify later)
  return mapping.findForward("ProductList");
  }
}

Android 秒级编译框架配置过程 --Freeline

Freeline是蚂蚁金服旗下一站式理财平台蚂蚁聚宝团队在Android平台上的量身定做的一个基于动态替换的编译方案,稳定性方面:完善的基线对齐,进程级别异常隔离机制。性能方面:内部采用了类似Face...
  • ocean20
  • ocean20
  • 2016-11-18 13:23:47
  • 1513

安装XAMPP 警示

"Important! Because an activated User Account Control (UAC) on your system some functions of XAMPP a...
  • pwq296306654
  • pwq296306654
  • 2015-07-03 19:41:05
  • 3617

TOEFL(20151212)分析

----------------------------------------阅读----------------------------------------------- R1:欧洲政府发展...
  • Canhui_WANG
  • Canhui_WANG
  • 2016-03-07 21:34:27
  • 345

UVA 10006

UVA 10006 An importanttopic nowadays in computer science is cryptography. Some people even think tha...
  • SnowdenJobs
  • SnowdenJobs
  • 2016-08-28 10:41:55
  • 309

HBASE我遇坑之client.AsyncProcess: #1, waiting for 5012 actions to finish

项目使用phoenix连接HBASE,数据用spark处理后通过phoenix批量插入到HBASE中, 其中,提交频率为5000条提交一次   发现插入表大的时候,速度变慢,查看日志发现如下线索: c...
  • xiefu5hh
  • xiefu5hh
  • 2016-11-06 21:48:06
  • 2891

IELTS12 Test5 It is good to share as much information as possible in any areas

这是IELTS12 Test5的一个话题,题目主要是讲分享信息的重要性讨论,我列举了我在科研上遇到的信息分享的问题,并鼓励大家进行分享,当然,有很多人处于种种原因,不愿意进行分享,我们要尊重他们,不能...
  • w5688414
  • w5688414
  • 2017-08-15 17:02:27
  • 627

java jre jdk 下载区别 以及环境变量配置(Error:could not open jvm.cfg 问题解决方案亲自测

JRE(Java Runtime Enviroment)是Java的运行环境。面向Java程序的使用者,而不是开发者。如果你仅下载并安装了JRE,那么你的系统只能运行Java程序。JRE是运行Java...
  • u012611644
  • u012611644
  • 2018-02-17 16:35:52
  • 35

雅思作文_教育类(一)

题目.1 Art classes, like painting and drawing, arenot as important as other subjects, so some people t...
  • xf_87
  • xf_87
  • 2017-12-22 10:24:31
  • 205

PHP 问题汇总

1、important! because an activated user account control 告诉你不要整在C盘的86目录,会用用户权限问题 2、it seems you have a...
  • baodinglaolang
  • baodinglaolang
  • 2015-08-19 13:48:26
  • 318

Android 开发指南 翻译 User Interfaces -- Action Bar

Action bar 行动条 The action bar is a window feature that identifies the application and user locati...
  • wangqingxun
  • wangqingxun
  • 2012-02-27 09:12:14
  • 3327
收藏助手
不良信息举报
您举报文章:Some important actions
举报原因:
原因补充:

(最多只允许输入30个字)