薄薄的Java
          技術本來很廣,但我們可以把它變薄!
          posts - 0,  comments - 2,  trackbacks - 0

          例子使用的工具:MyEclipse6.6,Jboss5.1, Tomcat6.18.
           實現功能描述:通過Struts2調用EJB3.0的遠程方法,完成簡單的登陸功能,只是簡單的字符串比對,未使用持久層。

          首先創鍵EJB project,我的項目名稱為:S2Login。然后定義Session Bean   LoginServiceBean實現遠程接口LoginServiceBeanRemote,代碼如下:

          遠程接口:

           1 package com.elric.s2login.ejb.sessionbeans;
           2 
           3 import javax.ejb.Remote;
           4 
           5 @Remote
           6 public interface LoginServiceBeanRemote
           7 {
           8   public boolean login(String name,String pwd);
           9 }
          10 

          實現接口類:
           1 package com.elric.s2login.ejb.sessionbeans;
           2 
           3 import javax.ejb.Remote;
           4 import javax.ejb.Stateless;
           5 
           6 @Stateless
           7 @Remote
           8 public class LoginServiceBean implements LoginServiceBeanRemote
           9 {
          10 
          11     public boolean login(String name, String pwd)
          12     {
          13         if (name.equalsIgnoreCase("zhangsan"&& pwd.equalsIgnoreCase("123"))
          14             return true;
          15         else
          16             return false;
          17     }
          18 
          19 }
          20 

          然后部署到JBoss服務器上,啟動服務器,登入查看JNDI信息,記錄下來:





          接下來,編寫客戶端部分了。我們使用Struts2來完成登錄頁面和業務調用,Web.xml、struts.xml、index.jsp、LoginAction的開發配置如下:

          web.xml

           1 <?xml version="1.0" encoding="UTF-8"?>
           2 <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
           3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
           5     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
           6     <welcome-file-list>
           7         <welcome-file>index.jsp</welcome-file>
           8     </welcome-file-list>
           9     <!-- 定義Struts 2的FilterDispatcher的Filter -->
          10 
          11     <filter>
          12         <!-- 定義核心Filter的名字 -->
          13         <filter-name>struts2</filter-name>
          14         <!-- 定義核心Filter的實現類 -->
          15         <filter-class>
          16             org.apache.struts2.dispatcher.FilterDispatcher
          17         </filter-class>
          18     </filter>
          19 
          20     <!-- FilterDispatcher用來初始化Struts 2并且處理所有的Web請求 -->
          21     <filter-mapping>
          22         <filter-name>struts2</filter-name>
          23         <url-pattern>/*</url-pattern>
          24     </filter-mapping>
          25 
          26 </web-app>
          27 


          struts.xml

           1 <?xml version="1.0" encoding="GBK"?>
           2 
           3 <!-- 指定Struts 2配置文件的DTD信息 -->
           4 
           5 <!DOCTYPE struts PUBLIC
           6 
           7         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
           8 
           9         "http://struts.apache.org/dtds/struts-2.0.dtd">
          10 
          11 <!-- struts是Struts 2配置文件的根元素 -->
          12 
          13 <struts>
          14 
          15     <!-- Struts 2的Action必須放在指定的包空間下定義 -->
          16 
          17     <package name="struts2" extends="struts-default">
          18 
          19         <!-- 定義login的Action,該Action的實現類為lee.Action類 -->
          20 
          21         <action name="Login" class="com.elric.s2login.actions.LoginAction">
          22 
          23             <!-- 定義處理結果和資源之間映射關系。 -->
          24 
          25             <result name="error">/error.jsp</result>
          26 
          27             <result name="success">/welcome.jsp</result>
          28 
          29         </action>
          30 
          31     </package>
          32 
          33 </struts>
          34 
          35 
          36 

          index.jsp
           1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
           2 <%@ taglib  uri="/struts-tags" prefix="s"%>
           3 <%
           4 String path = request.getContextPath();
           5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
           6 %>
           7 
           8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
           9 <html>
          10   <head>
          11     <base href="<%=basePath%>">
          12     
          13     <title>My JSP 'index.jsp' starting page</title>
          14     <meta http-equiv="pragma" content="no-cache">
          15     <meta http-equiv="cache-control" content="no-cache">
          16     <meta http-equiv="expires" content="0">    
          17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
          18     <meta http-equiv="description" content="This is my page">
          19     <!--
          20     <link rel="stylesheet" type="text/css" href="styles.css">
          21     -->
          22   </head>
          23   
          24   <body>
          25     Struts2結合EJB3.0完成登錄操作:
          26     <s:form action="Login" method="post">
          27     <s:textfield name="username" label="用戶名"></s:textfield>
          28     <s:password name="password" label="密  碼"></s:password>
          29     <s:submit value="提交"></s:submit>
          30     </s:form>
          31   </body>
          32 </html>
          33 

          LoginAction
           1 package com.elric.s2login.actions;
           2 
           3 import com.elric.s2login.biz.BizLogin;
           4 import com.opensymphony.xwork2.ActionSupport;
           5 
           6 public class LoginAction extends ActionSupport
           7 {
           8     /**
           9      * 
          10      */
          11     private static final long serialVersionUID = 1L;
          12     private String username;
          13     private String password;
          14 
          15 
          16     public String getUsername()
          17     {
          18         return username;
          19     }
          20 
          21     public void setUsername(String username)
          22     {
          23         this.username = username;
          24     }
          25 
          26     public String getPassword()
          27     {
          28         return password;
          29     }
          30 
          31     public void setPassword(String password)
          32     {
          33         this.password = password;
          34     }
          35 
          36     // 具體業務實現方法
          37     public String execute()
          38     {
          39 
          40         String name = this.getUsername();
          41         String pwd = this.getPassword();
          42         
          43 
          44         Boolean b;
          45         try
          46         {
          47             b = new BizLogin().isLogin(name, pwd);
          48 
          49         } catch (Exception e)
          50         {
          51             e.printStackTrace();
          52             return ERROR;
          53         }
          54 
          55         if (b)
          56             return SUCCESS;
          57         else
          58             return ERROR;
          59     }
          60 
          61     
          62 }
          63 

          這里,我定義了一個BizLogin來完成EJB的遠程調用。
          Ps:這里發現一個問題,使用Properties文件來配置JNDI會報一個命名空間異常,無法遠程調用Jboss上的登陸方法。所以,我在這里只有老老實實的寫段Properties代碼。

           1 package com.elric.s2login.biz;
           2 
           3 import java.util.Properties;
           4 
           5 import javax.naming.InitialContext;
           6 
           7 import com.elric.s2login.ejb.sessionbeans.LoginServiceBeanRemote;
           8 
           9 public class BizLogin
          10 {
          11     public boolean isLogin(String name, String pwd)
          12             throws Exception
          13     {
          14         Properties props = new Properties();
          15         props.setProperty("java.naming.factory.initial""org.jnp.interfaces.NamingContextFactory");
          16         props.setProperty("java.naming.factory.url.pkgs""org.jboss.naming");
          17         props.setProperty("java.naming.provider.url""localhost:1099");
          18         
          19         InitialContext ctx = new InitialContext(props);
          20         LoginServiceBeanRemote s = (LoginServiceBeanRemote) ctx
          21                 .lookup("LoginServiceBean/remote");
          22         System.out.println("是否成功調用遠程方法:  "+s.login(name, pwd));
          23         return s.login(name, pwd);
          24     }
          25 }
          26 

          Lookup方法中使用我們在JBoss中記錄下來的命名空間。
          這里注意一點,ctx.Lookup(“LoginServiceBean/remote”)方法返回的類型我們強轉為LoginServiceBean的接口,也就是其代理類。否則會報個代理錯誤的異常!

          最后,也是最重要的一點,我們還要將Jboss里面client文件夾下的所有jar包都復制進lib下面!這里試過很多辦法想去掉些jar,但項目總不能運行,所以全部使用。(光引入Jbossall-client是無效的,它只是個引用其他30多個Jar的只記錄了地址信息的Jar包,也試過把它上面引用的Jar統統拷進lib中,但項目依然報錯!)

          Ok,開發就此完成,部署后成功運行!


          posted on 2009-08-04 08:50 Rique 閱讀(1972) 評論(2)  編輯  收藏 所屬分類: MVC領域Webservices和EJB

          FeedBack:
          # re: Struts2整合EJB3.0簡單實例
          # re: Struts2整合EJB3.0簡單實例
          2012-09-21 17:02 | sss
          寫的太簡單了,能不能寫一個更復雜的,最好可以讓ejb發揮到他應該發揮的作用  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2011年3月>
          272812345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿

          文章分類

          文章檔案

          收藏夾

          搜索

          •  

          最新評論

          主站蜘蛛池模板: 星座| 乌海市| 乐亭县| 滦平县| 溆浦县| 东乡族自治县| 东阿县| 天峻县| 宕昌县| 云南省| 洛川县| 洪湖市| 苍梧县| 侯马市| 榕江县| 灵璧县| 仁怀市| 正镶白旗| 茌平县| 泸西县| 黎城县| 景德镇市| 青海省| 闵行区| 四川省| 视频| 莆田市| 固镇县| 天长市| 红安县| 中牟县| 万宁市| 宾阳县| 天柱县| 若尔盖县| 平山县| 双江| 玛曲县| 临清市| 拜泉县| 乌鲁木齐市|