码迷,mamicode.com
首页 > 其他好文 > 详细

用liferay实现的增删改查例子-book管理系统

时间:2015-09-12 15:59:03      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:

liferay 这个框架是一个开源的项目,大家可以修改源代码,来实现自己的需求。但是关于liferay的开发资料中文的很少关于liferay的基础知识,大家可以百度学习一下,再来看下边的例子

首先需要搭建环境,搭建环境百度一大堆,在这里不废话,直接上代码,

开发的步骤1:首先配置需要的配置的文件

<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.1.0//EN" "http://www.liferay.com/dtd/liferay-display_6_1_0.dtd">

<display>
<category name="category.ebizwindow.book">//在这个地方大家需要修改一下名字,不然的话在liferay的界面上不会显示出来你写的项目
<portlet id="BookSystem" />
</category>
</display>

2个配置的文件是

<?xml version="1.0"?>

<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0">
<portlet>
<portlet-name>BookSystem</portlet-name>
<display-name>BookSystem</display-name>
<portlet-class>com.ebizwindow.exceed.portlet.BookPortlet</portlet-class>//在这地方修改你要写的portlet的名字,默认生成的是JSPPortlet
<init-param>
<name>view-jsp</name>
<value>/jsp/book/mybook-list.jsp</value>//这个页面是你要访问的第一个页面,在这个页面是通过doView方法找到的页面。
</init-param>
<init-param>
<name>edit-jsp</name>
<value>/jsp/edit.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<supported-locale>zh_CN</supported-locale>
<resource-bundle>content.Language-ext</resource-bundle>
<portlet-info>
<title>BookSystem</title>
<short-title>BookSystem</short-title>
<keywords>BookSystem</keywords>
</portlet-info>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
</portlet-app>

3.是service.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">

<service-builder package-path="com.ebizwindow.exceed">
<namespace>BookSystem</namespace>

<entity name="Book" local-service="true" remote-service="false">//remote-service="false"这个设置为false的就是自动生成的时候不是生成一些方法, local-service="true"这个设置为true就会在项目中生成一些程序员要调用的方法。

<!-- company 公司的id -->
<column name="companyId" type="long" />//这个companyId就是自动有的这个不需要修改,就是公司的id


<!-- PK fields -->
<column name="bookId" type="long" primary="true" />//这个就是你的实体的id

<!-- Other fields 实体的属性-->
<column name="bookName" type="String" />
<column name="bookAuthor" type="String" />
<column name="bookAddress" type="String" />
<column name="bookPrice" type="double" />
<column name="bookDate" type="Date" />

<!-- Order 检索出来的数据按照name排序-->
<order by="asc">
<order-column name="bookName" />
</order>

<!-- Finder methods -->
<finder name="companyId" return-type="Collection">//对应自动生成的方法是getCompanyId(long companyId)。顾名思义就是按照公司的id检索所有的数据
<finder-column name="companyId" />
</finder>
<!-- 这个是根据用户的名字返回图书 -->
<finder return-type="Collection" name="bookName">//对应生成的方法是getBookName(String bookName)

<finder-column name="bookName" />

</finder>

<!-- 根据作者来查看图书 -->
<finder return-type="Collection" name="bookAuthor">//对应生成的方法是getBookAuthor(String bookAuthor)

<finder-column name="bookAuthor" />

</finder>

</entity>

</service-builder>

需要注意:根据实体的主键来检索一条数据,这个方法是不需要配置,默认会生成.

好吧,罗嗦了一大会,配置文件就搞定了,点击builder.xml,点击builder-service就会自动生成方法和实体,程序员只需要写自己的逻辑的代码就行了。

下面开始写portlet,这个portlet跟大家学的servlet有很大相似,但是也有不同,自己百度查查就知道。

package com.ebizwindow.exceed.portlet;

import com.ebizwindow.exceed.model.Book;
import com.ebizwindow.exceed.service.BookLocalServiceUtil;
import com.liferay.compat.portal.util.PortalUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portlet.PortletURLFactoryUtil;

import java.io.IOException;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletModeException;
import javax.portlet.PortletRequest;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletResponse;
import javax.portlet.PortletURL;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowStateException;


public class BookPortlet extends GenericPortlet {
	
	private final static String FORWARD = "forward";
	
	private final static String OPERATION = "operation";

	public void init() throws PortletException {
		editJSP = getInitParameter("edit-jsp");
		helpJSP = getInitParameter("help-jsp");
		viewJSP = getInitParameter("view-jsp");
	}

	
	public void doList(PortletRequest portletRequest){
		
		long companyId = PortalUtil.getCompanyId(portletRequest);
		
		_log.info(companyId+"ggggggggg$$$$$$$$$$$$$$$$$$$$");
		try {
			//得到数据库中所有的书籍
			List<Book> books = BookLocalServiceUtil.searchByCompanyId(companyId);
			
			_log.info(StringUtil.merge(books)+"abcdef=============================");
			
			//然后把书发送给jsp.首先把图书存到响应中
			portletRequest.setAttribute("books", books);
		
		} catch (SystemException e) {
			e.printStackTrace();
		}
		
	}
	
	public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {
		
		String jspPage = renderRequest.getParameter(FORWARD);
		if (jspPage == null) {
			this.doList(renderRequest);
		}
		jspPage = jspPage == null ? this.viewJSP : jspPage;
		include(jspPage, renderRequest, renderResponse);
		
		
		
		
	}

	public void doEdit(RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		if (renderRequest.getPreferences() == null) {
			super.doEdit(renderRequest, renderResponse);
		} else {
			include(editJSP, renderRequest, renderResponse);
		}
	}

	public void doHelp(RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		include(helpJSP, renderRequest, renderResponse);
	}
	
	public static PortletURL getPortletURL(ActionRequest actionRequest) throws PortalException, SystemException {
		long groupId = PortalUtil.getScopeGroupId(actionRequest);
		String portletId = PortalUtil.getPortletId(actionRequest);
		long plid = PortalUtil.getPlidFromPortletId(groupId, portletId);	
		PortletURL redirectURL = PortletURLFactoryUtil.create(PortalUtil.getHttpServletRequest(actionRequest),
				portletId, plid, PortletRequest.ACTION_PHASE);
		try {
			redirectURL.setPortletMode(actionRequest.getPortletMode());
			redirectURL.setWindowState(actionRequest.getWindowState());
		} catch (PortletModeException e) {
			_log.error(e);
		} catch (WindowStateException e) {
			_log.error(e);
		}
		return redirectURL;
	}

	public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
		throws IOException, PortletException {

		//得到操作的名称
		String actionName = ParamUtil.getString(actionRequest, OPERATION);
		
		if (actionName != null) {
			String forward = null;
			_log.info(ActionRequest.ACTION_NAME + " = " + actionName);
			
			
			if (actionName.equals("list")) {
				//首先执行这个请求的时候,就找到数据要去的页面
				//1:首先从数据库中选出来所有的数据,根据公司的id就行了
				//首先获取id
				this.doList(actionRequest);
				
				forward = "/jsp/book/mybook-list.jsp";
			} else if(actionName.equals("details")) {
				// 查看书的详细的信息1:首先获得前台界面传过来的id
				long bookId = ParamUtil.getLong(actionRequest, "bookId",0L);
				
				//在调用本地方法的实现的类
				Book book = null;
				try {
					 book = BookLocalServiceUtil.getBook(bookId);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//把这本书发送给界面就可以
				actionRequest.setAttribute("book", book);
				
				forward = "/jsp/book/mybook-details.jsp";
			} else if (actionName.equals("load")) {
				//如果的到的参数load的话就是要跳转的页面
				
				_log.info("----------------------------------------------"+actionName+"-------success");
				forward = "/jsp/book/mybook-add.jsp";
				
			}
			
			
			else if(actionName.equals("add")) {
				_log.info("@@@@@@@@@@@@@@@@@@@@@@@");
				//首先获取信息页面传过来的信息
				String bookName = ParamUtil.getString(actionRequest, "bookName",StringPool.BLANK);
				String bookAuthor =ParamUtil.getString(actionRequest, "bookAuthor",StringPool.BLANK);
				String bookAddress = ParamUtil.getString(actionRequest, "bookAddress",StringPool.BLANK);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				Date bookDate = (Date) ParamUtil.getDate(actionRequest, "bookDate", dateFormat);
				double bookPrice = ParamUtil.getDouble(actionRequest, "bookPrice",0.0);
				
				long companyId = PortalUtil.getCompanyId(actionRequest);
				_log.info(bookName+"--"+bookAuthor+"--"+bookAddress+"--"+bookPrice+"--"+bookDate);
				//添加图书,添加图书的时候你需要考虑id
				long bookId = 0L;
				try {
					bookId = CounterLocalServiceUtil.increment(Book.class.getName());
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//创建一个book的引用
				Book book = BookLocalServiceUtil.createBook(bookId);
				
				book.setBookAddress(bookAddress);
				book.setBookAuthor(bookAuthor);
				book.setBookDate(bookDate);
				book.setBookName(bookName);
				book.setBookPrice(bookPrice);
				book.setCompanyId(companyId);
				//调用添加的方法添加到数据库
				try {
					BookLocalServiceUtil.addBook(book);
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				this.doList(actionRequest);
				//数据添加到数据库中之后就到list的界面上
				
				
				//重定向
				PortletURL redirect = null;
				try {
					redirect = getPortletURL(actionRequest);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				redirect.setPortletMode(PortletMode.VIEW);
				actionResponse.sendRedirect(redirect.toString());
				
			
				
			}else if (actionName.equals("edit")){
				//首先获取用户要修改的id,然后根据id找到信息
				long bookId = ParamUtil.getLong(actionRequest, "bookId",0L);
				
				Book book = null;
				try {
					 book = BookLocalServiceUtil.getBook(bookId);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//把这本书发送给界面就可以
				actionRequest.setAttribute("book", book);
				
				
				
				forward = "/jsp/book/mybook-edit.jsp";
			} 
			
			
			
			else if(actionName.equals("editTrue")) {//得到的方法是修改
				//首先获得要编辑图书的id
				long bookId = ParamUtil.getLong(actionRequest, "bookId",0L);
				//根据id找到图书
				Book book = null;
				try {
					 book= BookLocalServiceUtil.getBook(bookId);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				//获得提交修改后的数据的参数
				String bookName = ParamUtil.getString(actionRequest, "bookName",StringPool.BLANK);
				String bookAuthor =ParamUtil.getString(actionRequest, "bookAuthor",StringPool.BLANK);
				String bookAddress = ParamUtil.getString(actionRequest, "bookAddress",StringPool.BLANK);
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				Date bookDate = (Date) ParamUtil.getDate(actionRequest, "bookDate", dateFormat);
				double bookPrice = ParamUtil.getDouble(actionRequest, "bookPrice",0.0);
				book.setBookAddress(bookAddress);
				book.setBookAuthor(bookAuthor);
				book.setBookDate(bookDate);
				book.setBookName(bookName);
				book.setBookPrice(bookPrice);
				//调用数据库中修改的方法
				try {
					BookLocalServiceUtil.updateBook(book);
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				PortletURL redirect = null;
				try {
					redirect = getPortletURL(actionRequest);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				redirect.setPortletMode(PortletMode.VIEW);
				actionResponse.sendRedirect(redirect.toString());
			
			
			
			
			} else if (actionName.equals("delete")){
				//删除的方法.首先获得要删除的数据的id
				long bookId = ParamUtil.getLong(actionRequest, "bookId",0L);
				//调用数据库删除的方法
				try {
					BookLocalServiceUtil.deleteBook(bookId);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
				PortletURL redirect = null;
				try {
					redirect = getPortletURL(actionRequest);
				} catch (PortalException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SystemException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				redirect.setPortletMode(PortletMode.VIEW);
				actionResponse.sendRedirect(redirect.toString());
				
				
			}else{
				forward = this.helpJSP;
			}

			_log.info(FORWARD + " = " + forward);
			
			if(forward != null){
				actionResponse.setRenderParameter(FORWARD, forward);
			}
			
		}
	}
		
	public void doDispatch(RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		String jspPage = renderRequest.getParameter(FORWARD);

		if (jspPage != null) {
			include(jspPage, renderRequest, renderResponse);
		} else {
			super.doDispatch(renderRequest, renderResponse);
		}
	}

	protected void include(String path, RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {

		PortletRequestDispatcher portletRequestDispatcher =
			getPortletContext().getRequestDispatcher(path);

		if (portletRequestDispatcher == null) {
			_log.error(path + " is not a valid include");
		} else {
			portletRequestDispatcher.include(renderRequest, renderResponse);
		}
	}

	protected String editJSP;
	protected String helpJSP;
	protected String viewJSP;

	private static Log _log = LogFactoryUtil.getLog(BookPortlet.class);

}

  界面代码:

   mybook-list.jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="Utf-8"%>
<%@page import="javax.portlet.PortletPreferences"%>
<!-- 引入init.jsp -->
<%@ include file="/init.jsp"%>
<!-- 使用portlet标签 来设计页面-->
<!-- 首先定义portlet提交的路径
<portlet:actionURL var="searchURL">
	<portlet:param name="operation" value="example.list" />
</portlet:actionURL>
 -->
<portlet:actionURL var="listURL">

	<portlet:param name="operation" value="list" />

</portlet:actionURL>

<!-- form表单的内容 -->
<form action="<%=listURL %>" method="post">
	
	<fieldset>
	
		<table>
			<tr>
				<legend>图书列表</legend>
				<th>图书名称</th>
				<th>图书作者</th>
				<th>图书出版社</th>
				<th>图书出版日期</th>
				<th>图书价格</th>
			</tr>
			
			<%
				//首先获取后台发过来的数据List<Orenge> results = (List<Orenge>)renderRequest.getAttribute("results");
				List<Book> books = (List<Book>)renderRequest.getAttribute("books");
				
				
				
				
				if (books.size()>0 || books != null) {
					for (Book book:books) {%>
				
				<td>
					<portlet:actionURL var="detailBookURL" windowState="<%=LiferayWindowState.EXCLUSIVE.toString() %>">
							<portlet:param name="operation" value="details" />
							<portlet:param name="bookId" value=‘<%=String.valueOf(book.getBookId())%>‘ />
						</portlet:actionURL>
						<a href="javascript:showOrenge(‘<%=detailBookURL %>‘, ‘book‘)"><%=book.getBookName() %></a>
				</td>
				
				<td>
					<%=book.getBookAuthor() %>
				
				</td>
				
				<td>
					<%=book.getBookAddress() %>
				
				</td>
				
				<td>
					<%=book.getBookDate() %>
				
				</td>
				
				<td>
					<%=book.getBookPrice() %>
				
				</td>
				
						
				<td>
						<liferay-ui:icon-menu>
							<portlet:actionURL var="editBookURL">
								<portlet:param name="operation" value="edit" />
								<portlet:param name="bookId" value=‘<%=String.valueOf(book.getBookId())%>‘ />
							</portlet:actionURL>
							<liferay-ui:icon image="edit" url="<%=editBookURL %>" message="edit"/>
							
							<portlet:actionURL var="deleteBookURL">
								<portlet:param name="operation" value="delete"/>
								<portlet:param name="action" value="delete"/>
								<portlet:param name="bookId" value=‘<%=String.valueOf(book.getBookId())%>‘ />
							</portlet:actionURL>
							<liferay-ui:icon-delete url="<%=deleteBookURL %>" />
						</liferay-ui:icon-menu>
					</td>		
				
					<% }
				
				}
			
			%>
		
		</table>
		
	</fieldset>
	
	<div>
		<portlet:actionURL var="addBookURL">
			<!-- 找到添加的页面 -->
			<portlet:param name="operation" value="load" />
			
		</portlet:actionURL>
		<input type="button" value="添加" onclick="javascript:location.href=‘<%=addBookURL %>‘">
		
	</div>
	
	
	
	
	
	



</form>

  2.mybook-add.jsp

   

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/init.jsp" %>
<portlet:actionURL var="addBookURL">
	<!-- 执行add的操作 -->
	<portlet:param name="operation" value="add" />
	
</portlet:actionURL>

  <form action="<%=addBookURL %>" method="post">
		图书名称:<input type="text" name="bookName" /></br>
		图书作者:<input type="text" name="bookAuthor"/></br>
		出版社:<input type="text" name="bookAddress"/></br>
		出版日期:<input type="text" name="bookDate"/></br>
                           图书价格:<input type="text" name="bookPrice"/></br>
		
		<input type="submit" value="提交"/>
		<input type="reset" value="重置"/>
  </form>

  3.mybook-edit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/init.jsp" %>

<%
	//得到要修改的图书
	Book book = (Book)renderRequest.getAttribute("book");


%>

<portlet:actionURL var="editBookURL">
	<portlet:param name="operation" value="editTrue" />
	<portlet:param name="bookId" value=‘<%=book != null ? String.valueOf(book.getBookId()) : "0" %>‘ />
</portlet:actionURL>

  <form action="<%=editBookURL %>" method="post">
		图书名称:<input type="text" name="bookName" value=‘<%=book != null ? String.valueOf(book.getBookName()):"" %>‘/></br>
		图书作者:<input type="text" name="bookAuthor" value=‘<%=book != null ? String.valueOf(book.getBookAuthor()) :"" %>‘/></br>
		出版社:<input type="text" name="bookAddress" value=‘<%=book != null ? String.valueOf(book.getBookAddress()):""  %>‘/></br>
		出版日期:<input type="text" name="bookDate" value=‘<%=book != null   ? String.valueOf(book.getBookDate()):""  %>‘/></br>
                           图书价格:<input type="text" name="bookPrice" value=‘<%=book != null  ? String.valueOf(book.getBookPrice()):""  %>‘/></br>
		
		<input type="submit" value="提交"/>
		<input type="reset" value="重置"/>
  </form>

  4.mybook-detail.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 引入init.jsp -->
<%@ include file="/init.jsp"%>
<%
	Book book = (Book)renderRequest.getAttribute("book");
%>

<style>
	#bookcontent{
		
	
	
	}



</style>

<div id = "bookcontent">
<table>
	<tr>
		<td>图书名称</td>
		<td>图书作者</td>
		<td>出版地址</td>
		<td>出版日期</td>
		<td>图书价格</td>
	</tr>
	
	<tr>
		<td>
			<%=book.getBookName() %>
		</td>
		
		<td>
			<%=book.getBookAuthor() %>
		</td>
		
		<td>
			<%=book.getBookAddress() %>
		</td>
		
		<td>
			<%=book.getBookDate() %>
		</td>
		
		<td>
			<%=book.getBookPrice() %>
		</td>
	
	
	
	</tr>
	

</table>
</div>

  终于结束了点击运行服务器,就可以实现了,下面动手试试吧!

 

用liferay实现的增删改查例子-book管理系统

标签:

原文地址:http://www.cnblogs.com/airycode/p/4803012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!