Struts

Welcome Program using Struts2.0 Framework
adplus-dvertising
Previous Home Next

Introduction:

This is basic examples of how you can make a struts2 application. Here we are going to make only View using JSP Struts2 Tag Library files. The purpose of this topic is taught you how you can make, run and debug struts2 application?

Class/Library File Descriptions

  • You can download "r4r-welcome-program-using-struts2.zip" from our website.
  • To do so just copy and past following URL into address bar of your browser http://r4r.co.in/java/struts/basic/tutorial/r4r-welcome-program-using-struts2.zip and then change its extension to .war then import it into your workspace by using any IDEs(eclipse or Netbaens).
  • If you are not using IDEs .Then extract it and save it into root folder into tomcat webapp or you can deploy it on tomcat. Then you can see .Here you can see two xml files (web.xml and struts.xml ) , one jsp (index.jsp)file,lib folder which includes all required jars. The source code for these files are given below you can explorer these sours code into your workspace.
  • To run our struts2 application start web server and type following on browser http://localhost:8080/r4r-welcome-program-using-struts/index.jsp Then you will see the output on your browser.

Source Code:

web.xml
?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <filter>
  <filter-name>f1</filter-name>
  <filter-class>org.apache.struts2.dispatcher
  .FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>f1</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="hello">
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>
struts.xml
!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation
//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="hello" class="mypack.MyAction">
<result name="success">/hello.jsp</result>
</action>
</package></struts>
MyAction.java
package mypack;
public class MyAction {
	private String name;
	public String execute(){
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}	
}
hello.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
 <b>Welcome, <s:property value="name"/></b>
Output:
Previous Home Next