Struts

Welcome Program using Struts2.0 Framework
Previous Home Next

 

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?

 
To do so just copy and past following code into your workspace by using any IDEs (MyEclipse ,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.

Directory Structure of Welcome Example in Struts 2.0 Using MyEclipse IDE



index.jsp

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

<s:form action="hello">
<s:textfield name="name" label="Name"/>
<s:submit value="Submit"/>
</s:form>

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>

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 org.r4r;
public class MyAction {
	private String name,password;
	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