Thursday, September 3, 2009
Thursday, July 23, 2009
PagingAction class (struts)
public class PagingAction extends org.apache.struts.action.Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
//-------------create session--------------
HttpSession sess = request.getSession();
//-------success and error forwards-------------
String SUCCESS = "success";
String ERROR = "error";
ActionMessages errors = new ActionMessages();//----all errors----------
//----Utility Class containing the createPartedList() function--------
UtilityClass uc = new UtilityClass();
//-----------------start of trycatch-----------------------
try{
String list ="";
int rec = 5; //no of rec per page
int total=0;
int pages=0;
int page = Integer.parseInt(request.getParameter("pg"));
switch (mod){
/*depending on different criteria you can set the incoming List object
and other values like forward path.*/
case 1: list = "list1";
//case 2: list1 = "list2";
}
List data = (ArrayList)sess.getAttribute(list);
if(data==null||data.size()<1){
errors.add("error.errmsg",new ActionMessage("error.noitem"));
}
total = data.size(); //total item in collection
pages=(total%rec==0)?total/rec:(total/rec)+1;
//total no. of pages set as session attribute
sess.setAttribute("pages", pages);
//return the list corresponding to the particular page number
data = (ArrayList)uc.createPartedView(data, rec, page)*;
//refer to previous post for this function
if(data.size()<1){
errors.add("errnoitem",new ActionMessage("error.noitem"));
}
//setting the list as request attribute
request.setAttribute(list, data);
}catch(Exception e){
errors.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errmsg"));
}//----------------end of trycatch---------------
saveErrors(request,errors);//---save all error------
//Forward the page
return mapping.findForward(SUCCESS);
}
}
also you can create a jsp include page which will retrieve the total no of pages
and display the page numbers as hyperlink calling to this paging action class
this PagingAction class can also be created as Filter
paging using list
//--------------------Method to create parted list for paging-----------------------
/*takes List(the collection),no of records/page and the current page number as parameter*/
public List createPartedView(List data, int rec,int pageno)
{
int from=1;
int to=0;
int total=data.size();
if(pageno==0||pageno==1){
from =0;
to=from+rec>total?total:from+rec;
}
else{
from =(pageno-1)*rec;
to=from+rec>total?total:from+rec;
}
return new ArrayList(data.subList(from, to));
}
java mail function
/*sendMail function for single recipient*/
public void sendMail(String from, String to, String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo = new InternetAddress(to);
message.setRecipient(javax.mail.Message.RecipientType.TO, addressTo);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/html");
Transport.send(message);
}
/*sendMail function for multiple recipient*/
public void sendMail(String from, String to[], String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo[] = new InternetAddress[to.length];
for(int i = 0; i < to.length; i++)
addressTo[i] = new InternetAddress(to[i]);
message.setRecipients(javax.mail.Message.RecipientType.TO, addressTo);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/plain");
Transport.send(message);
}
/*sendMail function for single recipient with cc and bcc*/
public void sendMail(String from, String to, String cc, String bcc, String subject, String messageBody)
throws MessagingException, AddressException
{
Properties props = System.getProperties();
props.put("hostname", "servername");
Session session = Session.getDefaultInstance(props, null);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress addressTo = new InternetAddress(to);
InternetAddress addressCC = new InternetAddress(cc);
InternetAddress addressBCC = new InternetAddress(bcc);
message.setRecipient(javax.mail.Message.RecipientType.TO, addressTo);
message.addRecipient(javax.mail.Message.RecipientType.CC, addressCC);
message.addRecipient(javax.mail.Message.RecipientType.BCC, addressBCC);
message.setSubject(subject);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
message.setContent(messageBody, "text/html");
Transport.send(message);
}
Subscribe to:
Comments (Atom)

