Halaman

Subscribe:

Main Menu

Kamis, Juli 24, 2008

Contoh EJB 3, Maven, GlassFish & Struts 2

Yach.. buat arsip aja. contoh kecil dari project EJB3 dan Struts2 make maven buildnya :), trus pake GlassFish untuk deploy dan di tampilkan di browser.

Pertama untuk Project EJB3 nya class2nya antara lain
  • RemovableRemote

import javax.ejb.Remote;

/**
*
* @author bastomi
*/
@Remote
public interface RemovableRemote {
public String remove(String message);

}


  • RemovableBean

import javax.ejb.Stateless;

/**
*
* @author bastomi
*/
@Stateless(name="RemovableBean", mappedName="ejb/Removable")
public class RemovableBean implements RemovableRemote{

public String remove(String message){
return message;
}
}

  • pom.xml nya kayak gini

<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>EJBLatihan</groupId>
<artifactId>EJBLatihan</artifactId>
<version>1</version>
<packaging>ejb</packaging>
<description></description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>

</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-ejb_3.0_spec</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>


untuk project Clientnya kita make Struts2. file2nya kayak gini

main
---java
|----latihan
|----HelloWorld.java
|----RemovableClient.java
---resource
|-----struts.xml
---webapp
|-----index.jsp
|-----WEB-INF
| |----web.xml
|-----latihan
|----HelloWorld.jsp

  • HelloWorld.java

package latihan;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
* Set welcome message.
*/
public class HelloWorld extends ActionSupport {

public String execute() throws Exception {
// setMessage(getText("nama"));
RemovableClient rem=new RemovableClient();

setMessage(rem.getRemote());
return Action.SUCCESS;
}

/**
* Provide default valuie for Message property.
*/
public static final String MESSAGE = "PesanQ";

/**
* Field for Message property.
*/
private String message;

/**
* Return Message property.
*
* @return Message property
*/
public String getMessage() {
return message;
}

/**
* Set Message property.
*
* @param message Text to display on HelloWorld page.
*/
public void setMessage(String message) {
this.message = message;
}
}

  • RemovableClient

package latihan;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
*
* @author bastomi
*/
public class RemovableClient {

public RemovableClient(){

}

public String getRemote(){
Context ctx = null;
RemovableRemote removable = null;
try {
ctx = new InitialContext();
removable = (RemovableRemote) ctx.lookup("ejb/Removable");
} catch (NamingException e) {
return e.getMessage()+"==>"+e.toString();
// TODO Auto-generated catch block
//e.prinmvntStackTrace();
}
if(removable==null){
return "Kosong";
}else
return removable.remove("Look at the server's log");

}

}

  • Struts.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.devMode" value="false" />

<package name="latihan" namespace="/latihan" extends="struts-default">
<action name="HelloWorld" class="latihan.HelloWorld">
<result name="success">/latihan/HelloWorld.jsp</result>
</action>
</package>
</struts>


  • HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello
<s: property value="message"/>
<br>
kok ngene yo?
<s: property value="nama"/>
</body>
</html>

  • web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="starter" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Q</display-name>

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

  • index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome to My First Struts..
<br>
Mboh lebokno ngawo ae karepmu :
<s:form action="latihan/HelloWorld.action">
<s:textfield label="Masukkan nama" name="message"/>
<s:submit/>
</s:form>
</body>
</html>

  • pom.xml nya kayak gini

<?xml version="1.0" encoding="UTF-8"?><project>
<modelVersion>4.0.0</modelVersion>
<groupId>EJBClient</groupId>
<artifactId>EJBClient</artifactId>
<version>1</version>
<packaging>war</packaging>
<description></description>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.0.11.1</version>
</dependency>
<dependency>
<groupId>EJBLatihan</groupId>
<artifactId>EJBLatihan</artifactId>
<version>1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.0.1</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
</configuration>
</plugin>
</plugins>
</build>
</project>

1 komentar:

Anonim mengatakan...

jumat ini tolong di jelaskan ke kita kita. step by step yach...jadi untuk jumat ini bukan pak dui yang tutor tapi bang ali. nuhun li.

Regards
ANDRI