1原生Portlet开发
这是最简单、最本质的开发方式,直接基于Portlet规范定义的接口开发Portlet。优点是贴近底层比较灵活,
缺点当然就是所有事情都要自己去做。就好比不用SpringMVC、Struts,直接基于Servlet开发一样。
这种方式比较适合自己开发Portlet框架。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
public class NativePortlet implements Portlet {
private Logger logger = LoggerFactory.getLogger(NativePortlet. class );
private PortletConfig config;
@Override
public void init(PortletConfig portletConfig) throws PortletException {
logger.info( "初始化Portlet" );
this .config = portletConfig;
}
@Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException {
logger.info( "处理Action动作" );
logger.info( "接收到POST请求,用户名为[{}]" , actionRequest.getParameter( "userName" ));
}
@Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException {
logger.info( "处理Render动作" );
WindowState state = renderRequest.getWindowState();
if (state.equals(WindowState.MINIMIZED)) {
return ;
}
// doDispatch();
PortletMode mode = renderRequest.getPortletMode();
if (PortletMode.VIEW.equals(mode)) {
// doView();
String normalPage = config.getInitParameter( "ViewPage" );
if (WindowState.NORMAL.equals(state)) {
this .include(renderRequest, renderResponse, normalPage);
} else {
this .include(renderRequest, renderResponse, "" );
}
} else if (PortletMode.EDIT.equals(mode)) {
// doEdit();
} else if (PortletMode.HELP.equals(mode)) {
// doHelp();
} else {
throw new PortletException( "unknown portlet mode: " + mode);
}
}
@Override
public void destroy() {
logger.info( "销毁Portlet" );
}
private void include(RenderRequest request, RenderResponse response, String viewPage) throws PortletException, IOException {
response.setContentType( "text/html" );
PortletContext context = config.getPortletContext();
PortletRequestDispatcher requestDispatcher = context.getRequestDispatcher(viewPage);
requestDispatcher.include(request, response);
}
}
|
可以看到,在render()方法中,我们要自己处理不同Portlet模式(View,Edit,Help等)的渲染,根据需要做分发。
诸如此类的各种细节都需要我们自己去制定规则,自己处理。
2注解Portlet开发
Portlet规范也提供了注解的方式来开发Portlet。这样不用依赖SpringMVC Portlet等第三方框架,
也能比较方便的开发Portlet,又不用处理特别底层的实现细节。来看一个例子。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class AnnotatedPortlet extends GenericPortlet {
private Logger logger = LoggerFactory.getLogger(AnnotatedPortlet. class );
/**
* 进入主页面(View模式)
* @param request
* @param response
* @throws Exception
*/
@RenderMode (name = "view" )
public void toIndexPage(RenderRequest request, RenderResponse response) throws Exception {
getPortletContext().
getRequestDispatcher(getInitParameter( "ViewPage" )).
include(request, response);
}
/**
* 保存用户名
* @param request
* @param response
*/
@ProcessAction (name = "saveUsername" )
public void saveUsername(ActionRequest request, ActionResponse response) {
String userName = request.getParameter( "userName" );
logger.info( "保存用户名[{}]" , userName);
}
/**
* 保存电子邮件地址
* @param request
* @param response
*/
@ProcessAction (name = "saveEmail" )
public void saveEmail(ActionRequest request, ActionResponse response) {
String email = request.getParameter( "email" );
logger.info( "保存Email[{}]" , email);
}
}
|
使用@RenderMode注解定义不同模式下,Portlet渲染的实现方法。@ProcessAction注解可以根据动作名称,
直接将请求交给标有注解的方法去处理,不用我们自己去实现根据注解分发请求的代码了。
附:其他代码
页面代码如下。使用Portlet规范中定义的标签产生ActionUrl。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
< portlet:defineObjects />
< h3 >这是一个演示FORM表单和AJAX提交的示例程序</ h3 >
<!-- FORM表单提交实例 -->
< form action='<portlet:actionURL name = "saveUsername" />' method="POST">
< tr >
< td >用户名:</ td >
< td >< input name = "userName" type = "text" /></ td >
</ tr >
< tr >
< td >< input type = "submit" value = "表单提交" /></ td >
</ tr >
</ form >
<!-- AJAX提交实例 -->
< tr >
< td >Email:</ td >
< td >< input id = "email" type = "text" /></ td >
</ tr >
< tr >
< input id = "ajaxBtn" type = "button" value = "AJAX提交" />
</ tr >
< script >
$(".ajaxBtn").bind("click", function () {
var email = $(".email").valueOf();
jQuery.ajax({
type: "GET",
async: true,
url: '< portlet:actionURL name = "saveEmail" />',
dataType: 'json',
data: {
"email": email
},
success: function (data) {
alert(data);
},
error: function (data, status, e) {
alert(e);
},
beforeSend: function () {
},
complete: function () {
}
});
});
</ script >
|