First struts program using WSAD 5
Struts
Purpose: Provides information on how to create struts and tiles projects in WASD 5.1.2. Document is useful for practical purpose for theoretical information go through the books in References and Books section:
Prerequisites:
1) Go through the basic definitions of action class, action forward, struts-config etc.
2) Basic WASD, jsp, servlet concepts.
References and Books (free PDF):
First struts program using WSAD 5.1.2
1) file->new->dynamic web project-> give project name and check “configure Advanced options” click on next ( my project name is strutsProject2)
2) Click on next.
3) Check “Add Struts support” and “jsp tag libraries” (WASD will automatically copy the required jar files and set up the struts configurations ) .Click next
[pic]
4) Click next.
5) Click finish.
INFO
The project structure and web.xml was created by the editor. Now we need to create a jsp which just displays a message “hello world. Welcome to my site”.
For that we need welcomeAction.java , welcome.jsp,index.jsp
Index.jsp: (webcontent/index.jsp)This is called by the server when we try to access the application like () .
This is because the web.xml will declare welcome list like below by default(WASD)
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
From this jsp we are forwarding the control to another jsp welcome.jsp which is under webcontent/jsp through welcomeAction.
Create action class
1)right click the java resources folder ->new->other->Web(Struts)->Action class->next
2) give action class name->next
[pic]
3) finish.
INFO
Go to welcomeAction.java .we should see execute method with try/catch blocks
That method can be replaced like the below as we don’t need try/catch blocks for this example
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
forward = mapping.findForward("welcomeForward");
return (forward);
}
Change the forward name to welcomeForward and we need to update struts config to declare the forward name.
Create index.jsp
1)under webcontent create a new jsp file with name index.jsp
The jsp looks like below
Create welcome.jsp
Create a new folder jsp under webContent and create new jsp file welcome.jsp like this
Hello World.Welcome to my site.
Change the struts-config.xml like this. We are mapping the action forward “welcomeForward” to “/jsp/Welcome.jsp”
Finally restart the server and click on “run on server” by right clicking the project. The output will look like
[pic]
What is happening when we click on ?
1)Go to WEB.xml
The controller first goes to web.xml as usual and dispatches to index.jsp
2) Go to index.jsp
Here we are forwarding the control to /welcome.do?dispatcher=welcomeMethod"
3) Go to Web.xml again
In web.xml the controller will find only one servlet that is ActionServlet with url-pattern .do . So it is forwarded to ActionServlet to dispatch the request to jsp.
action
org.apache.struts.action.ActionServlet
config
WEB-INF/struts-config.xml
*.do
4) Go to Struts-config.xml
Find the action mapping for “welcome” and enter the welcomeAction.java class
5) Go to welcomeAction.java
In the action class we only had an execute method so that method will get executed and finally forwarded the control to “welcomeForward”
6) The control will go the struts config and finds the forward name”welcomeForward” under welcome action mapping and forwards to welcome.jsp
Struts + Tiles + DispatcherAction
References and Books (free PDF) :
First tiles program (enhancement to previous project “strutsProject2”):
We are going to display a webpage with header and footer
[pic]
[pic]
We need tiles-definitions.xml, header.jsp, footer.jsp, body.jsp, BasicLayout.jsp
JSP changes:
Create header.jsp
Header.jsp just contains a titlebar .(I choose google logo for that). The code looks like
Create Footer.jsp
It will generally contain the copyrights info, footer links, etc(I choose another google logo) the code looks like
Create body.jsp.
It is just a Place holder which we are going to replace with our jsps.
hello
Create layout basicLayout.jsp
The layout defines the page with header as header.jsp , footer as footer.jsp etc
Modifications to welcome.jsp
Welcome to my site.Please enter your name
1) here we are giving the dispatcher name as helloUserMethod so that if we submit the page it will go to helloUserMethod and from there it will forwarded to helloUSer.jsp.
Add hello.jsp
Hello .Welcome to my site
1) here we are displaying the name of the user entered in previous page by using the form bean. This will help a lot if we have so many input fields on the screen.
2) formbean was declared in struts config in such a way that it will exist in request scope.
3) If we do not use form bean then we have to manually place the name in request before forwarding to app page and on the page we need to display the name from request object.
Change to index.jsp
1) The dispatcher name is welcomeMethod so the control goes to this method in action class
Action class and action form changes
Change welcomeAction
package com..strutsproject.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com..strutsproject.forms.WelcomeForm;
/**
* @version 1.0
* @author
*/
public class WelcomeAction extends DispatchAction {
public ActionForward welcomeMethod(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); WelcomeForm wForm=(WelcomeForm)form;
forward = mapping.findForward("welcomeForward");
return (forward);
}
public ActionForward helloUserMethod(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward();
forward = mapping.findForward("helloUserForward");
return (forward);
}
}
Here we are extending DispatchAction instead of Action class. So that we can write our own methods (other than execute) and call appropriate method from jsp based on the action(button clicks).
WelcomeMethod is same as previous one
helloUserMethod is called when user click on submit button from welcome.jsp page and forwards it to helloUser.jsp
Create WelcomeForm.java
package com..strutsproject.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 1 field on this form:
*
* user - [your comment here]
*
* @version 1.0
* @author
*/
public class WelcomeForm extends ActionForm {
private String user = null;
public String getUser() {
return user;
}
public void setUser(String u) {
this.user = u;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
user = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
// if ((field == null) || (field.length() == 0)) {
// errors.add("field", new org.apache.struts.action.ActionError("error.field.required"));
// }
return errors;
}
}
In the validate method we can validate the name entered on the screen
We can return errors to same page by giving input=”definition name” in struts config
Create tile defintions file under WEB-INF.
This file contains tiles with tile associated with parameters and identified by a name which we can use in jsps or directly give them in struts config as action forwards instead of jsp urls.
The tiles definition looks like this( tiles-definitions.xml)
Basic.Def is basic tile definition which we can extend to other definitions and override the body.jsp or header.jsps. if we extend this definition the new definition also gets the same layout and offcourse we can add some more tiles like menu.jsp or change the body content to different jsp other than body.jsp
Welcome.user and hello.user are two examples of tiles definitions which are extending the basic.Def. welcome.user is definition for welcome.jsp and hello.user is definition for hellUser.jsp
Changes in struts-config.xml
1) The action /welcome now has two forwards declared and pointing to appropriate tiles
2) Declare the controller as tilesRequestProcessor
3) Insert the tiles plugin.
4) define the form bean and connect the form with action class
5) the parameter=”dispatcher” is used to dispatch request to different methods in action class based on the dispatcher value given on jsp.
Changes to web.xml
definitions-config
/WEB-INF/tiles-definitions.xml
Define the parameter defintions-config ( we can even change the file name tiles-definitions.xml to another but that should be same as what we are giving here in web.xml)
Restart the server and run on the server
The out put looks like this
[pic]
[pic]
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- corba example university of north florida
- lecture 0 course introduction tcd
- introduction
- reasons for studying concepts of programming languages
- cop2210 programming i
- 1 creating a form
- first struts program using wsad 5
- tutorial for simulation based performance analysis of ad
- java lang package package in java java language
Related searches
- first time homebuyers program canada
- first time home buyers program canada
- first time auto loan program credit union
- first time homebuyers program virginia
- using viagra first time
- the hope program for first time homebuyers
- simple java program using array
- first time home buyer program 2008 repayment
- first home buyer program canada
- first time homebuyers program florida
- first time home buyer program 2020 florida
- first time home buyers program 2020