开发者学堂课程【Java Web开发系列课程 - Struts2框架入门:OGNL 处理关联表】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/537/detail/7324
OGNL 处理关联表
目录:
一.数据库与 Java 的关系
二.Vip.代码
三.User.代码
四.UserDao.代码
五.VipDao代码
六.VipService.代码
七.UserAction.代码
八.List 代码
九.用 map 方法进行修改
一、数据库与 Java 的关系
user 表和 VIP 表属于多对一的关系,多个 user 对应一个 vip 等级,外键关系的建立不影响 Java 查询,两者的关系只是一个数据库的关系。
二.Vip.代码
Package cn.sxt.entity
public class Vip{
private int id;
private String name;
public Vip(){
}
public Vip(int id,String name){
super();
this.id=id;
this.name=name;
}
public int getId(){
return id;
}
三.User.代码
public class User{
private int id;
private String name;
private String pwd;
private int vipId;
Private vip vip;
public User(){
}
Public User(int id,String name,String pwd,int vipId){
super();
this.id=id;
this.name=name;
this.pwd=pwd;
this.vipId=vipId;
}
Public User(int id,String name,String pwd,int vipId){
super();
this.id=id;
this.name=name;
this.pwd=pwd;
this.vipId=vipId;
}
Public User(int id,String name,String pwd){
super();
this.id=id;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
Public String getName(){
this.name=name;
Public String getpwd(){
return pwd;
Public void setPwd(String pwd){
this.pwd=pwd;
四.UserDao.代码
查询多一列:list.add(new User(rs.getInt(1),rs.getstring(2),rs.getstring(3),rs.getint(4)));
五.VipDao 代码
Package cn.sxt.dao;
i
mport java.
sql.resultset.
i
mport java.util.ArrayList;
i
mport java.util.List;
i
mport cn.sxt.entity.Vip;
Public class VipDao extends BaseDao{
Public List getList(){
String sql=“select*from t
vip”;
Listlist=new ArrayList();
ResultSet rs=this.executeQuery(sql);
While(rs.next()){
list.add(new Vip(rs.getInt(1),rs.getString(2));
catch(SQLException e)
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.close();
return list.size()>0?list:null;
Public Map getMap(){
String sql=“select*from t vip”;
Mapmap=new HashMap();
ResultSet rs=this.executeQuery(sql);
While(rs.next()){
map.put(rs.getInt(1),rs.getString(2));
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
finally{
this.close();
return map.size()>0?map:null;
六.VipService.代码
Package cn.sxt.service;
i
mport java.util.List;
i
mport java.util.Map;
i
mport cn.sxt.dao.VipDao;
i
mport cn.sxt.entity.Vip;
Public class VipService{
private VipDao vipDao=new VipDao();
Public List getList(){
return vipDao.getList();
public Map getMap(){
return vipDao.getMap();
七.UserAction.代码
package cn.sxt.action;
import java.util.List;
public class UserAction
private User user;
private List list;
private List vip
List;
private UserService userService = new UserService();
Private vipservice vipservice =new vipservice();
//登录
public String login()
User temp=userService.login(user);
if(temp!=null)
//list = userService.getList();
return Action.SUCCESS;
//根据 id
查询
用
户
然后跳特到 update.jsp
public String toUpdate()[
user = userService. getById(user.getId());returnAction.SUCCESS;
//更新用户
public String update()
intresult=userService.update(user)
;
if(result>0)
return Action. sUCCESS;
lrLr'esull=user'ser'vice.upuale(user');
if(result>0){
return Action.SUCCESS;
public User getUser()
return user;
public void setUser(User user)
this.user = user;
public List getList()
return list;
public void setList(List list)
this.list = list;
//
获取用户列表
public String list()
list=userService.getList();
vipList=vipService.getList();
map = vipseryice.getMap(;
return Action.SUCCESS;
八.List 代码
<%@ page language="java"import="java.util.*"pageEncoding="UTF-8"
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+reql6 %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href= "<%=basePath%>">
<title>My JSP 'index.jsp'starting page</title>
<meta http-equiv= "pragma" content= "no-cache">
<table width= "80%" align= "center"<tr>
<td>
编号
</td〉
<td>姓名</td>
<td>
密码
</td〉
<td>
会员等级
</td>
<td>操作</td〉
</tr>
<c:forEach items="${list }”var="bean">
<tr>
<s:iterator value="List">
<tr>
<td><s:property value="
#bean.
Id"/></td>
<td><s:property value="
#bean.name
"/></td>
<td><s:property value="
#bean.pwd
"/></td>
<td><a href= "toUpdate
?
user.id=<s:property value="id"/>">
修改
</a></td></tr>
<s:iterator value="vipList">
<s:if test="#bean.vipId==id">
<s:property value="name"/ >
<s : property value= "map[ #bean. vipId].value" / >
</s:if>
</s:iterator>
</table>
</body>
运行结果
九.用 map 方法进行修改
(1)修改 UserAction 代码
public class UserAction
private User user;
private List<User> list;
private List<Vip> vipList;
private Map<Integer,string> map;
private UserService userService = newUserService();
private VipService vipService = newVipService();
//获取用户列表
public String list()
list = userService.getList();//vipList=vipService.getList();
map = vipService.getMap();
return Action.SUCCESS;
(2)修改 List 代码
<%--<s:iterator value="vipList">
<s:if test="#bean.vipId==id">
<s:property value= "name"/>
</s:if>
</s: iterator>--%>
<s:property value= "map[#bean.vipId]"/>
(3)修改 update 代码
用户名:
<input type="text"name="user.name"value=”<s:property value="<s:property value= "user.name"/>"/><br>
密码:
<input type="password"name="user.pwd"value="<s:property value
=
"<s:property value= "user.pwd"/ >'/><br)
会员等级:
<select name="user.vipId"
<s:iterator value="map">
<option<s:if
test="user.vipId==key">selected</s:if>value"<s:propertyvalue="key"/>"><s:property
value "key"/>"><s:property value"
</s:iterator>
</select><br>
运行结果