Starter Tutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.
Home » Programming » Advanced Java » Struts » Program to implement java struts application
Suryateja Pericherla Categories: Struts. No Comments on Program to implement java struts application
Join Our Newsletter - Tips, Contests and Other Updates
Email
Name

Aim

Write a program to implement struts application.

 

Theory

Struts 2 Framework

The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc.

 

The important features of struts 2 framework are as follows:

  • Configurable MVC components
  • POJO based actions
  • AJAX support
  • Integration support
  • Various Result Types
  • Various Tag support
  • Theme and Template support

 

Program

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">    
<struts>
	<package name="default" namespace="/" extends="struts-default">
		<action name="getMessage" class="MyAction">
			<result name="success">/success.jsp</result>
			<result name="failure">/error.jsp</result>
		</action>
	
	</package>
</struts>

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" id="WebApp_ID" version="5.0">
  <display-name>StrutsDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

success.jsp

<html>
<head>
<title>Welcome</title>
</head>
<body>
	<p>Welcome to Sturts 2 Framework!</p>
</body>
</html>

 

error.jsp

<html>
<head>
<title>Error</title>
</head>
<body>
	<p>Error occurred...</p>
</body>
</html>

 

MyAction.java

public class MyAction {
	public String execute() {
		System.out.println("Hello Struts!");
		return "success";
	}
}

 

Input and Output

Implement struts application

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
Pinterest
Youtube
Instagram
Blogarama - Blog Directory