Struts 2 Data tages

1. url tag:- The url tag is a tag responsible for generating URL strings using struts tag lib. The advantage of this is that you can supply parameters to the tag. Let take an example to show the usage of url tag.

 

Example usage is:-

Create the dynamic web project from new project in elipse and then create the following files:-

 

Step 1:- Create WelcomeAction.java

 

package r4r;

import com.opensymphony.xwork2.ActionSupport;

public class WelcomeAction extends ActionSupport{

private static final long serialVersionUID = 1L;

private String name;

public static long getSerialversionuid() {

return serialVersionUID;

}

public String execute() throws Exception {

return "success";

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

 

Step 2:- Create view welcome.jsp

 

<%@ page contentType="text/html; charset=UTF-8"%>

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

<html>

<head>

<title>Hello World</title>

</head>

<body>

<s:url id="login" action="login" var="myurl">

<s:param name="name"></s:param>

</s:url>

<a href='<s:property value="#myurl"/>'>

<s:property value="#myurl"/></a>

</body>

</html>

 

Step 3:- Create 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">

<display-name>Login</display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<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>

</web-app>

 

Step 4:- Create struts.xml

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="r4r" extends="struts-default" namespace="/">

<action name="Student"

class="r4r.Student"

method="execute">

<result name="success">/student.jsp</result>

</action>

</package>

</struts>

 

you can download the project from here