简体   繁体   中英

Integration of struts2 with Liferay?

I made a sample application to integrate struts2 with liferay. But the problem arises when the setter method in the Action class doesn't get invoked. I am passing parameters through the struts form to the Action class but when I am printing those values through welcome.jsp I am getting values as null for name and 0 for id . Here is my code for form:-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>

<s:form action="product">  
<s:textfield name="id" label="Product Id"></s:textfield>  
<s:textfield name="name" label="Product Name"></s:textfield>  
<s:textfield name="price" label="Product Price"></s:textfield>  
<s:submit value="save"></s:submit>  
</s:form>  

The code for struts.xml is as follows:-

<?xml version="1.0" encoding="UTF-8" ?>  
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts  
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
<struts>  
<constant name="struts.objectFactory.spring.autoWire" value="type" />
<package name="view" namespace="/view" extends="struts-portlet-default">  

  <action name="product" class="com.abhishek.Product" method="execute">  

<result name="success">/welcome.jsp</result>  
</action>  

<action name="index"
            class="com.abhishek.Product">
            <result name="success">/index.jsp</result>
        </action>


</package>  

</struts> 

The code for Product.java is as follows:-

package com.abhishek;  

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

public class Product extends ActionSupport implements ServletRequestAware {  
/**
     * 
     */
    private static final long serialVersionUID = 1L;
/**
     * 
     */
    HttpServletRequest request;
    private int id;  
private String name;  
private float price;  
public int getId() {  
    return id;  
}  
public void setId(int id) {  
    this.id = id;  
}  
public String getName() {
       return name;  
}  
public void setName(String name) {  
    System.out.println("Inside setName");
    this.name = name;  
}  
public float getPrice() {  
    return price;  
}  
public void setPrice(float price) {  
    this.price = price;  
}  

public String execute(){
    System.out.println("name is"+ ActionContext.getContext().getParameters().get("name"));
    System.out.println("name="+name);
    return "success";  
} 

public String execute1(){  
    return "success";  
}
@Override
public void setServletRequest(HttpServletRequest request) {
    // TODO Auto-generated method stub
    this.request=request;
} 

public HttpServletRequest getServletRequest()
{
    return this.request;
}

}  

The code for welcome.jsp is as follows:-

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>  

Product Id:<s:property value="id"/><br/>  
Product Name:<s:property value="name"/><br/>  
Product Price:<s:property value="price"/><br/>

I want to ask if it is really be possible to integrate struts2 application with liferay and if so why does setter method not get invoked?I have already made a couple of applications in struts2 in liferay but came with same problem..So please help..any help would be appreciated.

Try checking the parameter value without reading using getParameter("yourParameter") method. Check if they are printed correctly, otherwise your model itself not initialized properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM