---
title: "J2EE开发练手（第一章）"
author: "tangxininjava"
date: "2013-02-07T09:30:20Z"
tags: []
categories: ["编程语言"]
slug: "j2ee"
draft: false
lastmod: "2013-02-07T16:03:01Z"
id: 1797198
url: "https://tangxininjava.iteye.com/blog/1797198"
---

![](http://dl.iteye.com/upload/attachment/0080/3712/97d683aa-4799-317d-8143-0b526d21f9d5.jpg)

 

根据类图建立表

```
CREATE TABLE `account` (
  `account_id` bigint(20) NOT NULL,
  `name` varchar(255) COLLATE utf8_bin NOT NULL,
  `currency` varchar(100) COLLATE utf8_bin NOT NULL,
  `balance` varchar(255) COLLATE utf8_bin NOT NULL,
  `customer_id` bigint(20) NOT NULL,
  PRIMARY KEY (`account_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
```

 

   

```
CREATE TABLE `privilege` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
```

 

  

```
CREATE TABLE `role` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
```

  

   

```
CREATE TABLE `user` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
```

   
    通过rapid-framework代码生成器生成dao代码 具体请查看[http://code.google.com/p/rapid-framework/](http://code.google.com/p/rapid-framework/)

   代码结构如下：

 

      
![](http://dl.iteye.com/upload/attachment/0080/3717/2adbef91-8f2e-380d-ae03-6a4e2740101e.jpg)  
 

    因为意图是做一个权限，权限系统一个有个树形结构是吧，先去找找看什么合适。

   思考：一个用户登录加载他的角色角色加载权限，权限一般和菜单关联的，没权限怎么弄，所以还是先做后台。  manager。

 

   先不管界面好看不，后续再优化界面，先做第一个小模块，添加用户。   

 

  建立一个edit.jsp页面

   

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/编辑用户</title>
</head>
<body>

<form action="${ctx}/user/edit.action" method="post">
	
</form>



</body>
</html>
```

 

    这里使用${ctx} 的意图是想获取jsp上下文，不想使用用脚本想引入jstl忘记指令了，只接的&lt;%@ include%&gt;  , &lt;%@ page%&gt; 于是www.baidu.com.

  

   
![](http://dl.iteye.com/upload/attachment/0080/3719/319caf31-cc4f-30b4-a062-7d0c78dad9f7.jpg)

 

只用eclipse中的代码智能提示，(小技巧光标要停止在@后面中间不要有空格)发现还有个taglib指令。

想在tld文件中复制&lt;[%@taglib](mailto:%@taglib) uri="" prefix="c" %&gt;来填写这个晕找不到了，以前用myeclipse很方便，现在用eclipse少了一些库包找不到。于是[www.baidu.com](http://www.baidu.com) jstl.

 

 这是jsp的标准标签库，因为忘记  
![](http://dl.iteye.com/upload/attachment/0080/3724/863d81db-1492-37a7-bb90-faf4104aa694.jpg)

 

  打开里面的tld复制  
 

 我想写个公用的${ctx}上下文，代码如下老是报错，我记得以前就是这样写的：

 

  

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
```

  

 

    后来发现我的uri错了，我复制了c-1\_0.tld应该复制c.tld中的uri郁闷，改之后如下：

  

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
```

 

   

   然后做一个页面user目录下的edit.jsp

   edit.jsp

   

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/commons/taglib.jsp" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/编辑用户</title>
</head>
<body>

<form action="${ctx}/user/edit.action" method="post">
    
	用户名：<input name="user.name" id="name" type="text">
<br/>
	<input type="submit" value="提交">
	
</form>



</body>
</html>
```

 

   

    然后struts2建一个actin

   打死都添加不了报错说：不能为property id设置1 继续游不能设置成2,3,4,5这样的于是我检查下ibatis自动生成的配置文件如下：

 

  

```
 <insert id="User.insert" >
    <![CDATA[
        INSERT INTO user (
        	Id ,
        	name 
        ) VALUES (
        	#id# ,
        	#name# 
        )
    ]]>
    <!--
    mysql:		type="post" SELECT  select last_insert_id()
    sqlserver:	type="post" SELECT  @@identity  AS  ID
    oracle:		type="pre" 	SELECT  sequenceName.nextval AS ID FROM DUAL
    DB2:		type="pre"	values nextval for sequenceName
    -->
        <selectKey resultClass="java.lang.Long" type="post" keyProperty="id" >
            select last_insert_id()
        </selectKey>
    </insert>
```

     声明这个id对应数据库（我用的mysql）中列id（int(11)）然后自动生成的属性有时Integer的，而这里的ibatis配置文件确对应是Long，于是我改成Integer了，修改之后：

  

   

```
 <insert id="User.insert" >
    <![CDATA[
        INSERT INTO user (
        	Id ,
        	name 
        ) VALUES (
        	#id# ,
        	#name# 
        )
    ]]>
    <!--
    mysql:		type="post" SELECT  select last_insert_id()
    sqlserver:	type="post" SELECT  @@identity  AS  ID
    oracle:		type="pre" 	SELECT  sequenceName.nextval AS ID FROM DUAL
    DB2:		type="pre"	values nextval for sequenceName
    -->
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id" >
            select last_insert_id()
        </selectKey>
    </insert>
```

 

    改了之后就可以保存到数据库了，所以代码生成工具也不是万能的，凡是还是小心的好。

 

     用户可以保存了，在来个查询用户列表的页面，就userList.jsp吧

 

    

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/commons/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
</head>
<body>
<table>
	<thead>
		<tr>
			<th>用户列表</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${page.result}" var="x">
			<tr>
                <td>用户Id</td><td>${x.id }</td>
				<td>用户名</td><td>${x.name }</td>
			</tr>
		</c:forEach>
	</tbody>
	<tfoot></tfoot>
</table>


</body>
</html>
```

 

 

    查询应该有个分页吧。

 

     找个分页插件来用用。

   

      用的是rapid-framework中的分页插件，用jsp tag file写的，不错。

      用别人的东西还是引入作者或连接

     

        
![](http://dl.iteye.com/upload/attachment/0080/3827/3aa45116-7468-3f19-8dff-f9cec586817c.jpg)  
 

    

     下面是jsp页面引入如何使用：

    

       userList.jsp

      

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="tang" tagdir="/WEB-INF/tags/simpletable"%>
<%@ include file="/commons/taglib.jsp"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户列表</title>
<script type="text/javascript">
	function togglePage(pageIndex){
		var index = parseInt(pageIndex);
		document.getElementById("pageNumber").value = index;
		var count = document.getElementById("pageSelect").value;
		document.getElementById("count").value = parseInt(count);
		document.listForm.submit();
		
	}
</script>


</head>
<body>
<form action="${ctx}/user/query.action" name="listForm" method="post">
	<input type="hidden" id="pageNumber" name="userQuery.pageNumber" value="${userQuery.pageNumber}">
   <input type="hidden" id="count" name="userQuery.pageSize" value="${userQuery.pageSize}">
</form>
<table>
	<thead>
		<tr>
			<th>用户列表</th>
		</tr>
	</thead>
	<tbody>
		<c:forEach items="${page.result}" var="x">
			<tr>
				<td>用户Id</td>
				<td>${x.id }</td>
				<td>用户名</td>
				<td>${x.name }</td>
			</tr>
		</c:forEach>
		<tr>
			<td colspan="4"><tang:pageToolbar page="${page}"></tang:pageToolbar></td>
		</tr>
	</tbody>
	<tfoot></tfoot>
</table>


</body>
</html>
```

 

    

    现在又想把用户列表和添加和用户放在一起，用什么呢？突然想到iframe，好久没用了，维护系统一年了，后台代码写多了，html代码忘光了，不要紧捡回来。

 

    于是在edit.jsp中添加几行代码如下：

   

```
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/commons/taglib.jsp"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加/编辑用户</title>
</head>
<body>

<form action="${ctx}/user/edit.action" method="post">用户名：<input
	name="user.name" id="name" type="text"> <input type="submit"
	value="提交"></form>

<iframe src="${ctx}/user/query.action" width="500" height="300"></iframe>




</body>
</html>
```

 

    效果图如下：

   

      
![](http://dl.iteye.com/upload/attachment/0080/3832/bba9e77c-fb72-3220-b089-0bcae63ff78d.jpg)