Liferay Struts Portlet 2

1.Define the Action

struts-config.xml, add

< action  path ="/ext/library/add_book"  type ="com.ext.portlet.library.action.AddBookAction" >
    
< forward  name ="portlet.ext.library.error"  path ="portlet.ext.library.error"   />
    
< forward  name ="portlet.ext.library.success"  path ="portlet.ext.library.success"   />
</ action >

tiles-defs.xml, add

< definition  name ="portlet.ext.library.error"  extends ="portlet.ext.library" >
    
< put  name ="portlet_content"  value ="/portlet/ext/library/error.jsp"   />
</ definition >

< definition  name ="portlet.ext.library.success"  extends ="portlet.ext.library" >
    
< put  name ="portlet_content"  value ="/portlet/ext/library/success.jsp"   />
</ definition >

2.Update existing JSP files

init.jsp, remove the following:

<p>Add commonly used variables and declarations here!</p>

view.jsp

<% @ include file="/html/portlet/ext/library/init.jsp"  %>

< br />
Add a book entry to the Library:
< br />< br />
< form  action ="<portlet:actionURL windowState=" <% = WindowState.MAXIMIZED.toString()  % > "> < portlet:param  name ="struts_action"  value ="/ext/library/add_book"   /></ portlet:actionURL > " method="post" name=" < portlet:namespace  /> fm">    
    
    Book Title:
    
    
< input  name ="<portlet:namespace />book_title"  size ="20"  type ="text"  value ="" >< br />< br />
    
    
< input  type ="button"  value ="Submit"  onClick ="submitForm(document.<portlet:namespace />fm);" >
</ form >
< br />

3.Create success and error JSP files

error.jsp, Contents:

ERROR!

success.jsp

< %@ include  file ="/html/portlet/ext/library/init.jsp"  % >
< %
    String 
bookTitle  = request.getParameter("book_title");
% >
< table  align ="center"  cellspacing ="10"  cellpadding ="3" >
< tr >
    
< td  style ="font-weight:bold" > Book Title: </ td >
    
< td >< %= bookTitle  % ></ td >
</ tr >
</ table >

4.Create Action Class to process submit

ext/ext-ejb/src/com/ext/portlet/library/action/AddBookAction.java 

package  com.ext.portlet.library.action;

import  javax.portlet.ActionRequest;
import  javax.portlet.ActionResponse;
import  javax.portlet.PortletConfig;
import  javax.portlet.RenderRequest;
import  javax.portlet.RenderResponse;

import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;

import  com.liferay.portal.struts.PortletAction;
public   class  AddBookAction  extends  PortletAction  {
    
public void processAction(
            ActionMapping mapping, ActionForm form, PortletConfig config,
            ActionRequest req, ActionResponse res)

        
throws Exception {
        
        String bookTitle 
= req.getParameter("book_title");
        
        
if ( null == bookTitle || "".equals(bookTitle) ) {
            setForward(req, 
"portlet.ext.library.error");
        }
 else {     
            setForward(req, 
"portlet.ext.library.success");
        }

    }


    
public ActionForward render(ActionMapping mapping, ActionForm form,
        PortletConfig config, RenderRequest req, RenderResponse res)
        
throws Exception {
        
if (getForward(req) != null && !getForward(req).equals("")) {
            
return mapping.findForward(getForward(req));
        }
 else {
            
return mapping.findForward("portlet.ext.library.view");
        }

    }

}

Finally, .../ext>ant deploy

Restart Tomcat, ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值