关闭

OpenCms中简单的Login模板实例代码

2095人阅读 评论(0) 收藏 举报

以下代码在JSP中实现OpenCms用户的登陆,并且转入相应的项目的特定页面.希望对大家有所帮助。

CODE:

<%@ page import="java.util.* ,
org.opencms.jsp.*,
org.opencms.file.*,
com.opencms.core.*,
org.opencms.main.CmsException" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>

<%
String sErrorMessage = null;
String sName = null;
CmsJspActionElement cms = new CmsJspActionElement(pageContext, request, response);
boolean logout = "true".equals(request.getParameter("logout"));

CmsObject oCms = cms.getCmsObject();

if(logout){
session.invalidate();
System.out.println("DesLogeado");
}

if(request.getParameter("name") != null && request.getParameter("password") != null){

String sUsername = (String)request.getParameter("name");
String sPassword = (String)request.getParameter("password");
try{
oCms.loginUser(sUsername, sPassword);
oCms.getRequestContext().setCurrentProject(oCms.readProject("Online"));//登陆的项目
response.sendRedirect(cms.link("/"));//转到登陆以后的页面
}catch(CmsException cmsE){
sErrorMessage = "用户登陆错误!";
}
}

if(!cms.user("name").equals("Guest")){

%>
<退出登陆:
<form method="post" action="<%= cms.link(oCms.getRequestContext().getUri()) %>" target="_self" name="logout" topmargin="0" marginheight="0" style="margin-top:0px; margin-bottom:0px;">
<input type="hidden" name="logout" value="true"><input type="submit" value="<< 退出">
</form>
<br>
<%}else{%>
<form method="post" action="<%= cms.link(oCms.getRequestContext().getUri()) %>" target="_self" name="login" topmargin="0" marginheight="0" style="margin-top:0px; margin-bottom:0px;">
<table width="600" border="0" align="center" cellpadding="0" cellspacing="0">
<% if(sErrorMessage != null) { %>
<tr>
<td class="login_error"><%= sErrorMessage %>
</td>
</tr>
<% } %>
<tr>
<td width="1" valign="top" bgcolor="#6166AB">&nbsp;</td>
<td valign="top"></td>
<td valign="top">
<table width="60%" border="0" cellspacing="5" cellpadding="5">
<tr>
<td width="54%">账号:</td>
<td width="46%"><input type="text" size="10" name="name" ></td>
</tr>
<tr >
<td>密码:</td>
<td><input name="password" type="password" size="10" ></td>
</tr>
<tr >
<td colspan="2">
<div align="right">
<input name="submit" type="submit" value="登陆 >>">
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<br>
<%}%>

 

自定义登陆界面

CODE:

<%@
page session="true"
import="org.opencms.main.*,
org.opencms.jsp.*,
org.opencms.file.*,
java.lang.String" %><%
CmsJspActionElement cms = new CmsJspActionElement(pageContext,request,response);

String user = request.getParameter("user");
String password = request.getParameter("password");
String url = request.getParameter("url");

if( url==null || url.length()==0 || url.equalsIgnoreCase("null") )
url = cms.link("/index.html");

boolean loginFailed = false;

//如果表单提交 => 试着登陆并转向给定的 URl
if((user!=null)&&(user.length()!=0)) {
try{
CmsObject cmsObject = cms.getCmsObject();
cmsObject.loginUser(user, password);
CmsProject cmsproject = cmsObject.readProject("Offline");
cmsObject.getRequestContext().setCurrentProject(cmsproject);
cmsObject.getRequestContext().setSiteRoot("/sites/default");

//登陆成功-重定向
response.sendRedirect(url);

} catch ( CmsException e ) {
loginFailed = true;
}
}

//no user submitted or login failed => show login form
if((user==null)||(user.length()==0)||loginFailed) {
//提交表单
%><!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>Frontend login</title></head>
<body onload="document.forms[0].elements[0].focus()">

<%if (loginFailed) { %><em>登陆失败!</em><% } %>

<h1>请登陆:</h1>
<form method="post" action="<%=cms.info("opencms.url")%>">
<input type="hidden" name="url" value="<%=url%>" />
<p>用户: <input name="user" /></p>
<p>密码: <input name="password" type="password" /></p>
<p><input type="submit" value="登陆" /></p>
</form></body></html><%
}
%>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:27667次
    • 积分:383
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条