package com.action;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.dto.Two;
import com.service.TeacherService;
/** * 两表联查,图片添加、下载 * @author hao * 2018-5-31 14:06:51 */
public class Action {
private TeacherService service = new TeacherService();
private List<Two> twoList = new ArrayList();
private Two two;
private File aaa;
private String aaaFileName;
public String execute() {
// TODO Auto-generated method stub
twoList = service.getTwoList();
return "toList";
}
public String setAdd() throws Exception{
System.out.println("进入setAdd");
String realPath = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("")+"/imgs";
File f = new File(realPath, aaaFileName);
FileUtils.copyFile(aaa, f);
two.setImg(aaaFileName);
int i = service.setAdd(two);
System.out.println(i);
return "reList";
}
public List<Two> getTwoList() {
return twoList;
}
public void setTwoList(List<Two> twoList) {
this.twoList = twoList;
}
public File getAaa() {
return aaa;
}
public void setAaa(File aaa) {
this.aaa = aaa;
}
public String getAaaFileName() {
return aaaFileName;
}
public void setAaaFileName(String aaaFileName) {
this.aaaFileName = aaaFileName;
}
public Two getTwo() {
return two;
}
public void setTwo(Two two) {
this.two = two;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'add.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
</head>
<body>
<center>
<form action="getList!setAdd" method="post" enctype="multipart/form-data">
id:<input name="two.id"><br>
name:<input name="two.name"><br>
sex:<input name="two.sex"><br>
hobby:<input name="two.hobby"><br>
did:<input name="two.did"><br>
address:<input name="address"><br>
img:<input name="aaa" type="file"><br>
<input type="submit" value="添加">
</form>
</center>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'list.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<link rel="stylesheet" href="css/index_work.css" type="text/css"></link>
<script type="text/javascript"> </script>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>name</th>
<th>sex</th>
<th>hobby</th>
<th>did</th>
<th>address</th>
<th>
头像
</th>
<th>department</th>
<th>content</th>
<th>datea</th>
</tr>
<c:forEach var="s" items="${ twoList}">
<tr>
<th>${s.id }</th>
<th>${s.name }</th>
<th>${s.sex }</th>
<th>${s.hobby }</th>
<th>${s.did }</th>
<th>${s.address }</th>
<th>
<c:if test="${s.img!=null&&s.img!=''}">
<a href=""><img width="160px" src="imgs/${s.img}"></a>
</c:if>
</th>
<th>${s.department }</th>
<th>${s.content }</th>
<th>${s.datea }</th>
</tr>
</c:forEach>
<tr>
<th colspan="21"><input type="button" value="添加" onclick="location='add.jsp'"></th>
</tr>
</table>
</body>
</html>
Only the deaf appreciate hearing, only the blind realize the manifold blessings that lie in sight.
只有聋子才珍惜听力,只有盲人才能感受到得见光明的喜悦。