Jbuilder8开发J2ee学习笔记(7)

类别:Java 点击:0 评论:0 推荐:

Jbuilder8开发J2ee学习笔记(7)

                                                                          作者:缪青

开发Struts

1.开发Struts

现在我们已经有了一个可以用JMS通过SessionBean,调用BMP,控制数据

表的例子了,下面我们开始使用Struts来实现通过SessionBean,调用BMP,控制数据表的例子。

新建Web Application,如下图:

接下来OK就好了。

下面开始实现Struts中的ActionForm,如下图:

 

 

由于我们要涉及到数据库中的2个字段,所以这一步应该增加,2个Field

 

Finish就可以完成ActionForm的创建。

 

接下来创建Action,如下图:

 

 

 

然后直接Finish就好了。

下面就是要建立刚才提到的input.jsp,这个JSP要和ActionForm匹配,如下图:

 

 

 

直接按Finish就好了。

 

然后建立成功页面inputOK.jsp和错误页面inputError.jsp

 

 

 

 

点Finish就好了。

 

2.书写代码

到现在我们需要的模板文件都有了,开始写代码了。

我在ActionForm中写了一些对字段的逻辑判断:

  public ActionErrors validate(ActionMapping actionMapping,

                               HttpServletRequest httpServletRequest) {

    ActionErrors errors = new ActionErrors();

    if ( (shancbz_new == null) || (shancbz_new.length() < 2)) {

      errors.add("shancbz", new ActionError("error.shancbz.required"));

    }

    if ( (xiwbz_new == null) || (xiwbz_new.length() < 2)) {

      errors.add("xiwbz", new ActionError("error.xiwbz.required"));

    }

    return errors;

  }

表示用户输入的内容不能为空,而且必须>=2

 

在Action中,我写了3个方法,getInitialContext,inputpar,inputparSess分别实现  连接JMS,调用JMS插入数据,调用SessionBean插入数据。

代码如下:

  public final static String

      JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

  public final static String URL = "t3://192.168.111.19:7001";

  private static final String QUEUE_CONNECTION_FACTORY =

      "DemoJMSConnectionFactory";

  private static final String QUEUE = "DemoJMSQueue";

  private static SessHome sessHome = null;

 

  private static InitialContext getInitialContext(String url) throws

      NamingException {

    Hashtable env = new Hashtable();

    env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);

    env.put(Context.PROVIDER_URL, url);

    return new InitialContext(env);

  }

 

  public static void inputpar(String Shancbz, String Xiwbz) throws Exception {

    Context ctx = getInitialContext(URL);

    QueueConnectionFactory qConnFact =

        (QueueConnectionFactory) ctx.lookup(QUEUE_CONNECTION_FACTORY);

    QueueConnection qConn = qConnFact.createQueueConnection();

    QueueSession qSess = qConn.createQueueSession(false,

                                                  Session.AUTO_ACKNOWLEDGE);

    Queue q = (Queue) ctx.lookup(QUEUE);

    QueueSender qSend = qSess.createSender(q);

    try {

      TextMessage txtMsg = qSess.createTextMessage(Shancbz);

      System.out.println("Sending a message to queue");

      qSend.send(txtMsg);

 

    }

    catch (Exception ex) {

      ex.printStackTrace();

      System.out.println("Error!!!!!!!!!");

    }

    //---------------------------------------------------------//

    qConn.close();

  }

 

  public static void inputparSess(String Shancbz, String Xiwbz) throws

      Exception {

    Context context = getInitialContext(URL);

    /*

     或者

        Hashtable env = new Hashtable();

        Context context = new InitialContext(env);

     */

    Object ref = context.lookup("SessBean");

    sessHome = (SessHome) PortableRemoteObject.narrow(ref, SessHome.class);

    sessHome.create().insTableAa11(Shancbz, Xiwbz);

  }

 

最后在perform中调用:

public ActionForward perform(ActionMapping actionMapping,

                               ActionForm actionForm,

                               HttpServletRequest httpServletRequest,

                               HttpServletResponse httpServletResponse) {

    LoginActionForm form = (LoginActionForm) actionForm;

    if ( (form.getShancbz_new().toString().equals(form.getXiwbz_new().toString().

                                                  trim()))) {

      HttpSession session = httpServletRequest.getSession();

      session.setAttribute("Shancbz", form.getShancbz_new());

      //---------------------------------------------------------------------//

      /*     try {

              inputpar(form.getShancbz_new().toString().trim(),

                       form.getXiwbz_new().toString().trim());

            return (actionMapping.findForward("success"));

           }

            catch (Exception ex) {

              System.out.println("insert error!");

              return (actionMapping.findForward("inputError"));

            }

      //JMS调用

      */

      try {

        inputparSess(form.getShancbz_new().toString().trim(),

                     form.getXiwbz_new().toString().trim());

      }

      catch (Exception ex) {

        System.out.println("insert error!");

        return (actionMapping.findForward("inputError"));

      }

      return (actionMapping.findForward("success"));

      //Session调用

      //---------------------------------------------------------------------//

 

    }

    ActionErrors errors = new ActionErrors();

    if (!form.getShancbz_new().equals("a")) {

      errors.add(ActionErrors.GLOBAL_ERROR,

                 new ActionError("error.Shancbz.wrong"));

    }

    if (!form.getXiwbz_new().equals("1")) {

      errors.add(ActionErrors.GLOBAL_ERROR,

                 new ActionError("error.Xiwbz.wrong"));

    }

    saveErrors(httpServletRequest, errors);

    return (actionMapping.findForward("inputError"));

  }

这里面用JMS调用,不能抛出数据库的违反唯一性错误。用Session调用,可以抛出数据库的违反唯一性错误。

 

Input.jsp的代码可以如下修改:

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ page contentType="text/html; charset=GBK" %><html:html>

<head>

<title>

input

</title>

</head>

<body>

<h1>JBuilder Generated Struts JSP for ActionForm userjmssesenbdb.LoginActionForm</h1>

<p>

<html:form action="/loginAction.do" method="POST">

<bean:message key="input.shancbz"/>

<html:text property="shancbz_new"/>

<br>

<bean:message key="input.xiwbz"/>

<html:text property="xiwbz_new"/>

<br>

<html:submit property="submit" value="Submit"/><br>

<html:reset value ="Reset"/>

</html:form>

</body>

</html:html>

 

很简单了!!!

 

inputError.jsp只要把input.jsp中的<h>改为<h1>input Error, pls reinput</h1>就好了;

 

InputOK.jsp代码如下:

<%@ page contentType="text/html; charset=GBK" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<%@ taglib uri="/WEB-INF/struts-template.tld" prefix="template" %>

<html>

<head>

<title>

inputOK

</title>

</head>

<body bgcolor="#ffffff">

<h1>

JBuilder Generated JSP

</h1>

<bean:message key="inputOK.message"/>

</body>

</html>

 

 

本文地址:http://com.8s8s.com/it/it17468.htm