Posts Tagged ‘struts’

Library to Run Hibernate

Posted in post on June 16th, 2010 by Aditya – Be the first to comment

based on the tutorial here

http://viralpatel.net/blogs/2010/02/create-url-shortner-in-java-struts2-hibernate.html

its said

You can download all of the above JARs from http://www.ibiblio.org/maven/

yeah rite… it takes me quite some time to download the correct jars.

below is the jar that you might need.

http://rapidshare.com/files/399870721/lib.zip

Passing Parameter to Struts2 Action Class

Posted in post on June 11th, 2010 by Aditya – Be the first to comment

Drop dead simple… assuming you are receiving param p from … say URL

http://…/bohaha.action?p=”wakakaka”

in order to retrieve “wakakaka” from p, just declare inside action class

String p
getter&setter for p (can use generator) as well

and thus by default… everytime p is called in action class after the above url, it will refer to “wakakaka”

How to do redirect to an external dynamic site in Struts2

Posted in post on June 11th, 2010 by Aditya – 1 Comment

I am using Struts 2.1.8.1 (in fact every single tutorial before this use that version).

For one of my Struts2 apps I need to redirect to an external site. Preferably in a dynamic manner

This is the struts.xml contains

	    <action name="?" class="?" method="execute">
	    	<result name="redirect" type="redirect">${externalUrl}</result>
	    </action>

in the action, it will be something like

private String externalUrl;

public String getExternalUrl()
{
 return externalUrl;
}

public String execute()
{
 blablablabla
 externalUrl = blablabla;
 return "redirect";
}

this can be useful for some rest related web service

Struts2 Tags to Iterate Through Multidimensional Array

Posted in post on June 5th, 2010 by Aditya – 1 Comment

Background: I am meddling with Twitter4j – http://twitter4j.org/ – an open source library to interact with Twitter API. I am using version 2.1.3 I am using this API together with Struts2 framework.

For the simplicity of this post, which happens to be my NOTE as well… let’s say I am pulling a lot of twiter status via getUserTimeline() method http://twitter4j.org/en/javadoc/twitter4j/Twitter.html#getUserTimeline()

This method will return me a list of status which I will then push to jsp page (view). Iterating through the status is rather easy.

The execution() method in .java action class will be something like

 public String execute() throws Exception {

        Twitter twitter = new TwitterFactory().getInstance("<change to twitterusername>",
                "<change to twitter password">); //how simple is this API

        //set to retrieve first page, in which each page will contain 200 tweets (max tweets per page)
        //twitter allows people to download up to 3200 latest tweets via API
        Paging paging = new Paging(1, 200);

        statusList = twitter.getUserTimeline("adityalesmana", paging);

        //iterating the result via console for verification purpose (just for fun - purely optional)
        for (Status status : statusList) {
            System.out.println(status.getUser().getName() + ":" + status.getText());
        }

        return SUCCESS;
    }

Notice getUserTimeline() will return an array of Status – http://twitter4j.org/en/javadoc/twitter4j/Status.html
The JSP portion that iterate through the array, using Struts2 tags, will be something like

<s:if test="statusList.size > 0">
       <ol>
         <s:iterator value="statusList">
	  		<li>
	  			<s:property value="id" />
	  			| <s:property value="createdAt" /> <s:property value="createdAt.Hours" />:<s:property value="createdAt.Minutes" />
	  			| by <s:property value="user.name" />
	  			| <s:property value="user.text" />
	  		</li>
	 </s:iterator>
       </ol>
    </s:if>

In case you do not know, a small number of tweets also contains location information. Such as longitude and latitude of the place where the tweet was sent. This API by default also retrieve such information, assuming such information is available. Displaying such information is as easy as adding the following struts tag between main s:iterator

Lat <s:property value="geoLocation.latitude" />
Long <s:property value="geoLocation.longitude" />

Now to make things more complicated, twitter may not always know the exact geo long/latitude location where the tweet was sent. In fact, most of the time, those location is just some approximate Place (http://twitter4j.org/en/javadoc/twitter4j/Place.html) defined by set of coordinates that represent smallest rectangular area from which the tweet was sent (boundingBox) OR coordinates that represent polygonal area from which the tweet was sent (geometryCoordinates).

Remember that getUserTimeline() method returns array of list which could contain place (getPlace()) information.

However, upon closer examination, getBoundingBoxCoordinates() returns a MULTIdimensional array of GeoLocation[][]

How to iterate multidimensional array? I could not use place.1 as the starting point of iteration.

Solution: After looking at struts tutorial documentation, and trying to ‘infer’ what it means, I can safely conclude that using “top” value is the way to iterate through that unknown [][] array.

The simplified solution is something like

<s:iterator value="statusList">
	  		        <s:property value="id" />
	  			| <s:property value="createdAt" /> <s:property value="createdAt.Hours" />:<s:property value="createdAt.Minutes" />
	  			| by <s:property value="user.name" />
	  			| <s:property value="user.text" /> 

  				<s:iterator value="place.BoundingBoxCoordinates">
					<s:iterator value="top">
						<s:iterator value="top">
							[<s:property value="latitude" /> , <s:property value="longitude" /> ]
						</s:iterator>
					</s:iterator>
  				</s:iterator>
  				<br />
		</s:iterator>

notice I use two iterators using value=”top” to iterate each value of GeoLocation[][] returned by getPlace().getBoundingBoxCoordinates() one by one.

Using Hibernate 3.5 from 3.3 tutorial

Posted in post on June 5th, 2010 by Aditya – Be the first to comment

You never believe what much configuration does it take to make Struts-Hibernate-Spring configuration run. I am currently following a very long tutorial outlined here http://www.scribd.com/doc/25244173/Java-Struts-Spring-Hibernate-Tutorial I got the tutorial from apache.org site – and pretty much a very promising one.

In short, the tutorial shows how to create web-app using hibernate and struts. The tutorial uses hibernate 3.3 core and annotation component. It is said that ‘If by the time you read this 3.5 has been released then you won’t need annotations as it is going to be’. I am using Hibernate 3.5 and noticing by page 56, the code does not works due to missing element and such.

Apparently, after much googling and code skimming, I have concluded that Hibernate 3.5 still required javax.persistence.jar

Solution: download Javax.persistence.jar and add it to lib.

Download the jar from here
Javax.persistence.jar

Continue with the tutorial

UPDATE:
No need to download the above files. Apparently that javax.persistence,jar do not have important class such as javax.persistence.Cacheable

I am looking for a newer version of javax.persistence.jar now
Let me know if you have

http://struts.apache.org/2.1.8.1/docs/tu…

Posted in Uncategorized on May 30th, 2010 by Aditya – Be the first to comment

http://struts.apache.org/2.1.8.1/docs/tutorials.html

http://struts.apache.org/2.1.8.1/docs/guides.html

http://struts.apache.org/2.x/docs/home.html

struts.xml needs parameter to be in sequence

Posted in post on May 30th, 2010 by Aditya – 1 Comment

For some reason, I do whatever tutorial on Struts exception handling http://struts.apache.org/2.1.8.1/docs/exception-handling.html and yet the Struts giving me that obscure error message at package nodes.

It is said

The content of element type “package” must match “(result-types?,interceptors?,default-interceptor-ref?,default-
action-ref?,default-class-ref?,global-results?,global-exception-mappings?,action*)”.

I have no idea what is it until I see the tutorial mentions that I need to add two important nodes which I gladly copy pasted.

   <global-exception-mappings>
	<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
	 <exception-mapping exception="java.lang.Exception" result="error" />
   </global-exception-mappings>

  <global-results>
        <result name="securityerror">/securityerror.jsp</result>
  	<result name="error">/error.jsp</result>
   </global-results>

It took me 15 minutes to figure out that the tutorial gives me the wrong sequence of nodes. I need to do global-result before global-exception-mappings. Making the correct sequence to be:

  <global-results>
        <result name="securityerror">/securityerror.jsp</result>
  	<result name="error">/error.jsp</result>
   </global-results>

   <global-exception-mappings>
	<exception-mapping exception="org.apache.struts.register.exceptions.SecurityBreachException" result="securityerror" />
	 <exception-mapping exception="java.lang.Exception" result="error" />
   </global-exception-mappings>

and based on the above mentioned error notification and seeing the tutorial sample file, I could piece up the final struts.xml. In case you are lazy bum who do not want to download that extra 3.5 mb zip files and (assuming you follow the tutorial faithfully), the final struts.xml at the end of the day should be

<?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.devMode" value="true" />
	<constant name="struts.custom.i18n.resources" value="global" />

	<package name="basicstruts2" extends="struts-default">

		<!--
			setup the default-stack exception mapping interceptor so that any
			exceptions not caught by this application will be logged and then
			handled by the global exception mapping
		-->

		<!--
			You can configure the Struts 2 framework to log any uncaught
			exceptions. To enable logging of the exceptions being handled by the
			Struts 2 framework you must specify some parameter values in
			struts.xml. If you examine the ExceptionMappingInterceptor class API
			there are three parameter values you can set to enable logging
			(logEnabled), the log level to use (logLevel), and the log category
			(logCategory) to specify in the log message. To set these parameter
			values for all actions that use a specific stack of interceptors in a
			package include the following in struts.xml just after the opening
			package node.
		-->
		<interceptors>
			<interceptor-stack name="appDefault">
				<interceptor-ref name="defaultStack">
					<param name="exception.logEnabled">true</param>
					<param name="exception.logLevel">ERROR</param>
				</interceptor-ref>
			</interceptor-stack>
		</interceptors>

		<default-interceptor-ref name="appDefault" />

		<!--
			To enable global exception handling you need to add two nodes to
			struts.xml: global-exception-mapping and global-results. For example
			examine struts.xml from the Exception Handling code download
			(http://code.google.com/p/struts2-examples/downloads/list).
		-->

		<global-results>
			<result name="error">/error.jsp</result>
			<result name="securityerror">/securityerror.jsp</result>
		</global-results>

		<global-exception-mappings>
			<exception-mapping
				exception="org.apache.struts.register.exceptions.SecurityBreachException"
				result="securityerror" />
			<exception-mapping exception="java.lang.Exception"
				result="error" />
		</global-exception-mappings>

		<action name="causesecurityexception" class="org.apache.struts.register.action.Register"
			method="throwSecurityException">
			<result>/register.jsp</result>
		</action>

		<action name="causeexception" class="org.apache.struts.register.action.Register"
			method="throwException">
			<result>/register.jsp</result>
		</action>

		<action name="causenullpointerexception" class="org.apache.struts.register.action.Register"
			method="throwNullPointerException">
			<result>/register.jsp</result>
		</action>

		<!--
			If you need to handle an exception in a specific way for a certain
			action you can use the exception-mapping node within the action node.
		-->

		<action name="actionspecificexception" class="org.apache.struts.register.action.Register"
			method="throwSecurityException">
			<exception-mapping
				exception="org.apache.struts.register.exceptions.SecurityBreachException"
				result="login" />
			<result>/register.jsp</result>
			<result name="login">/login.jsp</result>
		</action>

		<action name="index">
			<result>/index.jsp</result>
		</action>

		<action name="hello"
			class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
			<result name="success">/HelloWorld.jsp</result>
		</action>

		<action name="register" class="org.apache.struts.register.action.Register"
			method="execute">
			<result name="success">/thankyou.jsp</result>
			<result name="input">/register.jsp</result>
		</action>

		<action name="registerInput" class="org.apache.struts.register.action.Register"
			method="input">
			<result name="input">/register.jsp</result>
		</action>
	</package>

</struts>

Struts on Eclipse could not get refreshed

Posted in post on May 30th, 2010 by Aditya – Be the first to comment

Often when I am doing coding on java, most changes I have made seldom appear instantly. To make it faster, the following steps (in order / combination) might be working.

  1. Right click on the project –> “Build Project”
  2. Stop tomcat, clean the server
  3. Restart