⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newarticlebean.java

📁 用JSF.Spring和Hibernate开发的一个办公用品管理系统的WEB应用。
💻 JAVA
字号:
package com.jsfabc.jsh.view.bean;

import java.io.IOException;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import com.jsfabc.jsh.model.bo.Article;
import com.jsfabc.jsh.model.exception.DbException;
import com.jsfabc.jsh.model.service.ArticleService;
import com.jsfabc.jsh.utils.MessageFactory;
public class NewArticleBean {
	//属性

	private String articleName;
	private String unit;
	private Double currentPrice;
	//上传用品图片
    private UploadedFile _upFile;
    
	//getter与setter方法
   
    public void setArticleName(String newValue){
    	this.articleName=newValue;
    }
    public String getArticleName() {
        return this.articleName;
    }    
    
    public void setUnit(String newValue){
    	this.unit=newValue;
    }
    public String getUnit() {
        return this.unit;
    }        

    public void setCurrentPrice(Double newValue){
    	this.currentPrice=newValue;
    }
    public Double getCurrentPrice() {
        return this.currentPrice;
    } 
 	
    //用于添加用品图片
    public UploadedFile getUpFile()
    {
        return _upFile;
    }

    public void setUpFile(UploadedFile upFile)
    {
        _upFile = upFile;
    }
    
    //构造函数
    public NewArticleBean() {
    	
    }
    
    //注入对象
	private CategoryBean categoryBean;
	private ArticleService articleService;
    
    public void setCategoryBean(CategoryBean newValue){
    	this.categoryBean=newValue;
    }

    public void setArticleService(ArticleService newValue){
    	this.articleService=newValue;
    }
    
    public String uploadAction() throws IOException
    {
    	//取得faces上下文实例
        FacesContext facesContext = 
        	         FacesContext.getCurrentInstance();
        //通过faces上下文的外部上下文获得request对象  
        HttpServletRequest request = (HttpServletRequest)
                     facesContext.getExternalContext().getRequest();
        //取得应用程序的在服务器上的真实目录,进而得到文件上传的目标目录        
        String filePath=request.getSession().getServletContext()
                     .getRealPath("/")+"articleImg\\"; 
        //得到上传源文件的文件名       
        String srcFileName=_upFile.getName();
        //找到源文件扩展名前的“.”号在文件名字符串中的位置
        int i=srcFileName.lastIndexOf(".");
        //得到源文件的扩展名
        String extName=srcFileName.substring(i);
        //构建一个新的用品对象
        Article article=new Article();
        //用品对象设置类别属性
        article.setCategory(categoryBean.getCategory());
        //用品对象设置名称属性
        article.setArticleName(articleName);
        //用品对象设置计量单位属性
        article.setUnit(unit);
        //用品对象设置当前价格属性
        article.setCurrentPrice(currentPrice);
        //声明用品标识变量
        Integer articleId=0;
        try {
        	//调用业务服务保存用品的方法
			articleId=articleService.saveArticle(article);
			//计算上传目标文件的路径以及文件名和扩展名
			String targetFile=filePath+articleId+extName;
			//取得上传文件的字节
	        byte[] bytes=_upFile.getBytes();
	        //调用业务服务的上传文件方法
	        articleService.uploadFile(targetFile, bytes);
	        //做一些清理工作
	        categoryBean.setCategoryId(0);
	        //返回页面导航字符串	        
	        return "success";
        }
        catch(DbException de){
            //捕获DbException异常
            FacesMessage msg=MessageFactory.getMessage
                       (facesContext,"error_database_error","");
            //设置消息的严重程度
            msg.setSeverity(FacesMessage.SEVERITY_WARN);
            //在faces上下文中添加出错消息
            facesContext.addMessage(null,msg);
            // 返回页面导航字符串
            return "failure";
        }
    	catch(Exception e){
            //捕获其他异常
            FacesMessage msg=MessageFactory.getMessage
                       (facesContext,"error_unexpected","");            
            //设置消息的严重程度
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            //在faces上下文中添加出错消息
            facesContext.addMessage(null,msg);
            //返回页面导航字符串
            return "failure";
        }
    }

    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -