有了Struts資源的調(diào)用,我們繼續(xù)來完成JAVABEAN部分的編寫! 首先,我們編寫PAction.java文件來完成連接等初始化工作,源代碼如下: PAcation.java的源代碼
package guestbook.maxuan.action;
import javax.naming.*; import java.util.Properties; import java.util.*; import javax.rmi.PortableRemoteObject;
import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Locale; import java.util.Vector; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; 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.ActionServlet; import org.apache.struts.util.MessageResources; import guestbook.maxuan.ejb.control.*;
public class PAction extends Action{ //public ActionErrors errors = new ActionErrors(); public BookControl control = null; public Context getInitialContext() { String url = "t3://127.0.0.1:"; String user = null; String password = null; Properties properties = null; ActionServlet actionServlet = this.getServlet(); String port = actionServlet.getServletConfig(). getInitParameter("port"); url = url + port;
try { properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory"); properties.put(Context.PROVIDER_URL, url); if (user != null) { properties.put(Context.SECURITY_PRINCIPAL, user); properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password); }
return new InitialContext(properties); } catch(Exception e) { e.printStackTrace(); } return null; }
public BookControl getInitControl( ){ BookControl control = null; Context ctx = getInitialContext(); try{ Object object = ctx.lookup("BookControl"); BookControlHome bookcHome = (BookControlHome) PortableRemoteObject.narrow(object,
BookControlHome.class); control = bookcHome.create(); }catch(Exception e){ e.printStackTrace(); } return control; }
}
接著我們編寫LyListAction.java文件,得到所有的留言信息LIST。 LyListAction.java的源代碼
package guestbook.maxuan.action; import javax.naming.*; import java.util.Properties; import javax.rmi.PortableRemoteObject; import java.util.*;
import java.util.Locale; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ForwardingActionForward; import org.apache.struts.action.ActionServlet; import org.apache.struts.util.MessageResources; import guestbook.maxuan.ejb.control.*;
public class LyListAction extends PAction{ public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { Locale locale = getLocale(request); HttpSession session = request.getSession(); Context ctx = (Context)session.getAttribute("ctx");
if(ctx==null){ ctx = getInitialContext(); session.setAttribute("ctx",ctx); }
try{ Object object = ctx.lookup("BookControl"); BookControlHome bookHome = (BookControlHome) PortableRemoteObject.narrow(object,
BookControlHome.class); BookControl bookControl = bookHome.create(); ArrayList lyList = bookControl.getLyList(); session.setAttribute("lyList",lyList); }catch(Exception e){ e.printStackTrace(); } return (mapping.findForward("success")); } }
由于我們需要新加留言,所以這次比上次的EJB教程多出了存儲部份,留言需要一個FORM來提交,最后進行
存儲,所以我們需要新加入LyForm.java、LyEditAction.java、LySaveAction.java三個文件的編寫,源代
碼如下:
LyForm.java的源代碼
package guestbook.maxuan.action;
import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import guestbook.maxuan.ejb.entity.value.*;
public final class LyForm extends ActionForm{ public String action="add"; public String name; public String email; public String qq; public String comment; private BookValue value = new BookValue();
public String getAction() { return action; } public void setAction(String action) { this.action = action; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getQq() { return qq; } public void setQq(String qq) { this.qq = qq; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public BookValue getValue(){ value.setName(name); value.setEmail(email); value.setQq(qq); value.setComment(comment); value.setAction(action); return value; } public void setValue(BookValue value){ name = value.getName(); email = value.getEmail(); qq = value.getQq(); comment = value.getComment(); action = value.getAction(); } }
LyEditAction.java的源代碼
package guestbook.maxuan.action;
import javax.naming.*; import java.util.Properties; import java.util.*; import javax.rmi.PortableRemoteObject;
import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Locale; import java.util.Vector; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 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.ActionServlet; import org.apache.struts.util.MessageResources; import guestbook.maxuan.action.PAction; import guestbook.maxuan.ejb.entity.value.*; import guestbook.maxuan.ejb.entity.*; import guestbook.maxuan.ejb.control.*; import java.util.Collection;
public class LyEditAction extends PAction{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); String action = request.getParameter("action"); Context ctx = (Context)session.getAttribute("ctx"); BookValue value =(BookValue) request.getAttribute("value"); //errors.clear(); if (form == null) { form = new LyForm(); if ("request".equals(mapping.getScope())) { request.setAttribute(mapping.getAttribute(), form); } else { session.setAttribute(mapping.getAttribute(), form); } }
if(ctx==null){ ctx = getInitialContext(); session.setAttribute("ctx",ctx); }
LyForm lyForm = (LyForm) form; if(action!=null){ lyForm.setAction(action); } if (value!=null){ try { PropertyUtils.copyProperties(lyForm, value); } catch (Exception e) { } }
return (mapping.findForward("success")); } }
LySaveAction.java的源代碼
package guestbook.maxuan.action;
import javax.naming.*; import java.util.Properties; import java.util.*; import java.text.*; import javax.rmi.PortableRemoteObject;
import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Locale; import java.util.Vector; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.PropertyUtils; import org.apache.struts.action.ActionError; import org.apache.struts.action.ActionErrors; 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.ActionServlet; import org.apache.struts.util.MessageResources; import guestbook.maxuan.action.PAction; import guestbook.maxuan.ejb.entity.value.*; import guestbook.maxuan.ejb.entity.*; import guestbook.maxuan.ejb.control.*; import java.sql.*;
public final class LySaveAction extends PAction{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Locale locale = getLocale(request); MessageResources messages = getResources(request); HttpSession session = request.getSession(); BookValue value=null; LyForm lyForm = (LyForm) form; Context ctx = (Context)session.getAttribute("ctx"); //errors.clear(); if(control==null){ control=getInitControl(); session.setAttribute("control",control); } //errors = lyForm.check();
try { Calendar cal = Calendar.getInstance(); SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置留言時間格式 String datestr = formatter.format (new Timestamp(cal.getTime().getTime())); value = lyForm.getValue(); value.setIp(request.getRemoteAddr());//寫入訪問者IP value.setPosttime(datestr); control.savely(value); }catch(Exception e){ e.printStackTrace(); } return (mapping.findForward("success")); } }
在上面,我將原本寫在里面的檢查語句都注釋掉了,大家可以自己寫進去,也就是對郵件,留言不能為空等檢查,當(dāng)然也可以在頁面中寫javascript小腳本,不過這對有心惡意搗亂的人沒什么用! PAction.java完成一些連接等初始動作,其它的分別為顯示留言,提交留言等動作,也可以自行定義添加其它動作在里面!例如刪除!
出處:藍色理想
責(zé)任編輯:藍色
上一頁 [J2EE] 實戰(zhàn)留言本 [5] 下一頁 [J2EE] 實戰(zhàn)留言本[7]
◎進入論壇網(wǎng)絡(luò)編程版塊參加討論
|