Display (on JSP) an Object (From the Model) with Struts Tags

Display (on JSP) an Object (From the Model) with Struts Tags

Previous Home Next

 

Struts provide tags to display model’s objects on JSP page. In this section we will cover it. Action is end point for Struts based application .Action interacts with model after completions of calculations and manipulations Action class will returns a key. On the behalf of this key some control flow operations.

 There are following steps to display an object on view: 

Reconfigure struts-config.xml 

Add an attribute (i.e. attribute="user") in tag that is used to map the ActionForm for into scope (request/session) 

InfoData.java

package com.r4r;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class InfoData extends Action
{
  String username;
  public ActionForward execute(ActionMapping mapping,ActionForm form,
  HttpServletRequest request,HttpServletResponse response) throws Exception{

    return mapping.findForward("success");
  }
  public String getUsername() {
  return username;
  }
  public void setUsername(String username) {
  this.username = username;
  }
}

Here the attribute maps the Action-Form into a scope (The scope is by default session) under "user". Now ActionForm is set in session scope.So we can display the properties of ActionForm on JSP View . 

On JSP View: Import the bean tag library into the JSP as follows: <%@ taglib uri="/tags/struts-bean" prefix="bean"%> Use the tag to output properties of the ActionForm.

success.jsp

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html>
<head>
<title> World</title>
</head>
<body bgcolor="green">
<h1>You are successfully Login</h1>
<h1>welcome User,
<bean:write name="LoginForm" property="username"/>    
</h1>
</body>
</html>

Output



Previous Home Next