Struts 2 Example

HelloUser.java

Here is the action class, logic is implemented here

package net.javatips;

public class HelloUser {

  
private String message;
  
private String name;

  
public String execute() {
     
setMessage("Hello " + getName());
     
return "result";
  
}

  
public String getMessage() {
     
return message;
  
}

  
public void setMessage(String message) {
     
this.message = message;
  
}

  
public String getName() {
     
return name;
  
}

  
public void setName(String name) {
     
this.name = name;
  
}

}

struts.xml

Following is the struts 2 configuration, which will used for naviagating different page views

The name of this configuration file should be "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="default" extends="struts-default">
		<action name="HelloUser" class="net.javatips.HelloUser">
			<result name="result">/success.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="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>
Run

Struts 2 Example

Struts 2 Example





Previous 1 2








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