javajspstruts2actionresultxml-configuration

Display data from database with the property tag in Struts 2


When I want to display data from database using Struts 2 tags:

I have a problem in my JSP, it doesn't show anything. Although in Console I have the result

My Action class:

public class ObjectifAction extends ActionSupport implements ModelDriven<Objectif> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
        
    Objectif objectif = new Objectif();
    List<Objectif> objectifs = new ArrayList<Objectif>(); 
    ObjectifDAO objdao= new ObjectifDAO();
    
    //objdao.countTotal();
    
    public String afficher()
    {
        int i=0,taille;
       objectifs = objdao.afficher();
       taille=objdao.countTotal();
       for(i=0;i<taille;i++){
           
           System.out.println(objectifs.get(i).getDescription());
           
       }
       return "success";
    }
        
    public Objectif getObjectif() {
        return objectif;
    }

    public void setObjectif(Objectif objectif) {
        this.objectif = objectif;
    }

    public List<Objectif> getObjectifs() {
        return objectifs;
    }

    public void setObjectifs(List<Objectif> objectifs) {
        this.objectifs = objectifs;
    }

    public ObjectifDAO getObjdao() {
        return objdao;
    }

    public void setObjdao(ObjectifDAO objdao) {
        this.objdao = objdao;
    }

    @Override
    public Objectif getModel() {
        // TODO Auto-generated method stub
        return objectif;
    }
    
}

My JSP page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<table>
<tr>
<td>id</td>

</tr>
<s:iterator value="objectifs" var="o">  
<tr>
<td><s:property value="#o.id_objectif_metier"/></td>
       
</tr>
</s:iterator>   
</table>

</body>
</html>

It doesn't display anything, please help!

This is my struts.xml file:

I think there is a mistake in the JSP page. It only shows the name of the column without data

<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />

<!-- <constant name="struts.action.extension" value="xhtml,,xml,json,action"/> -->

<package name="rest" extends="rest-default" namespace="/rest">
</package>
<package name="default" extends="struts-default, json-default"
namespace="/">

<interceptors>
    <interceptor class="ma.interceptors.LoginInterceptor"
        name="loginInterceptor">
    </interceptor>
    <interceptor-stack name="loginStack">
        <interceptor-ref name="loginInterceptor" />
        <interceptor-ref name="defaultStack">
            <param name="exception.logEnabled">true</param>
            <param name="exception.logLevel">ERROR</param>
        </interceptor-ref>
    </interceptor-stack>
</interceptors>

<global-results>
    <result name="login">index.jsp</result>
    <result name="error">errors/error.jsp</result>
</global-results>

<action name="Connect"  class="ma.actions.UtilisateurAction"  method="Connect">
    <result name="ADMIN" type="redirect">admin</result>

    <result name="input" type="redirect">index.jsp</result>
</action>

<action name="admin">
    <interceptor-ref name="loginStack" />
    <result>/WEB-INF/admin/index.jsp</result>
</action>

<action name="listerObjectif" class="ma.actions.ObjectifAction" method="afficher">
    <interceptor-ref name="loginStack" />
    <result name="success" type="redirect">objectif</result>
</action>

<action name="objectif">
    <interceptor-ref name="loginStack" />
    <result>/WEB-INF/admin/objectif/objectif.jsp</result>
</action>

</package>

I don't think there is a mistake here?


Solution

  • The Struts is used to execute an action then return a JSP with the result. But redirect result type is used if you want to redirect to another URL. Doing this you loose everything used in JSP to populate data in your controls. Change

    <action name="listerObjectif" class="ma.actions.ObjectifAction" method="afficher">
        <interceptor-ref name="loginStack" />
        <result>/WEB-INF/admin/objectif/objectif.jsp</result>
    </action>