Struts2 由jsp传id查询数据库中一条数据
ENTITY
package dao.entity;
import java.io.File;
import java.io.Serializable;
public class Commodity implements Serializable{
private static final long serialVersionUID = 8641317384689435600L;
private int gno;
private String gname;
private int goprice;
private int grprice;
private int gstore;
private String gpicture;
public int getGno() {
return gno;
}
public void setGno(int gno) {
this.gno = gno;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public int getGoprice() {
return goprice;
}
public void setGoprice(int goprice) {
this.goprice = goprice;
}
public int getGrprice() {
return grprice;
}
public void setGrprice(int grprice) {
this.grprice = grprice;
}
public int getGstore() {
return gstore;
}
public void setGstore(int gstore) {
this.gstore = gstore;
}
public String getGpicture() {
return gpicture;
}
public void setGpicture(String gpicture) {
this.gpicture = gpicture;
}
}
DAO
package dao;
import java.sql.SQLException;
import java.util.List;
import dao.entity.Commodity;
import dao.entity.Good;
public interface CommodityDAO {
boolean select(Commodity commodity) throws SQLException;
}
DAO.IMPL
package dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import dao.CommodityDAO;
import dao.entity.Commodity;
public class CommodityDAOImpl implements CommodityDAO {
private Connection conn;
CommodityDAOImpl(Connection conn) {
this.conn = conn;
}
public boolean select(Commodity commodity) throws SQLException {
boolean exists = false;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM goodstable WHERE gno=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, commodity.getGno());
rs = pstmt.executeQuery();
exists = rs.next();
if (exists) {
commodity.setGname(rs.getString("gname"));
commodity.setGoprice(rs.getInt("goprice"));
commodity.setGrprice(rs.getInt("grprice"));
commodity.setGstore(rs.getInt("gstore"));
commodity.setGpicture(rs.getString("gpicture"));
}
return exists;
}
public static void main(String[] args) throws SQLException {
Connection conn = dao.common.DBUtilX.getConnection();
CommodityDAO commoditydao = new CommodityDAOImpl(conn);
Commodity commodity= new Commodity();
commodity.setGno(1);
commodity.setGname("1");
commodity.setGrprice(1);
commodity.setGoprice(1);
commodity.setGstore(1);
commodity.setGpicture("1");
commoditydao.select(commodity);
System.out.println(commodity.getGoprice());
}
}
运行impl可以正常输出
SERVICE
package service;
import java.util.List;
import dao.entity.Commodity;;
public interface CommodityService {
String select(Commodity commodity);
}
SERVICE.IMPL
package service.impl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import dao.CommodityDAO;
import dao.common.DBUtil;
import dao.common.DBUtilX;
import dao.entity.Commodity;
import dao.impl.DAOFactory;
import dao.impl.DAOFactoryX;
import service.CommodityService;
public class CommodityServiceImpl implements CommodityService{
public String select(Commodity commodity) {
Connection conn = DBUtilX.getConnection();
CommodityDAO commodityDao = DAOFactoryX.getCommodityDAO(conn);
String message = null;
try {
boolean result = commodityDao.select(commodity);
if(result){
message = "查询失败";
}
} catch (SQLException e) {
message = "查询失败,请稍后再试!";
e.printStackTrace();
}finally {
DBUtilX.closeConnection(conn);
}
return message;
}
}
ACTION
package web;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import service.CommodityService;
import service.impl.ServiceFactoryX;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import dao.entity.Commodity;
public class CommodityAction extends ActionSupport implements ModelDriven<Commodity>,SessionAware{
private static final long serialVersionUID = -1293038275588077562L;
private Map<String, Object> session;
private Commodity commodity = new Commodity();
public String detail(){
CommodityService commodityService = ServiceFactoryX.getCommodityService();
String message = commodityService.select(commodity);
if (message == null) {
session.put("commodity", commodity);
return SUCCESS;
} else {
this.addActionError(message);
return INPUT;
}
}
public Commodity getCommodity() {
return commodity;
}
public void setCommodity(Commodity commodity) {
this.commodity = commodity;
}
public Commodity getModel() {
return commodity;
}
public void setSession(Map<String, Object> arg0) {
this.session = arg0;
}
}
JSP
<tr>
<td colspan="2">
<table style="border-collapse: collapse;">
<s:form action="/shop/detail.action" method="post">
<s:iterator value="#session.allCommodity" var="commodity"
status="st">
<s:if test="#st.index % 3 == 0">
<tr height="300px">
</s:if>
<td width="180px" style="padding: 35px;">
<div align="center">
<img src='<s:property value="#commodity.gpicture"/>'
height="180px" width="200px" />
</div>
<div align="center">
商品编号:
<input style="text-align:center; background-color: gray;border: none;" type="text" id="gno" name="gno" readonly="readonly" value=<s:property value="#commodity.gno" /> >
</div>
<div align="center">
商品名称:
<s:property value="#commodity.gname" />
</div>
<div align="center">
商品价格(元):
<s:property value="#commodity.goprice" />
</div>
<div align="left">
<s:submit value="查看更多" />
</div>
</td>
<s:if test="#st.index % 3 == 2 || #st.last">
</tr>
</s:if>
</s:iterator>
</s:form>
</table>
</td>
</tr>
STRUTS
<package name="shop" namespace="/shop" extends="struts-default">
<action name="detail" class="web.CommodityAction" method="detail">
<result>/WEB-INF/view/shop/detail.jsp</result>
<result name="input">/WEB-INF/view/shop/main.jsp</result>
</action>
</package>
点击jsp页面中的查看更多按钮就报以下错误
ognl.MethodFailedException: Method "setGno" failed for object dao.entity.Commodity@1bb8694 [java.lang.NoSuchMethodException: dao.entity.Commodity.setGno([Ljava.lang.String;)]
java.lang.NoSuchMethodException: dao.entity.Commodity.setGno([Ljava.lang.String;)