📄 linkaction.java
字号:
/*
* @(#)ViewAction.java
*
* 创建日期 2005-4-21
* Copyright 2005 东软 国际合作事业部. All rights reserved.
*/
package liyong.action;
import liyong.ConnectionPool.*;
import liyong.model.*;
import liyong.ConnectionPool.DbConnection;
import liyong.form.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.util.ModuleException;
import org.apache.struts.util.MessageResources;
import zhangchunliang.model.AppMode;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
/**
* 这个类用来处理View,ViewForm以及页面中的动作事件
* 它包过添加,删除,更新,查询等
* 动作事件的处理并把处理结果
* 传递给LinkDAO类来进行数据库操作,
* 当动作处理完成后 将要发生跳转,并跳转到指定的页面
*
* @see View
* @see LinkDAO
* @see ViewForm
* @see Connection
* @author 李勇
*/
public final class LinkAction extends Action {
/** 定义日志文件 */
private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");
/** 建立数据池对象 */
DbConnection db = null;
Connection con = null;
/** 构造函数进行类的初始化建立数据池连接 */
/** 处理各种动作事件并跳转到各自的要到的页面 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (!AppMode.getAppMode()) {
System.out.println("Application is terminated...");
return mapping.findForward("appInfo");
}
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
/** 建立会话 */
HttpSession session = request.getSession();
ViewForm m_ViewForm = (ViewForm) form;
/** 建立Connection 对象 */
Connection con = null;
boolean flag = true;
boolean count = false;
int temp1 = 0;
int temp2 = 0;
/** 从这里开始捕获各种动作的异常 */
try {
/** 连接数据库 */
db = new DbConnection();
con = db.getCon();
/** 定义DAO对象,用于实现数据库的各种操作 */
LinkDAO m_ViewDAO = new LinkDAO(con);
/** 得到是那种动作类型 */
String action = request.getParameter("action");
/** 得到数据库查找的方式 */
String search = request.getParameter("search");
/** 参数,这里用于保存记录(行)的fno数据 */
String expression = request.getParameter("expression");
/** 如果没有指定动作类型则动作类型采用默认的数据类型 */
if (action == null)
action = "find";
/** 如果没有指定的查找方式则采用不进行查找方式 */
if (search == null)
search = "UNsearch";
/** 没有指定关键字 */
if (expression == null)
expression = "";
/** 处理中文问题,实现编码转换 */
expression = toChinese(expression);
/** 删除记录操作 */
if ("delete".equals(action)) {
m_ViewDAO.removeID(expression);
}
/** 更新或者是添加记录操作 */
if ("update".equals(action) || "insert".equals(action)) {
View m_View = new View();
/**
* 将在ViewForm中得到的数据 分别赋给View中对应的字段
*
*/
PlaneFno fFno = new PlaneFno();
if (fFno.isFno(toChinese(m_ViewForm.getFno()), "flightcom"))
m_View.setFno(toChinese(m_ViewForm.getFno()));
else
flag = false;
PlaneFcity fFcity = new PlaneFcity();
/** 获得更改的城市名 */
if (fFcity.isFcity(toChinese(m_ViewForm.getCityname())))
m_View.setCityname(toChinese(m_ViewForm.getCityname()));
else
flag = false;
/** 获得更改的时间 */
m_View.setCtime(toChinese(m_ViewForm.getCtime()));
m_View.setGtime(toChinese(m_ViewForm.getGtime()));
/**
* 权限
*
* @param userUpdateGrant
* 对表进行更新操作的权限
*/
String userUpdateGrant = (String) request.getSession()
.getAttribute("userUpdateGrant");
if (userUpdateGrant == "Y")
temp1 = 1;
if (userUpdateGrant == "N")
temp1 = 2;
/** 调用DAO对象更新记录 */
if (("update".equals(action)) && (flag == true) && (temp1 == 1))
m_ViewDAO.update(m_View, expression);
/** 调用DAO对象添加记录 */
if ("insert".equals(action))
m_ViewDAO.create(m_View);
}
/** 查询要处理的sql语句 */
String sql = "select fno,cityname,ctime,gtime from city";
/** 查询记录 */
if ("search".equals(search)) {
//<search>
sql += " where ";
sql += "fno";
sql += "='";
/** 如果没有制定关键字的话采用下面的方法进行查询 */
if ("NULL".equals(expression) || "".equals(expression)) {
sql += m_ViewForm.getFno();
}/** 如果指定了关键字则采用关键字的查询方式 */
else {
sql += expression;
}
sql += "'";
}
/** 设置每页显示的记录数 */
m_ViewDAO.setLength(6);
/** 当前显示页 */
int ipage;
try {
/** 分页参数,读取请求的当前页 */
String page = request.getParameter("page");
ipage = java.lang.Integer.parseInt(page, 10);
} catch (Exception e) {
ipage = m_ViewForm.getPage();
}
/** 如果没有找到数据显示为0个数据时则页面显示为1页 */
if (ipage < 1)
ipage = 1;
/** 通过DAO对象查询数据 */
/**
* 权限
*
* @param userSeleteGrant
* 对表进行浏览操作的权限
*/
String userSelectGrant = (String) request.getSession()
.getAttribute("userSelectGrant");
/** 通过DAO对象查询数据 */
if (userSelectGrant == "Y") {
temp2 = 1;
Collection col = m_ViewDAO.findSQL(sql, ipage);
/** 保存数据 */
request.setAttribute("View", col);
}
/** 分页字符串,分页内容 */
String pagestr = m_ViewDAO.getPagestr(ipage);
String s_find, str;
/**
* 查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。
* 如果是全部显示则去掉"search=search&"
*/
if (!"search".equals(search)) {
s_find = "search=search&";
while (pagestr.indexOf(s_find) != -1) {
str = pagestr.substring(0, pagestr.indexOf(s_find));
str += pagestr.substring(pagestr.indexOf(s_find)
+ s_find.length(), pagestr.length());
pagestr = str;
}
}
m_ViewForm.setPagestr(pagestr);
m_ViewForm.setAction(action);
request.setAttribute("ViewForm", m_ViewForm);
/** 返回一个字符串进行跳转 */
/** 返回一个字符串进行跳转 */
/**
* 权限
*
* @param userTableName
* 对表进行操作的权限
*/
String userTableName = (String) request.getSession().getAttribute(
"userTableName");
String str1 = "";
int temp3 = 0;
int temp4 = 0;
for (int m = 0; m < userTableName.length(); m++) {
str1 = userTableName.substring(m, m + 1);
if (";".equals(str1)) {
if ("flight".equals(userTableName.substring(temp3, m)))
temp4++;
if ("city".equals(userTableName.substring(temp3, m)))
temp4++;
temp3 = m + 1;
}
}
if ((temp2 == 1) && (temp1 == 0))
count = true;
if (temp1 == 2)
count = false;
if ((temp1 == 1) && (temp2 == 1) && (temp4 == 2))
count = true;
if (temp2 == 0)
count = false;
if ((flag == true) && (count == true))
return mapping.findForward("success");
else if (count == false)
return mapping.findForward("filter");
else
return mapping.findForward("error");
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Unable to get connection.");
} finally {
try {
/** 关闭数据库连接 */
if (con != null)
con.close();
} catch (SQLException e) {
throw new RuntimeException(e.getMessage());
}
}
}
/** 处理中文问题,实现编码转换 */
public String toChinese(String ss) {
if (ss != null) {
try {
String temp_p = ss;
byte[] temp_t = temp_p.getBytes("ISO-8859-1");
ss = new String(temp_t);
} catch (Exception e) {
System.err.println("toChinese exception:" + e.getMessage());
System.err.println("The String is:" + ss);
}
}
return ss;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -