• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Struts 2 – Configuration

January 4, 2012 By j2eereference Leave a Comment

Let’s understand the Struts 2 configuration with a simple example. We can have a form which has a text field to accept your name and on submit it should forward to a display page showing hello {YourName}.

Let’s have the following files for our example.

1)      web.xml

2)      struts.xml

3)      Helloworld.java

4)      input.jsp

5)      display.jsp

Now let’s see all the above components in details

Web.xml

The filter and the filter-mapping elements are used to setup the Struts 2 FilterDispatcher.

The filter is mapped to the URL pattern “/*“. This means all the incoming request that targets to the Struts 2 action will be handled by FilterDispatcher class.

1
2
3
4
5
6
7
8
9
<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>

struts.xml

This is the struts configuration XML file. The struts.xml file contains the following action mapping.

1
2
3
4
5
6
7
<struts>
<package name="default" extends="struts-default">
  <action name="HelloWorld">
   <result name="SUCCESS">/display.jsp</result>
  </action>
</package>
</struts>

input.jsp

Below is the input.jsp code which will take the input from the user.

For using the struts tags in the jsp page “struts-tags” taglib directive should be included.

1
<%@taglib uri="/struts-tags" prefix="s" %>

1
2
3
4
5
6
7
8
9
10
11
<html>
<head>
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld" >
<s:textfield name="userName" label="User Name" />
<s:submit />
</s:form>
</body>
</html>

HelloWorld.java

Now let’s see the Helloworld.java which will take care of the request from the client. This class is very simple and it contains two properties one for the user name and the other for displaying the message.

 

1
public class HelloWorld {

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
private String message;
private String userName;
public HelloWorld() {
}
 
public String execute() {
setMessage("Hello " + getUserName());
return "SUCCESS";
}
 
public String getMessage() {
return message;
}
 
public void setMessage(String message) {
this.message = message;
}
 
public String getUserName() {
return userName;
}
 
public void setUserName(String userName) {
this.userName = userName;
}
 
}

You can notice the following in the HelloWorld class

1) In the execute() method of the HelloWorld action we define the message to be displayed.

2) We need not have a separate form bean like struts 1 to access the form data. We can have a simple java class as action.

3) The action need not extend any class or implement any interface.

4) We need to have an execute() method which returns a String and has a public scope.

display.jsp

In the display page we display the “Hello User” message using the property tag.

1
2
3
4
5
6
7
8
9
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>First struts 2 application</title>
</head>
<body>
<h1><s:property value="message" /></h1>
</body>
</html>

Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.