以下是图书管理系统,只有部分代码,主要实现从JSP传数据到action,分页显示没列出,详细在附件
1.add.jsp页面内容
<span style="font-size:18px;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>添加新图书</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"> <script src="js/jquery-1.8.3.js"> </script> <script type="text/javascript"> </script> </head> <body> <form action="addbooks" method="post"> <table align="middle"> <tr> <td></td> <td bgcolor="lightblue" align="center">新增图书信息</td> <td></td> </tr> <tr> <td>图书名称:</td> <td><input type="text" name="book.name"/></td></span><span style="font-size: 18px; font-family: Arial, Helvetica, sans-serif;"><!-- name属性要和action里的属性一致 --></span><span style="font-size:18px;"> <td><s:fielderror fieldName="book.name"/></td><!-- 用框架验证的显示地方 --> </tr> <tr> <td>图书作者:</td> <td><input type="text" name="book.author"/></td> <td><s:fielderror fieldName="book.author"/></td> </tr> <tr> <td>购买日期:</td> <td><input type="text" name="book.buytime"/></td> <td><s:fielderror fieldName="book.buytime"/></td> </tr> <tr> <td>图书类别:</td> <td><select name="sort.name" size="1"> <option value="t" selected="selected">选择所属分类</option> <option value="计算机软件" >计算机软件</option> <option value="小说/文摘" >小说/文摘</option> <option value="杂项" >杂项</option> </select> </td> <td></td> </tr> <tr> <td></td> <td><input type="submit" value="增加图书"/></td> <td></td> </tr> </table> </form> <a href="listpage">返回列表</a> </body> </html> </span>
<span style="font-size:18px;"> </span>
<span style="font-size:18px;"> </span>
<span style="font-size:24px;">2.AddBooks.java是上文JSP的action</span>
<span style="font-size:24px;"> </span>
<span style="font-size:24px;"></span><pre name="code" class="html">package cn.action; import cn.dao.BooksDao; import cn.dao.SortDao; import cn.daoimpl.BooksDaoImpl; import cn.daoimpl.SortDaoImpl; import cn.entity.TbBooks; import cn.entity.TbSort; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionSupport; public class AddBooks extends ActionSupport{ private TbBooks book; private TbSort sort; @Override public String execute() throws Exception { BooksDao booksdao=new BooksDaoImpl(); SortDao sortdao=new SortDaoImpl(); //先将主键sort保存 sortdao.addSort(sort); book.setTbSort(sort); booksdao.addBooks(book); return Action.SUCCESS; } public TbBooks getBook() { return book; } public void setBook(TbBooks book) { this.book = book; } public TbSort getSort() { return sort; } public void setSort(TbSort sort) { this.sort = sort; } }
<span style="font-size:24px;"> </span>
<span style="font-size:24px;"> </span>3.对应的实体类TbBooks.java
<span style="font-size:24px;"> </span>
<span style="font-size:24px;">package cn.entity; import java.util.Date; /** * TbBooks entity. @author MyEclipse Persistence Tools */ public class TbBooks implements java.io.Serializable { <span style="white-space:pre"> </span>// Fields <span style="white-space:pre"> </span>private Integer id; <span style="white-space:pre"> </span>private TbSort tbSort; <span style="white-space:pre"> </span>private String name; <span style="white-space:pre"> </span>private String author; <span style="white-space:pre"> </span>private Date buytime; <span style="white-space:pre"> </span>// Constructors <span style="white-space:pre"> </span>/** default constructor */ <span style="white-space:pre"> </span>public TbBooks() { <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>/** full constructor */ <span style="white-space:pre"> </span>public TbBooks(TbSort tbSort, String name, String author, Date buytime) { <span style="white-space:pre"> </span>this.tbSort = tbSort; <span style="white-space:pre"> </span>this.name = name; <span style="white-space:pre"> </span>this.author = author; <span style="white-space:pre"> </span>this.buytime = buytime; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>// Property accessors <span style="white-space:pre"> </span>public Integer getId() { <span style="white-space:pre"> </span>return this.id; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setId(Integer id) { <span style="white-space:pre"> </span>this.id = id; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public TbSort getTbSort() { <span style="white-space:pre"> </span>return this.tbSort; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setTbSort(TbSort tbSort) { <span style="white-space:pre"> </span>this.tbSort = tbSort; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getName() { <span style="white-space:pre"> </span>return this.name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setName(String name) { <span style="white-space:pre"> </span>this.name = name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public String getAuthor() { <span style="white-space:pre"> </span>return this.author; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setAuthor(String author) { <span style="white-space:pre"> </span>this.author = author; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public Date getBuytime() { <span style="white-space:pre"> </span>return this.buytime; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public void setBuytime(Date buytime) { <span style="white-space:pre"> </span>this.buytime = buytime; <span style="white-space:pre"> </span>} }</span>
<span style="font-size:24px;"> </span>
<span style="font-size:24px;">还有另一个实体类TbSort.java</span>
<span style="font-size:24px;"> </span>
<span style="font-size:24px;"></span><pre name="code" class="html">package cn.entity; import java.util.HashSet; import java.util.Set; /** * TbSort entity. @author MyEclipse Persistence Tools */ public class TbSort implements java.io.Serializable { // Fields private Integer id; private String name; private Set tbBookses = new HashSet(0); // Constructors /** default constructor */ public TbSort() { } /** minimal constructor */ public TbSort(String name) { this.name = name; } /** full constructor */ public TbSort(String name, Set tbBookses) { this.name = name; this.tbBookses = tbBookses; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set getTbBookses() { return this.tbBookses; } public void setTbBookses(Set tbBookses) { this.tbBookses = tbBookses; } }
4.struts.xml的配置
<span style="font-size:24px;"> </span>
<span style="font-size:24px;"></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="default" extends="struts-default" namespace="/"> <action name="pagelist" class="cn.action.PageList"> <result>index.jsp</result> </action> <action name="del" class="cn.action.DelAction"> <result type="redirectAction">pagelist</result> </action> <action name="addbooks" class="cn.action.AddBooks"> <result type="redirectAction" name="success">pagelist</result> <result name="input">add.jsp</result> </action> </package> </struts>
<span style="font-size:24px;"> </span>
<span style="font-size:18px;"> </span>