Hello, Im having problems getting struts2-json-plugin 2.1.8.1 to
work with struts-jquery-plugin.
Im trying to get struts-jquery grid to load json data that struts2-json plugin forms from a List/object property in that action class.
my action class has to load a "success" result. The success result loads a
jsp page that has the struts-jquery grid. that grid should get
its json data from the action class that called the result. Instead, my grid is showing up with no data in it (no json data was loaded).
Im not sure if im just configuring this wrong, or what.
I have the struts2-json-plugin installed and added to my classpath. Same for the struts2-jquery-plugin.
Heres my struts.xml
**********************************************************************************
<
struts>
<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">
<param name="root">gridModel</param>
</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>
</struts>
***************************************************************************
Under package I have it extend struts-default and json-default. I do this because I need both i believe.
under result types i put <result-type name="json" class="com.googlecode.jsonplugin.JSONResult">, which lets struts know what to do with a json result in your action config.
Under action I have 3 different result types, one for success, one for error, and one for json.
The examples I saw online were simplified ones where they didnt have the result mapped to a certain page.
I find this very confusing, because if your json data has to be read from a certain jsp page (like my "success" result type), then how will struts know which page to load after
the action complets/json serialized?
<result type="json">
<param name="root">gridModel</param>
</result>
As far as I can tell, this result type="json" should cause the json plugin to, for this action class, find any paramter with the name gridModel, and json-ize it.
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;
}
}
**********************************************************************************
From reading online, all i need to do is have my list property i want json-ized and have a public accessor for it (get, set), which i do.
My action class has a success return which will thru the struts.xml file, open up Success.jsp, which will then load and when the
grid for success.jsp is loaded, it should find that json data and load it to the grid.
My "Success.jsp" page contains a grid from the new struts-jquery-plugin.
***************************************************************************************
<%@ 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.*"%>
<jsp:include page="/pages/uiTags/CheckLogin.jsp" />
<html>
<head>
<sj:head jqueryui="true" jquerytheme="redmond" />
<title>Welcome, you have logged in!</title>
</head>
<body>
<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.
Session Time: <%=new Date(session.getLastAccessedTime())%>
<h2>Password:<s:property value="password"/></h2>
<h2>userId:<s:property value="userId"/></h2>
<a href="<%= request.getContextPath() %>/logout.action">Logout
ID: <s:property value="id"/>
session id: <s:property value="JSESSIONID"/>
</body>
</html>
******************************************************************************************
Here my struts-jquery grid is set up to read the json data from the action class (at least i think so).
My grid shows up, but with no data loaded.
Im not even sure if the struts2-json plugin is actually working. None of the examples were set up like this, where the packages extension
needed to be struts-default as well as json-default.
Im confused about the
<result type="json">
<param name="root">gridModel</param>
</result>
that i mapped in the action class. I read online someone saying that if your action returned a "SUCCESS" then you dont need it to map to json data.
Well, thats not the case. I need my action to open up "success", and also have that page read the json data from the action class that is supposed to have
had a property (gridModel) json-ized.
Can someone explain how to do both of these together? Also, how to see the json data that the plugin json-ized from the action property collection (list<customer>),
just to make sure that Im getting something.
Please help me, you will be my super hero.
Sincerely,
thebravedave
ps: im sure that my action class's List<Customer> (the data that is supposed to be json-ized in the end) is loaded with Customer objects. So, json plugin shouldnt have a problem with a null value.