将jsp页面list值传给后台action处理

遇到个需要传输list的问题,困扰了两天,网上各种各样,今天突然看到这位大神的,然后根据情况自己终于解决了难题,现在结合大神的,大概总结一下,以便下次参考!
这位大神的微博链接:
https://blog.csdn.net/sinat_19650093/article/details/50462050

在Jsp页面传递需要到Action的List中的数据,书写格式为

<s:textfield name="%{'books['+#status.index+'].title'}" /> 或者
<pre name="code" class="html" style="font-size: 18px;"><s:textfield name="books[#status.index].title" />

在这里为Book对象传递的有3个值,书写语法都一样,代码如下

<body>
	<h1>图书订购系统</h1>
	<s:form  action="bookConfirm" method="post" theme="simple">
		<table>
			<tr>
				<td>书名</td>
			
				<td>价格</td>
			
				<td>数量</td>
			</tr>
			<s:iterator value="new int[5]" status="status">
			<tr>
				<td><s:textfield name="%{'books['+#status.index+'].title'}" /></td>		
				<td><s:textfield name="%{'books['+#status.index+'].price'}" /></td>				
				<td><s:textfield name="%{'books['+#status.index+'].amount'}" /></td>
			</tr>
			</s:iterator>
			<tr>
				<td colspan="3"><s:submit value="订阅图书" /></td>
			</tr>
		</table>
		
	</s:form>

</body>

书籍的实例对象定义为

public class Book {

	private String title;
	
	private float price;
	
	private int amount;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
	
	
}

请求的Action中定义列表List有两种不同的方式,如果我们在定义列表的时候已经为List限定了泛型,那么书写的格式如下(1)即可,否则需要如(2)代码所示,还要为Action添加一个ObjectTypeDeterminer对象来限定集合元素的类型。

1public class BookConfirm extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List<Book> books =null;
	
	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		System.out.println("enter");
		this.books = books;
	}

}2public class BookConfirm extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private List books =null;
	
	public List getBooks() {
		return books;
	}

	public void setBooks(List books) {
		System.out.println("enter");
		this.books = books;
	}

	
}

在Action所在目录下定义一个文件BookConfirm-conversion.properties,
文件命名格式为:classname-conversion.properties文件内添加语句

Element_books=com.addbooks.Book
(此句非常重要,一定得添加上)
然后action便会自动set属性值!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先需要明确的是,影片上传功能需要涉及到文件上传和文件存储两个方面。 1. 文件上传 在JSP中,可以使用form表单中的<input type="file">元素实现文件上传。具体实现方法如下: 在上传页面的表单中添加一个文件选择框: ``` <form action="upload.jsp" method="post" enctype="multipart/form-data"> <label for="file">选择文件:</label> <input type="file" name="file" id="file"> <input type="submit" name="submit" value="上传"> </form> ``` 在服务器端的upload.jsp页面中,通过request对象获取上传的文件并保存到指定位置: ``` <% String savePath = "D:/upload"; // 保存的目录 String fileName = ""; // 保存的文件名 File file = new File(savePath); if (!file.exists() && !file.isDirectory()) { System.out.println(savePath+"目录不存在,需要创建"); file.mkdir(); // 创建目录 } // 上传文件 String contentType = request.getContentType(); if (contentType != null && contentType.indexOf("multipart/form-data") >= 0) { try { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { fileName = item.getName(); // 处理上传的文件名,只保留文件名部分 fileName = fileName.substring(fileName.lastIndexOf("\\")+1); OutputStream out = new FileOutputStream(new File(savePath, fileName)); InputStream in = item.getInputStream(); int len; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); in.close(); } } } catch (Exception e) { e.printStackTrace(); } } %> ``` 2. 文件存储 影片上传之后需要将文件存储到服务器的指定目录中,并保存文件信息到数据库中。可以在upload.jsp页面中添加代码实现存储操作。具体实现方法如下: 在数据库中创建一个存储影片信息的表,例如film表,包含以下字段: - film_id:影片ID,自增长 - film_name:影片名称 - film_path:影片存储路径 - film_time:上传时间 在upload.jsp页面中添加代码实现存储操作: ``` <% String savePath = "D:/upload"; // 保存的目录 String fileName = ""; // 保存的文件名 File file = new File(savePath); if (!file.exists() && !file.isDirectory()) { System.out.println(savePath+"目录不存在,需要创建"); file.mkdir(); // 创建目录 } // 上传文件 String contentType = request.getContentType(); if (contentType != null && contentType.indexOf("multipart/form-data") >= 0) { try { DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = upload.parseRequest(request); for (FileItem item : items) { if (!item.isFormField()) { fileName = item.getName(); // 处理上传的文件名,只保留文件名部分 fileName = fileName.substring(fileName.lastIndexOf("\\")+1); OutputStream out = new FileOutputStream(new File(savePath, fileName)); InputStream in = item.getInputStream(); int len; byte[] buffer = new byte[1024]; while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); in.close(); // 将影片信息保存到数据库中 String filmName = request.getParameter("film_name"); String filmPath = savePath + "/" + fileName; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String filmTime = sdf.format(new Date()); Connection conn = null; PreparedStatement ps = null; try { Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/test"; String user = "root"; String password = "123456"; conn = DriverManager.getConnection(url, user, password); String sql = "insert into film(film_name, film_path, film_time) values(?,?,?)"; ps = conn.prepareStatement(sql); ps.setString(1, filmName); ps.setString(2, filmPath); ps.setString(3, filmTime); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } } } catch (Exception e) { e.printStackTrace(); } } %> ``` 这样,影片上传功能就实现了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值