Mvcportlet Example

Model:

package com.test.business;
 
public class Company {
    private String companyName;
    private String address1;
    private String address2;
    private String city;
    private String zip;
    private String country;
 
    public String getCompanyName() {
        return companyName;
    }
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
    public String getAddress1() {
        return address1;
    }
    public void setAddress1(String address1) {
        this.address1 = address1;
    }
    public String getAddress2() {
        return address2;
    }
    public void setAddress2(String address2) {
        this.address2 = address2;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
 
}
package com.test.business;
 
public class LoadCompany {
 
    public static Company getCompany()
    {
        Company company = new Company();
        company.setCompanyName("Travel Tips International");
        company.setAddress1("123 North Main");
        company.setAddress2("Suite 600");
        company.setCity("Southfield");
        company.setZip("48075");
        company.setCountry("United States");
 
        return company;
    }
 
}
package com.test.business;
 
import java.util.Vector;
 
public class LoadToys {
 
    public static Vector getAvailableItems() {
        Vector v = new Vector();
        v.add("Ninjago");
        v.add("Clone troopers");
        v.add("Rubber ducky");
 
        return v;
    }
 
}

View:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
 
<portlet:defineObjects />
 
<table>
<tr>
<td>Company Name:</td><td>${company.companyName}</td>
</tr>
<tr>
<td>Address:</td><td>${company.address1}</td>
</tr>
<tr>
<td></td><td>${company.address2}</td>
</tr>
</table>
 
<ul>
<c:forEach var="toy" items="${itemList}">
<li>${toy}</li>
</c:forEach>
 
</ul>

The <portlet:defineObjects /> tag defines the following implicit portlet objects:
  • RenderRequest
  • RenderResponse
  • PortletConfig

You must include the tag as the first tag of a JSP to use the implicit portlet objects throughout the JSP.

Controller:

package com.test;
 
import java.io.IOException;
import java.util.Vector;
 
import javax.portlet.*;
 
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.test.business.Company;
import com.test.business.LoadCompany;
import com.test.business.LoadToys;
 
/**
 * Portlet implementation class MyGraph
 */
public class MyGraph extends MVCPortlet {
 
    public  void doView (RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException 
    {
        Company company = LoadCompany.getCompany();
        renderRequest.setAttribute("company", company);
 
        Vector itemList = LoadToys.getAvailableItems();
        renderRequest.setAttribute("itemList", itemList);
 
        include(viewJSP, renderRequest, renderResponse);
 
    }
 
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License