jetspeed学习笔记2

1.这次主要是针对portlet编程,首先建立java类TestDataPortlet extends GenericPortlet,重写doView、processAction、init和destroy。代码如下所示:
package com.tortrend.portlet;

import java.io.IOException;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.EventRequest;
import javax.portlet.EventResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.WindowState;

import com.tortrend.general.TestDataBean;
import com.tortrend.general.TestDataDAOImpl;

public class TestDataPortlet extends GenericPortlet {

	private static final String NORMAL_VIEW = "/test/normal.jsp";
	private static final String MAXIMIZED_VIEW = "/test/maximized.jsp";
	private static final String HELP_VIEW = "/test/help.jsp";

	private PortletRequestDispatcher normalView;
	private PortletRequestDispatcher maximizedView;
	private PortletRequestDispatcher helpView;

	protected void doView(RenderRequest request, RenderResponse response)
			throws PortletException, IOException {
		System.out.println("testdataportlet doView ...");
		PortletSession session = request.getPortletSession();
		if ("delete".equals(request.getParameter("actionflag"))) {
			int id = Integer.parseInt(request.getParameter("id"));
			TestDataDAOImpl.delete(id);
			System.out.println("testdataportlet doView delete");
		} else if("adduser".equals(request.getParameter("actionflag"))) {
			int id = TestDataDAOImpl.getCount() + 1;
			TestDataBean tdb = new TestDataBean();
			tdb.setId(id);
			tdb.setName("wgj" + id);
			tdb.setPassword("123");
			TestDataDAOImpl.add(tdb);
			System.out.println("testdataportlet doView add");
		} else if("update".equals(request.getParameter("actionflag"))) {
			int id = Integer.parseInt(request.getParameter("id"));
			TestDataDAOImpl.update(id);
			session.removeAttribute("test.list");
			System.out.println("testdataportlet doView update");
		}
		int curp = request.getParameter("curp") != null ? Integer.parseInt(request.getParameter("curp")):1;
		int pagesize = 5;
		int totalNum = TestDataDAOImpl.getCount();
		int totalPage = (totalNum + pagesize - 1 ) / pagesize;

		List<TestDataBean> list = TestDataDAOImpl.getSome(curp, pagesize);
		session.setAttribute("test.list", list);
		session.setAttribute("test.curp", curp);
		session.setAttribute("test.totalPage", totalPage);
		if (WindowState.MINIMIZED.equals(request.getWindowState())) {
			return;
		}

		if (WindowState.NORMAL.equals(request.getWindowState())) {
			normalView.include(request, response);
		} else {
			maximizedView.include(request, response);
		}
		
		
	}	
public void processAction(ActionRequest request, ActionResponse arg1)
			throws PortletException, IOException {
		if("Submit".equals(request.getParameter("actionflag"))) {
			//System.out.println("testdataportlet processAction checklogin");
			PortletSession session = request.getPortletSession();
			String name = request.getParameter("username");
			String password = request.getParameter("password");
			System.out.println("name=" + name);
			System.out.println("password=" + password);
			if(TestDataDAOImpl.checkLogin(name, password)) {
				session.setAttribute("test.username", name);
				List<TestDataBean> list = TestDataDAOImpl.getAll();
				session.setAttribute("test.list", list);
				//System.out.println("testdataportlet processAction checklogin success");
			} else {
				session.setAttribute("test.username", null);
				//System.out.println("testdataportlet processAction checklogin failure");
			}
		} else if ("delete".equals(request.getParameter("actionflag"))) {
			int id = Integer.parseInt(request.getParameter("id"));
			TestDataDAOImpl.delete(id);
			System.out.println("testdataportlet processAction delete");
		}
	}

	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(
				NORMAL_VIEW);
		maximizedView = config.getPortletContext().getRequestDispatcher(
				MAXIMIZED_VIEW);
		helpView = config.getPortletContext().getRequestDispatcher(HELP_VIEW);
	}
	
	 public void destroy() {
	        normalView = null;
	        maximizedView = null;
	        helpView = null;
	        super.destroy();
	    }

}

2.所对应的jsp代码如下:
<%@ page session="false" import="java.util.*,javax.portlet.*,com.tortrend.general.*" %>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects/>

<%
	PortletSession session = renderRequest.getPortletSession();
	String username = (String)session.getAttribute("test.username");
	List<TestDataBean> list = (List<TestDataBean>)session.getAttribute("test.list");
	int curp = (Integer)session.getAttribute("test.curp");
	int totalPage = (Integer)session.getAttribute("test.totalPage");
	int lastPage = curp - 1;
	int nextPage = curp + 1;
	//System.out.println("username=" + username);
%>

<DIV style="margin: 12px; margin-bottom: 36px">
	<%
		if (username != null) {
	%>
			Hello <%=username %>.<br><span><a href="<portlet:renderURL>
					<portlet:param name="actionflag" value="adduser"/>
					</portlet:renderURL>">add user</a></span>
		<% if(list.size() > 0) { %>
			<table  border="1" width="80%">
			<% for(TestDataBean tdb : list) { %>
				<tr>
					<td width="60%"><a href="#"><%=tdb.getName() %></a></td>
					<td width="50px;"><a href="<portlet:renderURL>
					<portlet:param name="actionflag" value="update"/>
					<portlet:param name="id" value="<%=String.valueOf(tdb.getId()) %>" />
					</portlet:renderURL>">EDIT</a></td>
					<td width="50px;"><a href="<portlet:renderURL>
					<portlet:param name="actionflag" value="delete"/>
					<portlet:param name="id" value="<%=String.valueOf(tdb.getId()) %>" />
					</portlet:renderURL>">DELETE</a></td>
				</tr>
			<% } %>
			</table>
			<% if(curp > 1) { %>
				<span><a href="<portlet:renderURL>
					<portlet:param name="curp" value="<%=String.valueOf(lastPage) %>"/>
					</portlet:renderURL>">last page</a></span>
			<% } else { %>
				<span>last page</span>
			<% } %>
			<% if(curp < totalPage) { %>
				<span><a href="<portlet:renderURL>
					<portlet:param name="curp" value="<%=String.valueOf(nextPage)  %>"/>
					</portlet:renderURL>">next page</a></span>
			<% } else { %>
				<span>next page</span>
			<% } %>
			
		<% } %>
	<%
		}
	%>
	<FORM method="POST" action="<portlet:actionURL/>">
		<INPUT name="username" type="text"/><br>
		<INPUT name="password" type="password"/><br>
		<INPUT name="actionflag" type="submit" value="Submit"/>
	</FORM>
</DIV>


3.数据库处理文件
package com.tortrend.general;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TestDataDAOImpl {
	private static String DRIVER = "org.gjt.mm.mysql.Driver";
	private static String URL = "jdbc:mysql://127.0.0.1:3307/protal";
	private static String USERNAME = "root";
	private static String PASSWORD = "root";
	private static Connection conn = null;
	private static PreparedStatement pst = null;
	private static ResultSet rs = null;
	
	static {
		try {
			Class.forName(DRIVER);
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public static List<TestDataBean> getAll() {
		List<TestDataBean> list = new ArrayList<TestDataBean>();
		String sql = "select * from test_data";
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			while(rs.next()) {
				TestDataBean tdb = new TestDataBean();
				tdb.setId(rs.getInt("id"));
				tdb.setName(rs.getString("name"));
				tdb.setPassword(rs.getString("password"));
				list.add(tdb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public static List<TestDataBean> getSome(int page, int pagesize) {
		List<TestDataBean> list = new ArrayList<TestDataBean>();
		String sql = "select * from test_data order by id limit ?,?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, (page - 1) * pagesize);
			pst.setInt(2, pagesize);
			rs = pst.executeQuery();
			while(rs.next()) {
				TestDataBean tdb = new TestDataBean();
				tdb.setId(rs.getInt("id"));
				tdb.setName(rs.getString("name"));
				tdb.setPassword(rs.getString("password"));
				list.add(tdb);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public static boolean checkLogin(String name, String password) {
		boolean flag = false;
		String sql = "select name from test_data where name = ? and password = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, name);
			pst.setString(2, password);
			rs = pst.executeQuery();
			if(rs.next()) {
				flag = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	public static int getCount() {
		String sql = "select count(*) num from test_data";
		try {
			pst = conn.prepareStatement(sql);
			rs = pst.executeQuery();
			if(rs.next()) {
				return rs.getInt("num");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}
	
	public static void add(TestDataBean tdb) {
		String sql = "insert into test_data values(?, ?, ?)";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, tdb.getId());
			pst.setString(2, tdb.getName());
			pst.setString(3, tdb.getPassword());
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void update(int id) {
		String sql = "update test_data set name = ? where id = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, "wgj-update");
			pst.setInt(2, id);
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void delete(int id) {
		String sql = "delete from test_data where id = ?";
		try {
			pst = conn.prepareStatement(sql);
			pst.setInt(1, id);
			pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				pst.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

你可能感兴趣的:(sql,编程,jsp,mysql,jdbc)