How to map json payload to model bean in struts2 - struts2

Is there any way to map json payload to Model bean.? If possible, please provide me an example.
Following are the classes I am using.
package com.sample;
import java.io.Serializable;
public class Employee implements Serializable{
private String firstName;
private String lastName;
private int id;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
#Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Employee [firstName=").append(firstName)
.append(", lastName=").append(lastName).append(", id=")
.append(id).append("]");
return builder.toString();
}
}
Following is my action class.
package com.sample.controller;
import com.opensymphony.xwork2.ModelDriven;
import com.sample.Employee;
public class EmployeeController implements ModelDriven<Employee> {
private String name = "Hari krishna";
Employee emp = new Employee();
public String addEmployee() {
System.out.println(emp);
return "success";
}
#Override
public Employee getModel() {
return emp;
}
}
Following is my struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="json-default">
<interceptors>
<interceptor-stack name="jsonStack">
<interceptor-ref name="json">
<param name="enableSMD">true</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="addEmployee" class = "com.sample.controller.EmployeeController" method = "addEmployee">
<interceptor-ref name="jsonStack"></interceptor-ref>
<result type="json" />
</action>
</package>
</struts>
When I call the action "addEmployee", with json data "{"firstName":"Hari","id":123,"lastName":"assds"}" I am getting following response. I set content type to text/json.
{
"model": {
"firstName": null
"id": 0
"lastName": null
}-
}
I am posting data using Advanced Rest Client.

when you send the json data from client to server, send them as string and in action class create a normal String variable to receive and hold the json's string.
Then, you can manually populate your bean from string using json parser,
or use a lib like google gson that has a build in features for this purposes.
Google Gson is a Java serialization/deserialization library that can convert Java Objects into JSON and back.
You can create your own struts Interceptor to wrap this process or implements it directly inside your action class.

Related

displaytag not working with struts 2 use tiles

In my application using struts 2 framework and tiles.
Now, I want to use displaytag to display the data in a database table. I did the following:
in my struts.xml:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>
In file UserAction:
public class UserAction extends ActionSupport{
private List<Users> listUsers;
private String myActionName;
public String getMyActionName() {
return myActionName;
}
public void setMyActionName(String myActionName) {
this.myActionName = myActionName;
}
public List<Users> getListUsers() {
return listUsers;
}
public void setListUsers(List<Users> listUsers) {
this.listUsers = listUsers;
}
#Override
public String execute() throws Exception {
listUsers = UserServices.selectAll();
return NONE;
}
}
And In file user_admin.jsp(file use in tiles):
<s:actionerror cssStyle="color:red"/>
<div class="well">
<display:table name="listUsers" id="listUsers" requestURI="/UserAction" cellpadding="5px;"
cellspacing="5px;" style="margin-left:50px;margin-top:20px;">
<display:column property="EMAIL" title="Email"/>
<display:column property="NAME" title="Name"/>
</display:table>
</div>
File user model:
package com.model;
public class Users {
private String EMAIL;
private String NAME;
public String getEMAIL() {
return EMAIL;
}
public void setEMAIL(String EMAIL) {
this.EMAIL = EMAIL == null ? null : EMAIL.trim();
}
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME = NAME == null ? null : NAME.trim();
}
}
When I run the application error occurs in apache tomcat log
org.apache.catalina.core.ApplicationDispatcher.invoke Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: org.apache.commons.collections.IteratorUtils
Who can help me point out why the error, in the solution to overcome it?
Add commons-collections-3.1.jar to your libraries.
Note that you are using only one Interceptor, unless checkSession is the name of your stack.
EDIT
Interceptor checkSession to check my log on the website. How should I use.
You can define a custom interceptor stack, and then reference it, or include your custom interceptor AND another stack (for example, the default one) for a single action, like this:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<interceptor-ref name="defaultStack" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>

Struts 2.0 error

I am developing a simple HelloWorld struts application. I am doing the configuration using struts.xml file. the welcome page is displayed but when I click on the form submit button, it gives 404 error.
Kindly provide some pointers.
action class:
package client;
public class Hello {
public String name;
public String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
message = "Hello " + name;
return "success";
}
}
namecollect.jsp
<s:form action="hello" method="post">
<s:textfield name="name">Enter name</s:textfield>
<s:submit></s:submit>
</s:form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="client.Hello">
<result name="success">hello.jsp</result>
</action>
</package>
This is the package structure:
Your config file is in the wrong place.
By default struts.xml is expected to be at the root of your classpath.

Prepopulating form's textfields with bean default value in Struts2

In Struts 1.x , I have prepopulated the forms text fields using form bean's with default values as follows,
<html:form action="/actions/signup1">
First name: <html:text property="firstName"/><BR>
And in form bean, I have default value as follows...
public class ContactFormBean {
private String firstName = "First name";
But in Struts 2.x , when I tried with struts-tags textfield as follows, its not prepopulating the default value from the bean,
<s:form action="signup1">
<s:textfield name="formBean.firstName" label = "First Name" />
I have the formBean declared in my Action class as follows with appropriate getter and setter methods...
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
#Override
public String execute() throws Exception {
....
}
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
Please let me know if this can be accomplished at Request level and not at session level.
Thanks in advance.
<--Edited-->
struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/index.jsp</result>
</action>
<action name="signup">
<result>/SignUp.jsp</result>
</action>
<action name="signup1" class="coreservlets.action.SignupAction1" method="execute">
<result name="success">/SignUp-Confirmation.jsp</result>
<result name="error">/SignUp.jsp</result>
</action>
</package>
</struts>
SignUp.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Sign UP</title>
</head>
<body>
<H1 ALIGN="CENTER">Sign Up</H1>
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:textfield name="formBean.lastName" label = "Last Name" />
<s:textfield name="formBean.email" label = "Email Address" />
<s:textfield name="formBean.faxNumber" label = "Fax Number" />
<s:submit action="signup1" method="loginAfterSubmit" value="Click here to Submit"/>
</s:form>
</body>
</html>
ContactFormBean.java
public class ContactFormBean {
private String firstName = "First name";
private String lastName = "Last name";
private String email = "user#host";
private String faxNumber = "xxx-yyy-zzzz";
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return(lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return(email);
}
public void setEmail(String email) {
this.email = email;
}
public String getFaxNumber() {
return(faxNumber);
}
public void setFaxNumber(String faxNumber) {
this.faxNumber = faxNumber;
}
public boolean isMissing(String value) {
if ((value == null) || (value.trim().equals(""))) {
return(true);
} else {
for(int i=0; i<defaultValues.length; i++) {
if (value.equals(defaultValues[i])) {
return(true);
}
}
return(false);
}
}
}
SignupAction1.java
public class SignupAction1 extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private ContactFormBean formBean;
#Override
public String execute() throws Exception {
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() {
String firstName = formBean.getFirstName();
String lastName = formBean.getLastName();
String email = formBean.getEmail();
String faxNumber = formBean.getFaxNumber();
if (formBean.isMissing(firstName)) {
return ERROR;
} else if (formBean.isMissing(lastName)) {
return ERROR;
} else if ((formBean.isMissing(email)) ||
(email.indexOf("#") == -1)) {
return ERROR;
} else if (formBean.isMissing(faxNumber)) {
return ERROR;
} else {
return SUCCESS;
}
}
public ContactFormBean getFormBean(){
return this.formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
}
Change your signup action declaration to
<action name="signup" class="coreservlets.action.SignupAction1">
<result>/SignUp.jsp</result>
</action>
and signup1 to
<action name="signup1" class="coreservlets.action.SignupAction1" method="signup1">
create method signup1 in your SignupAction1 action class and move your logic from execute to it. In execute then create new instance of your ContactFormBean.
put your default value to the constructor:
public class ContactFormBean {
private String firstname;
public ContactFormBean (){
this.firstName = "First name";
}
}
You are returning a null formBean object with the getFormBean method,
then the constructor is never called and the attempt to acces the formBean attribute firstName is giving an error (that is not showed because it is wrapped by the Struts2 tag on the JSP.
You can
1) instantiate it on your execute method:
public String execute() throws Exception {
this.formBean = new ContactFormBean();
}
2) instantiate it lazily on your getter:
public ContactFormBean getFormBean(){
if (formBean==null)
this.formBean = new ContactFormBean();
return this.formBean;
}
EDIT (due to your comments):
I don't know how you've structured your web application;
But if you are on a JSP, an Action (and its execute() method, or another method if specified) was called BEFORE rendering the JSP.
So, regardless if you have ActionOne loading stuff and ActionTwo called after submit, or ActionOne loading stuff and another method of ActionOne called after submit, you can know if you are in a "pre-JSP" state or in a "post-submit" state...
That said, if you are exposing an Object, and you want its value or its attributes to be different from null, you have to instantiate it in one of the way described above.
Obviously, your object should contain getters and setters for its attributes, and they must have been bound to your JSP objects.
In your example, you could have:
ContactFormBean:
public class ContactFormBean {
private String firstName = "First name";
public String getFirstName(){
return this.firstName;
}
public String setFirstName(String firstName){
this.firstName = firstName;
}
}
Your Action:
public class SignupAction1 extends ActionSupport {
private ContactFormBean formBean;
public ContactFormBean getFormBean(){
return formBean;
}
public void setFormBean(ContactFormBean fb){
formBean = fb;
}
#Override
public String execute() throws Exception {
/* this method is executed before showing the JSP */
/* first time initialization */
this.formBean = new ContactFormBean();
return SUCCESS;
}
public String loginAfterSubmit() throws Exception {
/* this method is executed after the submit button was pressed */
/* i don't initialize nothing here,
ContactFormBean is coming from the page */
System.out.println("ContactFormBean's firstName value is: " +
this.formBean.getFirstName());
return "goSomewhereElse";
}
}
and in Your JSP:
<s:form>
<s:textfield name="formBean.firstName" label = "First Name" />
<s:submit action="signup1" method="loginAfterSubmit" value="Press here to submit" />
</s:form>
You can do this with two Action by splitting the execute() and the loginAfterSubmit() methods into two execute() methods of two different Actions;
Then you must have your formBean in BOTH the Actions, with at least the getter in the first, and the setter in the second.
You can also set the Name value in your bean in execute method, it will populate it in your JSP page. Example is :
public String execute() throws Exception {
this.formBean = new ContactFormBean();
//Set your Name value here
formBean.setName("John Doe");
return SUCCESS;
}
Once JSP is populated you will find this value in Name text box.
Hope this will help.
Tapan

How can i invoke the "execute" method of an Action redirected from another action in Struts 2?

I've got a problem with redirecting from one action to another in struts 2.
In action one (called: StudentZuPruefungHinzufuegen) i am creating action errors and want to show these in action two (called:ZeigePruefungsliste).
If i just use the "redirectAction" from struts it shows the correct action error at the place i wanted to show it. The problem is that the framework doesn't invoke my "execute" method before showing the resultpage with the action errors. Though the table I am filling within my "execute" method is empty and the resulting page is in fact useless.
Can somebody tell me how I can tell Struts to invoke the method while redirecting to action two?
My struts.xml looks like that:
<action name="ZeigePruefungsliste"
class="de.nak.multiplechoice.action.ZeigePruefungslisteAction">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<interceptor-ref name="defaultLoginStack" />
<result type="tiles" name="*">pruefungsListe</result>
</action>
<action name="StudentZuPruefungHinzufuegen"
class="de.nak.multiplechoice.action.EditiereTeilnehmerAction">
<result type="tiles">studentZuPruefungHinzufuegen</result>
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<interceptor-ref name="defaultLoginStack" />
<result name="input" type="redirectAction">
<param name="actionName">
ZeigePruefungsliste
</param>
</result>
</action>
EDIT:
Code of my action classes.
public class EditiereTeilnehmerAction extends AbstractAction {
private static final long serialVersionUID = 882657701678551299L;
private Long ausgewaehltePruefungId;
private List<Nutzer> studenten;
private List<Long> ausgewaehlteStudenten;
#Override
public String execute() {
if (ausgewaehltePruefungId == null) {
addActionError("Es muss eine Prüfung ausgewählt werden.");
return INPUT;
}
studenten = getNutzerService().ladeAlleStudenten();
return SUCCESS;
}
public String zuPruefungHinzufuegen() {
getPruefungService().registriereStudentenFuerPruefung(ausgewaehltePruefungId, ausgewaehlteStudenten);
return SUCCESS;
}
public List<Nutzer> getStudenten() {
return studenten;
}
public void setStudenten(List<Nutzer> studenten) {
this.studenten = studenten;
}
public Long getAusgewaehltePruefungId() {
return ausgewaehltePruefungId;
}
public void setAusgewaehltePruefungId(Long ausgewaehltePruefungId) {
this.ausgewaehltePruefungId = ausgewaehltePruefungId;
}
public List<Long> getAusgewaehlteStudenten() {
return ausgewaehlteStudenten;
}
public void setAusgewaehlteStudenten(List<Long> ausgewaehlteStudenten) {
this.ausgewaehlteStudenten = ausgewaehlteStudenten;
}
}
public class ZeigePruefungslisteAction extends AbstractAction {
private static final long serialVersionUID = -7559234907568287686L;
private List<Pruefung> pruefungen;
#Override
public String execute() {
pruefungen = getPruefungService().getAllePruefungen(getAktuellenNutzer());
return SUCCESS;
}
public List<Pruefung> getPruefungen() {
return pruefungen;
}
}
public abstract class AbstractAction extends ActionSupport {
private static final long serialVersionUID = -6905222101893534102L;
private INutzerService nutzerService;
private IPruefungService pruefungService;
public HttpSession getHttpSession() {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(StrutsStatics.HTTP_REQUEST);
return request.getSession(true);
}
public Nutzer getAktuellenNutzer() {
return nutzerService.get(((Nutzer) getHttpSession().getAttribute(
StrutsKonstanten.NUTZER)).getId());
}
protected INutzerService getNutzerService() {
return nutzerService;
}
public void setNutzerService(INutzerService nutzerService) {
this.nutzerService = nutzerService;
}
protected IPruefungService getPruefungService() {
return pruefungService;
}
public void setPruefungService(IPruefungService pruefungService) {
this.pruefungService = pruefungService;
}
}
Greetings,
Marcus

can not login when using Interceptor in struts2

I have a small application to learn Struts2 Application
I write a admin page and inside that , my code will check if user logged or not, if not it will redirect to login page.
I write interceptor to check for all pages that user try to access but not login, it will redirect this user to login page. Everything is work well, but when i enter username and password correct with database, it can not login, when i remove interceptor i can be logged into admin page
Cause maybe interceptor check session of user before and after login, but maybe some cases i dont know why my application, session get null althought my username and password is true but it till null when i set session.
My code bellow will show you what i said:
Login Action
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.view;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import org.dejavu.software.dao.UserDAO;
import org.dejavu.software.model.GroupMember;
import org.dejavu.software.model.User;
/**
*
* #author Administrator
*/
public class AdminLoginAction extends ActionSupport {
private static final long serialVersionUID = -1457633455929689099L;
private User user;
private String username, password;
private String role;
private UserDAO userDAO;
private GroupMember group;
public AdminLoginAction() {
userDAO = new UserDAO();
}
#Override
public String execute() {
String result = null;
System.out.println(getUsername());
if (getUsername().length() != 0 && getPassword().length() != 0) {
setUser(userDAO.checkUsernamePassword(getUsername(), getPassword()));
if (getUser() != null) {
for (GroupMember g : getUser().getGroups()) {
boolean admincp = g.getAdminpermission().contains("1");
if (admincp == true) {
Map session = ActionContext.getContext().getSession();
session.put("userLogged", getUsername());
session.put("passwordLogged", getPassword());
result = "success";
} else {
result = "error";
}
}
}
}
return result;
}
#Override
public void validate() {
if (getUsername().length() == 0) {
addFieldError("username", "Username is required");
}
if (getPassword().length() == 0) {
addFieldError("password", getText("Password is required"));
}
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public GroupMember getGroup() {
return group;
}
public void setGroup(GroupMember group) {
this.group = group;
}
}
My custom interceptor Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.dejavu.software.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.Map;
import org.apache.struts2.StrutsStatics;
/**
*
* #author Anministrator
*/
public class LoginInterceptor extends AbstractInterceptor implements StrutsStatics {
private static final long serialVersionUID = -3874262922233957387L;
#Override
public void destroy() {
}
#Override
public void init() {
}
#Override
public String intercept(ActionInvocation ai) throws Exception {
Map<String, Object> session = ai.getInvocationContext().getSession();
Object user = session.get("userLogged");
if (user == null) {
return "login";
} else {
return ai.invoke();
}
}
}
my struts config
<?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>
<package name="default" namespace="/" extends="struts-default">
<action name="index" class="org.dejavu.software.view.HomeAction">
<result>home.jsp</result>
</action>
<action name="about" class="org.dejavu.software.view.AboutHomeAction">
<result>about.jsp</result>
</action>
</package>
<package name="admincp" namespace="/admincp" extends="struts-default">
<interceptors>
<interceptor name="login" class="org.dejavu.software.interceptor.LoginInterceptor" />
<interceptor-stack name="stack-with-login">
<interceptor-ref name="login"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="stack-with-login"/>
<global-results>
<result name="login">login.jsp</result>
</global-results>
<action name="logincp" class="org.dejavu.software.view.AdminLoginAction">
<result name="success">dashboard.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
</struts>
When i enter correct username and password match to database it till redirect to login.jsp page
and i have no idea about that
please help me
Thank you very much
You must configure your login action to use default interceptor stack or it will NOT execute your method because your interceptor will return login result.
<action name="logincp" class="org.dejavu.software.view.AdminLoginAction">
<interceptor-ref name="defaultStack" />
<result name="success">dashboard.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
you also have to check whether user is trying to log in for first time or not.
Because when user tries to log in first time, the session will always be null it will redirect to login page.
For this you can use one other parameter in your login form to check whether user is trying to log in for first time inside interceptor and if yes then invoke the action.
for example:
<form action='' method=''>
<input type='hidden' name='firstLogin' value='1'/>
<input type='text' name='username'/>
<input type='password' name='password'/>
</form>
I used plain html in this code may be you are using struts2-tags so you can implement in that way also.
And inside your Interceptor check.
request = ai.getInvocationContext().get(HTTP_REQUEST);
if(user == null)
{
if(!StringUtils.isEmpty(request.getParameter('firstLogin'))){
return ai.invoke();
}
return "login";
}
else{
return ai.invoke();
}

Resources