`
xxp3369
  • 浏览: 151319 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

ssh_training_itemmgr 03

阅读更多
DrpExceptionHandler.java

package com.bjsxt.drp.web.util;

import org.apache.struts.action.ExceptionHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.config.ExceptionConfig;

import com.bjsxt.drp.business.util.AppException;
import com.bjsxt.drp.business.util.AppException;

/**
 * 个性化异常处理类
 */
public class DrpExceptionHandler extends ExceptionHandler {
    public ActionForward execute(
            Exception ex,
            ExceptionConfig ae,
            ActionMapping mapping,
            ActionForm formInstance,
            HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException {
        
        if (!(ex instanceof AppException )) {
            return super.execute(ex, ae, mapping, formInstance, request, response);
        }
        
        AppException  errCodeEx =(AppException) ex;
        ActionForward forward = null;
        ActionMessage error = null;
        String property = null;

        // Build the forward from the exception mapping if it exists
        // or from the form input
        if (ae.getPath() != null) {
            forward = new ActionForward(ae.getPath());
        } else {
            forward = mapping.getInputForward();
        }
        
        //----------处理个性化异常-------------
        AppException ece =(AppException) ex;
        String errorCode =ece.getErrorCode();
        error = new ActionMessage(errorCode, ece.getArgs());
        property = error.getKey();
        //-----------------------------------
        
        logException(ex);

        // Store the exception
        request.setAttribute(Globals.EXCEPTION_KEY, ex);
        storeException(request, property, error, forward, ae.getScope());

        return forward;
    }

}



LoginActionForm.java


package com.bjsxt.drp.web.itemmgr.forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

/**
 * 收集表单数据的ActionForm,ActionForm中的属性必须与html中表单中的name名称一致
 * @author Administrator
 *
 */
public class LoginActionForm extends ActionForm {

	private String username;
	
	private String password;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}



ItemActionForm.java

package com.bjsxt.drp.web.itemmgr.forms;

import org.apache.struts.action.ActionForm;
import org.apache.struts.upload.FormFile;

public class ItemActionForm extends ActionForm {

    //物料代码
	private String itemNo;
	
	//物料名称
	private String itemName;
	
	//规格
	private String spec;
	
	//型号
	private String pattern;
	
	//类型
	private String categoryId;
	
	//单位
	private String unitId;
	
	//收集物料代码,主要用户批量删除
	private String[] selectFlag;
	
	//查询条件
	private String clientIdOrName;
	
	//上传的文件名
	private FormFile fileName;
	
	//第几页
	private int pageNo;
	
	//每页多少条
	private int pageSize;

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public String getItemNo() {
		return itemNo;
	}

	public void setItemNo(String itemNo) {
		this.itemNo = itemNo;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public String getSpec() {
		return spec;
	}

	public void setSpec(String spec) {
		this.spec = spec;
	}

	public String[] getSelectFlag() {
		return selectFlag;
	}

	public void setSelectFlag(String[] selectFlag) {
		this.selectFlag = selectFlag;
	}

	public String getClientIdOrName() {
		return clientIdOrName;
	}

	public void setClientIdOrName(String clientIdOrName) {
		this.clientIdOrName = clientIdOrName;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getUnitId() {
		return unitId;
	}

	public void setUnitId(String unitId) {
		this.unitId = unitId;
	}

	public FormFile getFileName() {
		return fileName;
	}

	public void setFileName(FormFile fileName) {
		this.fileName = fileName;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}	
}


BaseAction.java

package com.bjsxt.drp.web.itemmgr.actions;

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;

public class BaseAction extends DispatchAction {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (request.getSession().getAttribute("user") == null) {
			return mapping.findForward("index");
		}
		
		return super.execute(mapping, form, request, response);
	}

}


ChangeLanguageAction.java


package com.bjsxt.drp.web.itemmgr.actions; 

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class ChangeLanguageAction extends Action {
	
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
    	String lan = request.getParameter("lan");
    	Locale currentLocale = null;
    	if ("en".equals(lan)) {
    		currentLocale = new Locale("en", "US");
    	}else if ("zh".equals(lan)) {
    		currentLocale = new Locale("zh", "CN");
    	}
    	request.getSession().setAttribute(Globals.LOCALE_KEY, currentLocale);
    	return mapping.findForward("success");
    } 	
}


ItemAction.java

package com.bjsxt.drp.web.itemmgr.actions;

import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.bjsxt.drp.business.itemmgr.manager.ItemManager;
import com.bjsxt.drp.business.itemmgr.model.Item;
import com.bjsxt.drp.business.itemmgr.model.ItemCategory;
import com.bjsxt.drp.business.itemmgr.model.ItemUnit;
import com.bjsxt.drp.business.util.PageModel;
import com.bjsxt.drp.web.itemmgr.forms.ItemActionForm;

/**
 * 统一处理所有的请求
 * @author Administrator
 *
 */
public class ItemAction extends BaseAction {

	private ItemManager itemManager;
	
	private String uploadPath;
	
	/**
	 * 如果没有传递任何标识参数(如command参数),则默认调用unspecified方法
	 */
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		System.out.println("ItemAction=>>unspecified()");
		ActionForward listActionForward = new ActionForward("/index.jsp", true);
		return listActionForward;
	}

	/**
	 * 添加物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//创建Item实体对象,并将ActionForm中的内容设置到Item对象中
		Item item = new Item();
		
		//将属性值从ItemActionForm对象拷贝到Item对象
		BeanUtils.copyProperties(item, iaf);
		
		//设置ItemCategory
		ItemCategory ic = new ItemCategory();
		ic.setId(iaf.getCategoryId());
		item.setCategory(ic);
		
		//设置itemUnit
		ItemUnit iu = new ItemUnit();
		iu.setId(iaf.getUnitId());
		item.setUnit(iu);
		
		//调用业务逻辑操作
		itemManager.addItem(item);
		ActionForward af = new ActionForward("item.do?command=list&pageNo=" + 
				                              iaf.getPageNo() + 
				                              "&pageSize=" + iaf.getPageSize(), true);
		return af;
	}

	/**
	 * 删除物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//调用业务逻辑操作
		itemManager.deleteItem(iaf.getSelectFlag());
		ActionForward af = new ActionForward("item.do?command=list&pageNo=" + 
                iaf.getPageNo() + 
                "&pageSize=" + iaf.getPageSize(), true);
		return af;
	}

	/**
	 * 根据物料代码查询需要修改的物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward modifyDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//调用业务逻辑操作
		Item item = itemManager.findItemById(iaf.getItemNo());
		
		//将查询结果放到request中
		request.setAttribute("item", item);
		return mapping.findForward("modify_detail");
	}

	/**
	 * 修改物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward modify(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//创建Item实体对象,并将ActionForm中的内容设置到Item对象中
		Item item = new Item();
		
		//将属性值从ItemActionForm对象拷贝到Item对象
		BeanUtils.copyProperties(item, iaf);
				
		//设置ItemCategory
		ItemCategory ic = new ItemCategory();
		ic.setId(iaf.getCategoryId());
		item.setCategory(ic);
		
		//设置itemUnit
		ItemUnit iu = new ItemUnit();
		iu.setId(iaf.getUnitId());
		item.setUnit(iu);
		
		//调用业务逻辑操作
		itemManager.modifyItem(item);
		ActionForward af = new ActionForward("item.do?command=list&pageNo=" + 
                iaf.getPageNo() + 
                "&pageSize=" + iaf.getPageSize(), true);
		return af;
	}

	/**
	 * 根据物料代码查询物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward findDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//调用业务逻辑操作
		Item item = itemManager.findItemById(iaf.getItemNo());
		
		//将查询结果放到request中
		request.setAttribute("item", item);

		return mapping.findForward("find_detail");
	}

	/**
	 * 查询全部物料
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//调用业务逻辑操作
		PageModel pageModel = itemManager.findAllItem(iaf.getPageNo(), iaf.getPageSize(), iaf.getClientIdOrName());
	
		//将查询结果放到request中
		request.setAttribute("pagemodel", pageModel);
	
		return mapping.findForward("list_success");
	}

	/**
	 * upload页面详细信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward uploadDetail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		
		//调用业务逻辑操作
		Item item = itemManager.findItemById(iaf.getItemNo());
		
		//将查询结果放到request中
		request.setAttribute("item", item);
		
		return mapping.findForward("upload_detail");
	}

	/**
	 * 上传图片
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward upload(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//获取从页面表单中提交过来的值
		ItemActionForm iaf = (ItemActionForm)form;
		FormFile myFile = iaf.getFileName();
		if(myFile != null){
			//将上传的文件流保存到磁images/item下
			//String realPath = request.getContextPath().getRealPath("/images/item");
			//System.out.println("realpath" + );
			//FileOutputStream fos = new FileOutputStream("C:\\apache-tomcat-5.5.17\\webapps\\struts_training_itemmgr\\images\\item\\"+iaf.getItemNo() + ".gif");
			FileOutputStream fos = new FileOutputStream(this.uploadPath + iaf.getItemNo() + ".gif");
			fos.write(myFile.getFileData());
			fos.flush();
			fos.close();
		}
		ActionForward af = new ActionForward("item.do?command=list&pageNo=" + 
                iaf.getPageNo() + 
                "&pageSize=" + iaf.getPageSize(), true);
		return af;
	}

	public void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}

	public void setUploadPath(String uploadPath) {
		this.uploadPath = uploadPath;
	}

}


LoginAction.java

package com.bjsxt.drp.web.itemmgr.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bjsxt.drp.web.itemmgr.forms.LoginActionForm;

/**
 * 用户登录Action
 * @author Administrator
 *
 */
public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf = (LoginActionForm)form;
		String username = laf.getUsername();
		String password = laf.getPassword();
		
		if ("admin".equals(username) && "admin".equals(password)) {
			request.getSession().setAttribute("user", "admin");
			return mapping.findForward("sucess");
		}else {
			return mapping.findForward("index");
		}
	}

}


分享到:
评论

相关推荐

    ssh_training_itemmgr.zip

    这个"ssh_training_itemmgr.zip"压缩包很可能是提供了一个SSH框架整合的实际项目示例,帮助开发者理解和学习如何在实际应用中运用这些技术。 **Struts** 是一个基于MVC设计模式的Java Web框架,主要用于控制应用...

    SSH_Test.rar_C ssh_C#ssh_C++ ssh_C++ SSH_libssh2库

    SSH_Test.rar_C ssh_C#ssh_C++ ssh_C++ SSH_libssh2库是一个针对C、C#和C++编程语言的SSH实现示例,特别提到了libssh2库的使用。在Visual Studio 2010环境下,这个压缩包提供了通过SSH进行文件上传的功能。接下来,...

    ssh_do_all_list.sh

    ssh_do all_list执行命令,需要修改脚本“node_list”和集群操作命令即可,执行样例如下:ssh ssh_do_all_list.sh "cat /etc/hosts

    ssh.rar_qt ssh_qtssh_qt中调用ssh_qt使用ssh_ssh qt

    SSH(Secure Shell)是一种网络协议,用于在不安全的网络上提供安全的远程登录和其他服务。在QT(一个跨平台的C++图形用户界面应用程序开发框架)中集成SSH功能,可以实现与远程服务器的安全交互,比如执行Linux命令...

    ssh_test.rar_ssh_ssh_test

    标题中的"ssh_test.rar_ssh_ssh_test"可能是指一个与SSH(Secure Shell)相关的项目或教程,其中包含了"ssh_test"的实例代码。SSH是一种网络协议,用于安全地远程登录到服务器,执行命令和传输数据。它在IT行业中...

    SSH.rar_myeclipse ssh_ssh_ssh myeclipse_ssh2_ssh2 Myeclipse

    标题"SSH.rar_myeclipse ssh_ssh_ssh myeclipse_ssh2_ssh2 Myeclipse"表明这是一个关于SSH配置和MyEclipse集成的资源包,可能包含了SSH框架在MyEclipse中的配置教程,以及SSH2(更安全的SSH版本)的相关资料。...

    SSH_province_sql.rar_SSH_province_sql_ajax_dwr_ssh_ssh ajax

    SSH_province_sql.rar_SSH_province_sql_ajax_dwr_ssh_ssh_ajax这个标题暗示了这是一个与SSH框架(Struts2、Spring、Hibernate)结合DWR(Direct Web Remoting)实现的项目,主要涉及到了省市县三级联动的数据处理,...

    固定资产管理系统SSH_JSP_MySql(源码+数据库sql+lun文+视频齐全).zip

    固定资产管理系统SSH_JSP_MySql(源码+数据库sql+lun文+视频齐全)固定资产管理系统SSH_JSP_MySql(源码+数据库sql+lun文+视频齐全)固定资产管理系统SSH_JSP_MySql(源码+数据库sql+lun文+视频齐全)固定资产管理系统SSH_...

    ssh_known_hosts, cookbook ssh_known_hosts的开发库.zip

    ssh_known_hosts, cookbook ssh_known_hosts的开发库 ssh_known_hosts-cookbook Chef向添加主机和密钥的/etc/ssh/ssh_known_hosts 文件公开资源和默认配方,已知主机的public 键的全局文件。使用键类型和ohai数据...

    ssh.rar_java ssh_ssh_ssh 源码

    标签"java_ssh ssh ssh_源码"进一步确认了这是关于Java语言实现SSH功能的源代码,强调了SSH的多次提及,可能意味着源码中包含了多个SSH相关模块或者示例。 在压缩包子文件的文件名称列表中,"www.pudn.com.txt"可能...

    4.7javaEE_SSH_MySqL基于SSH的医院在线挂号(源码+数据库sql+lun文+视频齐全).zip

    4.7javaEE_SSH_MySqL基于SSH的医院在线挂号(源码+数据库sql+lun文+视频齐全);javaEE_SSH_MySqL基于SSH的医院在线挂号(源码+数据库sql+lun文+视频齐全);javaEE_SSH_MySqL基于SSH的医院在线挂号(源码+数据库sql+lun...

    javaEE_SSH_MVC_MySql动漫论坛的设计与实现(源码+数据库sql+lun文+视频齐全).zip

    javaEE_SSH_MVC_MySql动漫论坛的设计与实现(源码+数据库sql+lun文+视频齐全)javaEE_SSH_MVC_MySql动漫论坛的设计与实现(源码+数据库sql+lun文+视频齐全)javaEE_SSH_MVC_MySql动漫论坛的设计与实现(源码+数据库sql+...

    javaEE_SSH_oracle土地档案管理系统(源码+数据库sql+lun文+视频齐全).zip

    javaEE_SSH_oracle土地档案管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_oracle土地档案管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_oracle土地档案管理系统(源码+数据库sql+lun文+视频齐全)javaEE_...

    JuiceSSH_2.1.4.apk

    JuiceSSH_2.1.4.apk

    ssh_2.rar_SSH MyEclipse_myeclipse ssh_ssh_ssh 源码

    SSH(Struts、Spring、Hibernate)是Java开发中常见的三个框架的组合,广泛应用于企业级应用的开发。这个名为"ssh_2.rar"的压缩包文件包含了一个基于SSH的项目源码,适合在MyEclipse集成开发环境中运行和学习。 **...

    javaEE_SSH_SQLServer商品供应管理系统(源码+数据库sql+lun文+视频齐全).zip

    javaEE_SSH_SQLServer商品供应管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_SQLServer商品供应管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_SQLServer商品供应管理系统(源码+数据库sql+lun文+视频齐全...

    javaEE_SSH_mysql百货中心供应链管理系统(源码+数据库sql+lun文+视频齐全).zip

    javaEE_SSH_mysql百货中心供应链管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_mysql百货中心供应链管理系统(源码+数据库sql+lun文+视频齐全)javaEE_SSH_mysql百货中心供应链管理系统(源码+数据库sql+lun文+...

    淘宝发货__ssh__网上商城(ssh_eshop)__sqlserver.zip

    "淘宝发货__ssh__网上商城(ssh_eshop)__sqlserver.zip" 这个标题表明我们正在处理一个与网上商城系统相关的压缩文件。"ssh"通常指的是Struts2、Spring和Hibernate这三个Java开发框架的组合,它们在Web应用开发中广泛...

    Update_SSH_1.0.21.tar.gz

    在IT行业中,SSH(Secure Shell)是一种用于在网络之间安全地执行命令、传输数据以及管理远程系统的网络协议。OpenSSH是SSH协议的开源实现,而OpenSSL则是一个强大的安全套接层(SSL)和传输层安全(TLS)协议库。...

Global site tag (gtag.js) - Google Analytics