views:

1175

answers:

3

Hello, Im having an issue getting json working with the struts-jquery-plugin-2.1.0

I have included the struts2-json-plugin-2.1.8.1 in my classpath as well. Im sure that I have my struts-jquery-plugin configured correctly because the grid loads, but doesnt load the data its supposed to get from the action class that has been json'ized.

The documentation with the json plugin and the struts-jquery plugin leaves ALOT of gaps that I cant even find with examples/tutorials, so I come to the community at stackoverflow.

My action class has a property called gridModel thats a List with a basic POJO called Customer. Customer is a pojo with one property, id. I have a factory that supplies the populated List to the actions List property which i mentioned called gridModel.

Heres how i set up my struts.xml file:

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

<package name="org.webhop.ywdc" namespace="/" extends="struts-default,json-default">

<result-types>
    <result-type name="json" class="com.googlecode.jsonplugin.JSONResult">

    </result-type>
</result-types>
    <action name="login" class="org.webhop.ywdc.LoginAction" >

            <result type="json"></result>
            <result name="success" type="dispatcher">/pages/uiTags/Success.jsp</result>
           <result name="error" type="redirect">/pages/uiTags/Login.jsp</result>
            <interceptor-ref name="cookie">
                <param name="cookiesName">JSESSIONID</param>
            </interceptor-ref>
    </action>
    <action name="logout" class="org.webhop.ywdc.LogoutAction" >

           <result name="success" type="redirect">/pages/uiTags/Login.jsp</result>
    </action>
</package>

In the struts.xml file i set the and in my action i listed in the action configuration.

Heres my jsp page that the action loads:

<%@ taglib prefix="s" uri="/struts-tags" %> <%@ taglib prefix="sj" uri="/struts-jquery-tags"%> <%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>

<%@ page language="java" contentType="text/html" import="java.util.*"%>

Welcome, you have logged in!

<s:url id="remoteurl" action="login"/> 

    <sjg:grid 
    id="gridtable" 
    caption="Customer Examples" 
    dataType="json" 
    href="%{remoteurl}" 
    pager="false" 
    gridModel="gridModel"
>
    <sjg:gridColumn name="id" key="true" index="id" title="ID" formatter="integer" sortable="false"/>

</sjg:grid>

 Welcome, you have logged in. <br />
<b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>

  <h2>Password:<s:property value="password"/></h2>

  <h2>userId:<s:property value="userId"/></h2>
  <br />
  <a href="<%= request.getContextPath() %>/logout.action">Logout</a><br /><br />
 ID: <s:property value="id"/>
 session id: <s:property value="JSESSIONID"/>

</body>

Im not really sure how to tell what json the json plugin is creating from the action class. If i did know how i could tell if it wasnt formed properly. As far as I know if I specificy in my action configuration in struts.xml, that the grid, which is set to read json and knows to look for "gridModel" will then automatically load the json to the grid, but its not.

Heres my action class:

public class LoginAction extends ActionSupport {

  public String JSESSIONID;
  public int id;
  private String userId;
  private String password;
  public Members member;
  public List<Customer> gridModel;

  public String execute()
  {
            Cookie cookie = new Cookie("ywdcsid", password);
            cookie.setMaxAge(3600);
            HttpServletResponse response = ServletActionContext.getResponse();
            response.addCookie(cookie);

            HttpServletRequest request = ServletActionContext.getRequest();
            Cookie[] ckey = request.getCookies();
            for(Cookie c: ckey)
            {
                System.out.println(c.getName() + "/cookie_name + " + c.getValue() + "/cookie_value");
            }
            Map requestParameters = ActionContext.getContext().getParameters();//getParameters();
            String[] testString = (String[])requestParameters.get("password");
            String passwordString = testString[0];
            String[] usernameArray = (String[])requestParameters.get("userId");
            String usernameString = usernameArray[0];

            Injector injector = Guice.createInjector(new GuiceModule());
            HibernateConnection connection = injector.getInstance(HibernateConnection.class);
            AuthenticationServices currentService = injector.getInstance(AuthenticationServices.class);
            currentService.setConnection(connection);
            currentService.setInjector(injector);
            member = currentService.getMemberByUsernamePassword(usernameString, passwordString);
            userId = member.getUsername();
            password = member.getPassword();

            CustomerFactory customerFactory = new CustomerFactory();
            gridModel = customerFactory.getCustomers();

            if(member == null)
            {
                return ERROR;
            }
            else
            {
                id = member.getId();
                Map session = ActionContext.getContext().getSession();
                session.put(usernameString, member);
                return SUCCESS;
            }   
  }

    public String logout() throws Exception 
    {
        Map session = ActionContext.getContext().getSession();
        session.remove("logged-in");
        return SUCCESS;
    }
    public List<Customer> getGridModel()
    {
        return gridModel;
    }
    public void setGridModel(List<Customer> gridModel)
    {
        this.gridModel = gridModel;
    }
    public String getPassword() 
    {
        return password;
    }

    public void setPassword(String password) 
    {
        this.password = password;
    }

    public String getUserId() 
    {
        return userId;
    }

    public void setUserId(String userId) 
    {
        this.userId = userId;
    }
    public String getJSESSIONID() {
        return JSESSIONID;
    }

    public void setJSESSIONID(String jsessionid) {
        JSESSIONID = jsessionid;
    }

}

Please help me with this problem. You will make my week, as this is a major bottleneck for me :(

thanks so much,

thebravedave

A: 

I am having the same issue. I test my Action class seperately and it returns valid JSON, but on my JSP, the grid widget renders without any data.

Did you find a solution?

LEN
yes. I posted my solution right above.
thebravedave
A: 

It turns out I was not setting my struts.xml config file correctly. I put a json return in my same action class that was used to return non json data. Dont do this. Create another action class that specifically just returns the json data Heres my struts.xml [code]

<package name="org.webhop.ywdc" namespace="/" extends="struts-default">


    <action name="login" class="org.webhop.ywdc.LoginAction">
            <interceptor-ref name="params"/>
            <interceptor-ref name="cookie">
                <param name="userId" />
            </interceptor-ref>

            <result name="success" type="dispatcher">/pages/Success.jsp</result>
            <result name="error" type="dispatcher">/pages/Login.jsp</result>
    </action>

    <action name="logout" class="org.webhop.ywdc.LogoutAction" >
           <result name="success" type="dispatcher">/pages/Login.jsp</result>
    </action>
    <action name="editcellentry" class="org.webhop.ywdc.EditCellEntryAction">
         <result name="success" type="dispatcher">/pages/Success.jsp</result>
    </action>
</package>

<package name="example" namespace="/" extends="json-default">
    <result-types>
        <result-type name="json" class="com.googlecode.jsonplugin.JSONResult"/>
    </result-types>
    <action name="jsonaction" class="example.JsonAction">
        <result type="json" />  
    </action>
</package>

[/code] You will notice i even put it in a new package that specifically extends json-default. I also ran into the problem that initially i set my json root to be the List collection that i set up to be my collection to be serialized as json data. If you dont do this it will serialized every collection with getters an setters for you. If you set the collection as the root for json then it wont return the name of the collection with the json data, and you need to set that in the gridModel parameter of the grid configuration in your jsp page that the grid is on. use firebug to return your json and make sure that the name of the json returned matches this value you defined in gridModel.

thebravedave
A: 

Hi,

My concerned is what will be the action name of your jsp after separating your struts.xml.

Is it "" or

""

Thanks,

Czar