📄 instructoraction.java
字号:
package com.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.dao.Constants;
import com.dao.DaoSupport;
import com.dao.PageBean;
import com.dao.UploadBean;
import com.form.InstructorForm;
public class InstructorAction extends DispatchAction {
private InstructorForm instructorform = null;
private DaoSupport dao = null;
/*
* 添加技术支持信息情况
*/
public ActionForward addinstructor(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("GB2312");
instructorform = (InstructorForm)form;
System.out.println("instructorform:" + instructorform.getTitle());
String dir = servlet.getServletContext().getRealPath("/uploadImage");
UploadBean upb = new UploadBean();
try {
//通过该方法上传图片并将图片名称及大小存放到form中
if(!upb.upload(dir, instructorform,"instructorForm")){
request.setAttribute("name", "overSize");
return mapping.findForward("addsucess");
}
} catch (Exception ex) {
System.out.println("------在上传图片时抛出异常,内容如下:");
ex.printStackTrace();
}
String addSql = "insert into tb_Instructor(title,imagepath) values (";
addSql = addSql + "'" + instructorform.getTitle().trim() + "','" + upb.getFileName(instructorform,"instructorForm") + "')";
System.out.println("addSql:" + addSql);
dao.executeSql(addSql);
request.setAttribute("name", "instructoradd");
return mapping.findForward("addsucess");
}
//修改技术信息
public ActionForward updateinstructor(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
request.setCharacterEncoding("GB2312");
instructorform = (InstructorForm)form;
System.out.println("title"+instructorform.getTitle());
String dir = servlet.getServletContext().getRealPath("/uploadImage");
UploadBean upb = new UploadBean();
try {
//通过该方法上传图片并将图片名称及大小存放到form中
upb.upload(dir, instructorform,"instructorForm");
} catch (Exception ex) {
System.out.println("------在上传图片时抛出异常,内容如下:");
ex.printStackTrace();
}
String addSql = "update tb_Instructor set title = '" + instructorform.getTitle().trim()
+ "',imagepath ='" + upb.getFileName(instructorform,"instructorForm") + "' where ID = " + instructorform.getId();
System.out.println("addSql:" + addSql);
dao.executeSql(addSql);
request.setAttribute("name", "instructorupdate");
return mapping.findForward("modiupdateinstructors");
}
//获取所有的技术信息
public ActionForward listAllinstructor(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println("lislistAllinstructor");
String selectSql = "Select * from tb_Instructor;";
List listinstructors = dao.getObjectList(selectSql);
request.setAttribute("ListInstructors", listinstructors);
String name = request.getParameter("name");
if (name.equals("modi")) {
return mapping.findForward("listallinstructorsmodi");
}else if (name.equals("dele")){
return mapping.findForward("listallinstructorsdele");
}else{
return null;
}
}
//根据id查询出技术信息
public ActionForward listInstructor(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception{
String id = request.getParameter("id");
String selectSql = "Select * from tb_Instructor where ID = '" + id + "'" ;
System.out.println("selectSql:" + selectSql);
List listinstructors = dao.getObjectList(selectSql);
request.setAttribute("Instructors", listinstructors);
return mapping.findForward("listinstructors");
}
//删除技术信息
public ActionForward delInstructor(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception{
String id = request.getParameter("id");
String selectSql = "delete from tb_Instructor where ID = '" + id + "'" ;
System.out.println("selectSql:" + selectSql);
dao.executeSql(selectSql);
request.setAttribute("name", "instructordele");
return mapping.findForward("delelistinstructors");
}
// 控制动态分页
public ActionForward view(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("GB2312");
/* 获取参数值:跳转到的页码编号 */
int curPage;
String pageNum = request.getParameter("jumpPage");
if (pageNum == null || pageNum.equals("")) {
curPage = 1;
} else {
curPage = Integer.parseInt(pageNum);
}
log.info("过去所有的记录");
int maxRowCount = dao.totalPage("SELECT count(*) FROM tb_instructor");
log.info(" view():页面跳转第" + curPage + "页!!!");
try {
log.info("将查询的值存入request中!");
if(curPage>1){
PageBean pb=dao.getPageBean(
Constants.Instructor_sql1, Constants.Instructor_sql2,
Constants.Instructor_sql3, Constants.Instructor_sql4,Constants.Instructor_sql5, Constants.Instructor, curPage,
maxRowCount);
System.out.println(pb.getMaxRowCount());
request.setAttribute("PageBean", pb);
}else{
String sql = "SELECT top " + Constants.PAGE_LENGTH + " ID, title, imagepath FROM tb_instructor";
PageBean pb=dao.getPageBean1(sql, curPage, maxRowCount);
request.setAttribute("PageBean", pb);
}
} catch (Exception e) {
System.out.println("view " + e.getMessage());
}
return mapping.findForward("instructor");
}
public DaoSupport getDao() {
return dao;
}
public void setDao(DaoSupport dao) {
this.dao = dao;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -