Showing posts with label Struts. Show all posts
Showing posts with label Struts. Show all posts

Thursday, April 22, 2010

A Simple Apllication with DWR:

A Simple Apllication with DWR:

DWR: Direct Web Remoting-Easy Ajax for java

Link for DWR tutorial and download is:
http://directwebremoting.org/dwr/index.html

I found it very useful for my project PMS(Project Management System).
That's why I would like to demonstrate a simple application.

Just create webproject and put dwr.jar into lib.

Make changes in web.xml:
"<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>"

create dwr.xml and put following entries
"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 0.4//EN" "http://www.getahead.ltd.uk/dwr/dwr.dtd">

<dwr>
<allow>
<create creator="new" javascript="DwrDemo">
<param name="class" value="deep.DwrDemo"/>
</create>

</allow>
</dwr>"
value of class where you write your fuction which you want to call on any event.
and this javascript (Here DwrDemo) willbe created corresponding to that class.

Now we try to use it in a simple JSP page.
Add these lines in you JSP.

" <script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<!-- This JavaScript file is generated specifically for your application -->
<script type='text/javascript' src='dwr/interface/DwrDemo.js'></script>
<script type="text/javascript">

function demo() {
var name = DWRUtil.getValue("name");

DwrDemo.demo(name,function(val)
{
DWRUtil.setValue("first",val);
}
);
}
</script>

This javascript function calls demo() method which is inside DwrDemo method.
val is value which is returned by function demo().

<body>
<select id="name" name="name" onchange="demo();">
<option value="Deepak">Deepak</option>
<option value="Anil">Anil</option>
<option value="Akash">Akash</option>
<option value="Mayank">Mayank</option>
</select>

<input type="text" id="first"/>
</body> "

Class deep.DwrDemo is given below.
package deep;

public class DwrDemo {

public String demo(String name)
{
if(name.equals("Deepak"))
return "Hello "+name+" Pandey";
else
return "Hello "+name;
}

}

Now Run your application.

Tuesday, March 30, 2010

Project Management System

Hi,

Last time i wrote about Project Management System(PMS). URL for PMS is

http://pms.iitk.ernet.in/


Thanks!!
Deepak

Tuesday, March 2, 2010

Custom tag

1)Create a class which extends BodyTagSupport or implements Tag

package deep;

import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;


public class Deepak extends BodyTagSupport {



BodyContent bodyContent;


public int doStartTag() throws JspException {

return EVAL_BODY_TAG;
OR
return SKIP_BODY;

}

public void setBodyContent(BodyContent
bodyContent) {
this.bodyContent = bodyContent;
}

public int doAfterBody() throws JspException {

return EVAL_BODY_TAG;
OR
return SKIP_BODY;

}

public int doEndTag() throws JspException {

return EVAL_PAGE;
}
}
//If It returns SKIP_PAGE,then code after the tag will not be executed ,in case of EVAL_PAGE it willbe calculated
2)Create tld(For ex:deep.tld)

3)Edit your web.xml

4)Add line in your JSP page
<%@ taglib uri="/WEB-INF/tag/name.tld" prefix="n" %>

and use tag which you defined earlier


5)Run your application

These are the steps which involved in making custom tag.Any suggestion or correction,plz leave comment.

Thanks
Deepak

Monday, June 29, 2009

a basic application in Struts

Currently i am learning struts and i want to share some basics of it.Here i will tell how to create a basic struts application.

Struts is a free open-source framework for creating Java web applications.

Model-View-Controller (MVC) architecture:
The Model represents the business or database code, the View represents the page design code, and the Controller represents the navigational code. The Struts framework is designed to help developers create web applications that utilize a MVC architecture.(Learn MVC before learning Struts)

The framework provides three key components:

A "request" handler provided by the application developer that is mapped to a standard URI.

A "response" handler that transfers control to another resource which completes the response.

A tag library that helps developers create interactive form-based applications with server pages.

Setting Up Development Environment:

1)jdk 1.6 2)Tomcat 6.0 3)Eclipse

Step 1:-On Eclipse create new Web project and add Struts capabilities.(In myEclipse-->Click on myEclipse and project capabilities and add Struts capabilities)

Step 2:-Now create a New Form,action and JSP.

(select project-->new-->other-->MyEClipse-->WebStruts-->Struts 1.2-->Form,action,jsp)

Give name ,super class(ActionForm)

if you everything is right,it should be there.

Inside src:

1)A form bean:

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 05-20-2009
*
* XDoclet definition:
* @struts.form name="myForm"
*/
public class MyForm extends ActionForm {
/*
* Generated fields
*/


/*
* Generated Methods
*/

/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}

/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/*here you will get getter and setter of Properties,which you gave*/


}

3)A property file is also there.

# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project StrutsDemo

key=value

2)Action class

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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 com.yourcompany.struts.form.MyForm;

/**
* MyEclipse Struts
* Creation date: 03-20-2009
*
* XDoclet definition:
* @struts.action path="/my" name="myForm" input="/form/my.jsp" scope="request" validate="true"
*/
public class MyAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

/* Write logic here or function which you want to perform*/
MyForm myForm = (MyForm) form;// TODO Auto-generated method stub


return mapping.findForward null;
}

}

Inside WEB-INF

3)Struts_config

I feel its the heart of struts.

Entries are given below:












attribute="myForm"
input="/form/my.jsp"
name="myForm"
path="/my"
scope="request"
type="com.yourcompany.struts.action.MyAction" >

name="success"
path="/Success.jsp"
redirect="true" />








4)web.xml


action
org.apache.struts.action.ActionServlet

config
/WEB-INF/struts-config.xml


debug
3


detail
3

0


action
*.do


index.jsp

Step 3:Now you can run struts application.Give some fields in your form and try to print them in your JSP,which you will call.

For any mistake,sorry!!

and for query,plz ask.

Thanks

Deepak