前言:
通过对struts2之增删改查的了解,我们来解决我们在原基础上遗留的问题,也就是我们的图片上传以及图片的展示
首先我们先来建立我们所需要的上传图片界面
clzList.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/deng" prefix="d" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>班级主界面</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
班级名:<input type="text" name="cname"> <input type="submit"
value="确定">
<input type="hidden" name="rows" value="15">
</form>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">增加</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>教员</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${clzList }" var="b">
<tr>
<td>${b.cid }</td>
<td>${b.cname}</td>
<td>${b.cteacher}</td>
<td>
<img style="height: 60px;width: 60px" src="${pageContext.request.contextPath}${b.pic}">
</td>
<td>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a>
<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a>
<a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a>
</td>
</tr>
</c:forEach>
</table>
<d:page pageBean="${pageBean}"></d:page>
</body>
</html>
clzUpload.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="cid" value="${result.cid}"><br>
<input type="hidden" name="cname" value="${result.cname }"><br>
<input type="hidden" name="cteacher" value="${result.cteacher}"><br>
<input type="file" name="file">
<input type="submit" value="ok">
</form>
</body>
</html>
然后我们在ClazzAction.java里面添加我们需要的属性值以及方法(注:我们所添加的属性值一定要生成set和get方法,否则没有效果)
ClazzAction.java
代码如下:
package com.dengrenli.web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import com.dengrenli.dao.ClazzDao;
import com.dengrenli.entity.Clazz;
import com.dengrenli.util.BaseAction;
import com.dengrenli.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private Clazz clazz=new Clazz();
private ClazzDao clazzDao=new ClazzDao();
//这里的属性名要和上传图片哪里的name名对应 xxx
private File file;
//xxxContentType
private String fileContentType;
//xxxFileName
private String fileFileName;
/**
* 跳转上传图片的界面
*/
public String preUpload() {
try {
this.result= this.clazzDao.list(clazz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "preUpload";
}
public String upload() {
String realDir= "E:/aaa/T226";
String severDir="/upload";
try {
FileUtils.copyFile(file, new File(realDir+"/"+fileFileName));
clazz.setPic(severDir+"/"+fileFileName);
try {
try {
this.clazzDao.edit(clazz);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* 查询
* @return
*/
public String list() {
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
try {
List<Clazz> list = this.clazzDao.list(clazz, pageBean);
request.setAttribute("clzList", list);//传参传到前台去
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
/*
* 跳转编辑页面(增加和修改页面)
*/
public String preSave() {
if(clazz.getCid()!=0) {//id不等于0就是有值
try {
this.result= this.clazzDao.list(clazz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preSave";
}
//增加
public String add() {
try {
this.code=this.clazzDao.add(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//修改
public String edit() {
try {
this.clazzDao.edit(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//删除
public String del() {
try {
this.clazzDao.del(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
// TODO Auto-generated method stub
return clazz;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}
除了以上用我们的工具类来实现以外,我们还可以自己来写方法,这样也可以实现图片上传,而且效率更高
代码如下所示:
package com.dengrenli.web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import com.dengrenli.dao.ClazzDao;
import com.dengrenli.entity.Clazz;
import com.dengrenli.util.BaseAction;
import com.dengrenli.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private Clazz clazz=new Clazz();
private ClazzDao clazzDao=new ClazzDao();
//这里的属性名要和上传图片哪里的name名对应 xxx
private File file;
//xxxContentType
private String fileContentType;
//xxxFileName
private String fileFileName;
/**
* 跳转上传图片的界面
*/
public String preUpload() {
try {
this.result= this.clazzDao.list(clazz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "preUpload";
}
public String upload() {
String realDir= "E:/aaa/T226";
String severDir="/upload";
try {
// FileUtils.copyFile(file, new File(realDir+"/"+fileFileName));
copyFile(file, new File(realDir+"/"+fileFileName));
clazz.setPic(severDir+"/"+fileFileName);
try {
try {
this.clazzDao.edit(clazz);
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* 利用缓冲流技术进行拷贝
* @param source
* @param target
* @throws IOException
*/
public void copyFile(File source,File target) throws IOException {
BufferedInputStream in=new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
byte[] bbuf=new byte[1024];
int len=0;
while((len=in.read(bbuf))!=-1) {
out.write(bbuf,0,len);
}
in.close();
out.close();
}
/**
* 查询
* @return
*/
public String list() {
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
try {
List<Clazz> list = this.clazzDao.list(clazz, pageBean);
request.setAttribute("clzList", list);//传参传到前台去
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
/*
* 跳转编辑页面(增加和修改页面)
*/
public String preSave() {
if(clazz.getCid()!=0) {//id不等于0就是有值
try {
this.result= this.clazzDao.list(clazz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preSave";
}
//增加
public String add() {
try {
this.code=this.clazzDao.add(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//修改
public String edit() {
try {
this.clazzDao.edit(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
//删除
public String del() {
try {
this.clazzDao.del(clazz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
// TODO Auto-generated method stub
return clazz;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
}
struts-sy.xml
代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/clz_*" class="com.dengrenli.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="preUpload">/clzUpload.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
</action>
</package>
</struts>
以上的步骤我们可以实现把图片上传到指定路径,以及在数据库中图片路径的修改
接下来我们要做的就是把我们上传的图片显示在我们的主页上
我们先来添加服务器与真实目录的映射关系,只有这样才能做到图片的显示
首先如图所示:
然后点击进入server.xml,添加映射关系
path:项目名/upload
docBase:上传图片的完整路径
clzList.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/deng" prefix="d" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>班级主界面</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
班级名:<input type="text" name="cname"> <input type="submit"
value="确定">
<input type="hidden" name="rows" value="15">
</form>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">增加</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>教员</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${clzList }" var="b">
<tr>
<td>${b.cid }</td>
<td>${b.cname}</td>
<td>${b.cteacher}</td>
<td>
<img style="height: 60px;width: 60px" src="${pageContext.request.contextPath}${b.pic}">
</td>
<td>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a>
<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a>
<a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a>
</td>
</tr>
</c:forEach>
</table>
<d:page pageBean="${pageBean}"></d:page>
</body>
</html>
只有添加了映射关系,在主页上才可以通过
p
a
g
e
C
o
n
t
e
x
t
.
r
e
q
u
e
s
t
.
c
o
n
t
e
x
t
P
a
t
h
{pageContext.request.contextPath}
pageContext.request.contextPath{b.pic}这个路径里面的文件名,去到映射关系里面的上传图片所用到的路径里面去找到对应的图片,然后把图片显示在主页上,从而达到想要的效果
效果如图所示:
谢谢大家,多多指教!!!