📄 testaction.java
字号:
package emptyprj;
import javawebstudio.struts_db.ConnectionPool;
import emptyprj.TestDAO;
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 javawebstudio.struts_db.DbAction;
//import javawebstudio.struts_db.DbMySQLAction; //如果是MySQL,请用这一句。
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
//public final class TestAction extends DbMySQLAction //如果是MySQL,请用这一句。
public final class TestAction extends DbAction
{
// 变量定义:
private Log log = LogFactory.getLog("org.apache.struts.webapp.Example");
private ConnectionPool pool;
public TestAction() {
pool = ConnectionPool.getInstance();
}
//定义方法find,查找数据:
public ActionForward find(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// 首先定义属性和变量以及设置初值
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TestForm m_TestForm = (TestForm) form;
Connection con = null;
try
{
con = pool.getConnection();
TestDAO m_TestDAO = new TestDAO(con); //定义DAO对象,用于实现数据库的各种操作
String action=request.getParameter("action"); //动作类型
String search=request.getParameter("search"); //数据库查找方式
String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据
if(action==null)action="find";
if(search==null)search="UNsearch";
if(expression==null)expression="";
//action=toChinese(action); //处理中文问题,实现编码转换,如果是用于MySQL,请加上这之一句
expression=toChinese(expression); //处理中文问题,实现编码转换,如果是用于MySQL,请去掉之一句
///////////////查找数据库
//<sqlstr>
String sql="SELECT * FROM dbo.员工奖金";
//</sqlstr>
////SQL字符处理
if("search".equals(search))
{
//<search>
sql+=" where 姓名='";
if("NULL".equals(expression)||"".equals(expression)) sql+=m_TestForm .get姓名();
else sql+=expression;
sql+="'";
//</search>
}
//<分页>
m_TestDAO.setLength(4);//设置每页显示记录数
int ipage;//当前页
try
{
String page=request.getParameter("page");//分页参数,读取请求的当前页
ipage=java.lang.Integer.parseInt(page,10);
}
catch (Exception e)
{
ipage=m_TestForm.getPage();
}
if(ipage<1)ipage=1;
Collection col = m_TestDAO.findSQL(sql,ipage);//通过DAO对象查询数据
request.setAttribute("Test",col); //保存数据
String pagestr=m_TestDAO.getPagestr(ipage);//分页字符串,分页内容
if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"
{
pagestr=pagestr.replaceAll("search=search&","");
}
m_TestForm.setPagestr(pagestr);
m_TestForm.setAction(action);
request.setAttribute("TestForm",m_TestForm);
//</分页>
//转发到输出页面
return mapping.findForward("success");
}
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());
}
}
}
//定义方法remove,删除记录:
public ActionForward remove(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// 首先定义属性和变量以及设置初值
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TestForm m_TestForm = (TestForm) form;
Connection con = null;
try
{
con = pool.getConnection();
TestDAO m_TestDAO = new TestDAO(con); //定义DAO对象,用于实现数据库的各种操作
String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据
if(expression==null)expression="";
expression=toChinese(expression); //处理中文问题,实现编码转换
m_TestDAO.removeID(expression);
}
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());
}
}
///////////////查找数据库
return find( mapping,form,request,response);
}
//定义方法update,更新记录:
public ActionForward update(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// 首先定义属性和变量以及设置初值
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TestForm m_TestForm = (TestForm) form;
Connection con = null;
try
{
con = pool.getConnection();
TestDAO m_TestDAO = new TestDAO(con); //定义DAO对象,用于实现数据库的各种操作
String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据
if(expression==null)expression="";
expression=toChinese(expression); //处理中文问题,实现编码转换
Test m_Test= new Test();
//<update>
m_Test.set姓名(m_TestForm.get姓名());
m_Test.set性别(m_TestForm.get性别());
m_Test.set年龄(m_TestForm.get年龄());
m_Test.set时间(m_TestForm.get时间());
m_Test.set奖金(m_TestForm.get奖金());
//</update>
m_TestDAO.update(m_Test,expression);
}
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());
}
}
///////////////查找数据库
return find( mapping,form,request,response);
}
//定义方法insert,添加新记录:
public ActionForward insert(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// 首先定义属性和变量以及设置初值
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TestForm m_TestForm = (TestForm) form;
Connection con = null;
try
{
con = pool.getConnection();
TestDAO m_TestDAO = new TestDAO(con); //定义DAO对象,用于实现数据库的各种操作
Test m_Test= new Test();
//<insert>
m_Test.set姓名(m_TestForm.get姓名());
m_Test.set性别(m_TestForm.get性别());
m_Test.set年龄(m_TestForm.get年龄());
m_Test.set时间(m_TestForm.get时间());
m_Test.set奖金(m_TestForm.get奖金());
//</insert>
m_TestDAO.create(m_Test);
}
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());
}
}
///////////////查找数据库
return find( mapping,form,request,response);
}
/**
* Provides the mapping from resource key to method name.
*
* @return Resource key / method name map.
*/
// protected abstract Map getKeyMethodMap();
public Map getKeyMethodMap()
{
Map map=new HashMap();
map.put("button.remove","remove");
map.put("button.update","update");
map.put("button.insert","insert");
map.put("button.search","find");
map.put("button.link","find");
map.put("button.edit","find");
map.put("button.find","find");
map.put("button.showall","find");
map.put("button.cancel","find");
map.put("remove","remove");
map.put("update","update");
map.put("insert","insert");
map.put("search","find");
map.put("link","find");
map.put("edit","find");
map.put("find","find");
map.put("showall","find");
map.put("cancel","find");
map.put("button.proc","proc");
return map;
}
/////////如果资源文件还未包含下列键值,请把下面18个键值拷贝到资源文件中,采选择“生成”--“编码转换”菜单进行编码转换!!
/*
button.remove=删除
button.update=更新
button.insert=添加
button.search=搜索
button.link=链按
button.edit=编辑
button.find=查找
button.cancel=取消
button.showall=全部显示
remove=remove
update=update
insert=insert
search=search
link=link
edit=edit
find=find
cancel=cancel
showall=showall
*/
//定义方法proc,调用存储过程:
public ActionForward proc(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception
{
// 首先定义属性和变量以及设置初值
Locale locale = getLocale(request);
MessageResources messages = getResources(request);
HttpSession session = request.getSession();
TestForm m_TestForm = (TestForm) form;
Connection con = null;
try
{
con = pool.getConnection();
TestDAO m_TestDAO = new TestDAO(con); //定义DAO对象,用于实现数据库的各种操作
String action=request.getParameter("action"); //动作类型
String search=request.getParameter("search"); //数据库查找方式
String expression=request.getParameter("expression"); //参数,这里用于保存记录(行)的ID数据
if(action==null)action="find";
if(search==null)search="UNsearch";
if(expression==null)expression="";
//action=toChinese(action); //处理中文问题,实现编码转换,如果是用于MySQL,请加上这之一句
expression=toChinese(expression); //处理中文问题,实现编码转换,如果是用于MySQL,请去掉之一句
///////////////查找数据库
String sql= "{call dbo.myProcTest}";
//<分页>
m_TestDAO.setLength(4);//设置每页显示记录数
int ipage;//当前页
try
{
String page=request.getParameter("page");//分页参数,读取请求的当前页
ipage=java.lang.Integer.parseInt(page,10);
}
catch (Exception e)
{
ipage=m_TestForm.getPage();
}
if(ipage<1)ipage=1;
Collection col = m_TestDAO.proc(sql,ipage);//通过DAO对象查询数据
request.setAttribute("Test",col); //保存数据
String pagestr=m_TestDAO.getPagestr(ipage);//分页字符串,分页内容
if(!"search".equals(search)) //查找和全部显示两个不同的操作,其分页字符串不同,在此进行处理。如果是全部显示则去掉"search=search&"
{
pagestr=pagestr.replaceAll("search=search&","");
}
m_TestForm.setPagestr(pagestr);
m_TestForm.setAction(action);
request.setAttribute("TestForm",m_TestForm);
//</分页>
//转发到输出页面
return mapping.findForward("success");
}
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());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -