2

I want to know if anyone here knows how to get an array from a controller using Struts?

showTeams.jsp

<%@page import="java.util.ArrayList"%>
<%@page import="nl.***.controller.TeamController"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@include file="includes/layout.jsp" %>
<div class="col-lg-12">
    <h1>Alle Teams</h1>
    <table>
        <caption><h1>List of Teams</h1></caption>
        <th>ID Team</th>
        <th>Naam Team</th>
        <tr>
            <c:forEach var="team" items="${teams}">
                <td>${team.getId}<td>
                <td>${team.getName()}<td>
            </c:forEach>
        </tr>
    </table>
</div>
<a href="index.jsp">Back to Start</a>
<%@include file="includes/footer.jsp" %>

TeamController.java

package nl.***.controller;

import java.util.ArrayList;
import nl.***.datalayer.DAOTeam;
import nl.***.models.Team;
import com.opensymphony.xwork2.ActionSupport;
/**
 *
 * @author 
 */
public class TeamController extends ActionSupport{
    private String name;
    ArrayList<Team> teams = new ArrayList<Team>();

    public String execute() throws Exception{
        return "createTeam";
    }

    public String getName() {
        return name;
    }

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

    public String addTeamToDB(String name){
        try{
            Team t = new Team(name);
            DAOTeam.getInstance().createTeam(t);
            return "<h1>Team " + name + " is toegevoegd aan de Database</h1>";
        }catch(Exception e){
        return "<h1>FAILED with message" + e.getMessage() + "</h1>"; 
        }
    }

    public String getAllTeams(){
        teams = DAOTeam.getInstance().loadTeams();
        System.out.println("ik ben er!!@#!EWDSRETGDFQTWTRE"); //for testing, removed when done
        //mapping.findForward("showTeams");
        return "getallteams";
    }
}

struts.xml

<struts>
<constant name="struts.devMode" value="true" /> 
    <package name="teamcreate" extends="struts-default">
      <action name="createTeam" 
            class="controller.TeamController" 
            method="execute">
            <result name="createTeam">/view/prossesTeam.jsp</result>
      </action>
    </package>
    <package name="teamLoad" extends="struts-default">
          <action name="showTeams" 
            class="controller.TeamController" 
            method="getallteams">
            <result name="getallteams">/view/showTeams.jsp</result>

      </action>
    </package>
</struts>

When I load the page it does not have the array 'teams' defined in the controller. I've tried changing values in struts.xml but still nothing. If tried to make an if statement in the execute function in TeamController to make sure it even ran but I got nothing I don't know what I'm doing wrong.

EDIT: What I essentially want is that when you go to showTeams.jsp, it should show you a table with all the teams using Struts.

1
  • You can use struts iterator tag too. Commented Jun 15, 2016 at 13:57

2 Answers 2

1

The view should be returned from the controller with the result getallteams. So you should call showTeams action to load teams into the action class variable.

private List<Team> teams = new ArrayList<Team>();

public List<Team> getTeams() { return teams; };

The last method is required to evaluate teams in the forEach tag

<c:forEach var="team" items="${teams}">
    <td>${team.id}<td>
    <td>${team.name}<td>
</c:forEach> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I indeed forgot to make an getTeams() function in the controller it works now.
0

I didn't do any struts for the last 5 years, so I'm not sure about your controller, but if you copy/paste your code of the JSP, then you have some syntax problem.

<td>${team.getId}<td>
<td>${team.getName()}<td>

getId / getName() => one with brackets, one without ? And you should not use the name of the getter but the property. Like :

<td>${team.id}<td>
<td>${team.name}<td>

If the syntax is correct, you should at least get an empty array (just the headers).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.