📄 day06.txt
字号:
formBean name : delModProductForm
scope : session
validate : true
input jsp : productMgm.jsp
--> finish
==========================================================
13, 修改perform() 方法
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import delegates.ProductMgmDelegate;
import exceptions.ServiceLocatorException;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
import java.util.Collection;
import javax.ejb.RemoveException;
import dto.*;
import java.util.*;
public class DelModProductAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
DelModProductForm form =
(DelModProductForm) actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
String type = request.getParameter( "delete" );
Collection categoryCol = pd.getAllCategory();
request.getSession().
setAttribute( "ALL_CATEGORY" , categoryCol );
if( type != null ){
pd.deleteProduct(form.getPrductId());
Collection productCol = pd.getAllProduct();
request.getSession().
setAttribute( "ALL_PRODUCT" , productCol);
return actionMapping.findForward( "success");
}
ProductDTO product =
pd.getProductById( form.getPrductId() );
request.getSession().
setAttribute( "PRODUCT" , product );
return actionMapping.findForward( "viewProduct" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systeError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systeError" );
}
catch( RemoveException re ){
re.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
==========================================================
14, 为DelModProductAction 配置forward
a) when delete product and query category success
path : /productMgm.jsp
name : success
b) when used to modify product and query product success
path : /viewProduct.jsp
name : viewProduct
c) when some exceptions be caught
path : /fail.jsp
name : systemError
==========================================================
15, 创建 viewProduct.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import = "dto.*,java.util.*" %>
<html>
<head>
<title>
viewProduct
</title>
</head>
<%
ProductDTO product =
(ProductDTO)session.getAttribute( "PRODUCT" );
Collection col =
(Collection)session.getAttribute( "ALL_CATEGORY" );
%>
<body bgcolor="#ffffff">
<form action="modifyProduct.do" method="post">
<input type="hidden" name="id" value='<%=product.getProductId()%>'>
<p align=center>
<table width="600" border="0" cellspacing="1"
cellpadding="4" bgcolor="3399ff">
<tr>
<td colspan="2" align="center">
<font color="#ffffff" class="title">
修改产品</font>
</td>
</tr>
<tr>
<td bgcolor="#ececec" >产品类别</td>
<td bgcolor="#ececec" ><select name="category">
<%
Iterator i = col.iterator();
while( i.hasNext() ){
CategoryDTO category = CategoryDTO)i.next();
if( category.getId(). equals(
product.getCategory().getId()) ){
%>
<option
value='<%=""+category.getId().intValue()%>'
selected>
<%=category.getName()%>
</option>
<%
}
else{
%>
<option value='<%=""+category.getId().intValue()%>'>
<%=category.getName()%>
</option>
<%
}
}
%>
</td>
</tr>
<tr>
<td bgcolor="#ececec" >产品名称</td>
<td bgcolor="#ececec" >
<input type="text" name="name"
value='<%=product.getName()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" >产品单价</td>
<td bgcolor="#ececec" >
<input type="text" name="basePrice"
value='<%=product.getBasePrice()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" >详细信息</td>
<td bgcolor="#ececec" >
<textarea name="desc">
<%=product.getDesc().trim()%>
</textarea>
</td>
</tr>
<tr>
<td bgcolor="#ececec" colspan="2"align="center">
<input type="submit" value="修改">
</td>
</tr>
</table>
</form>
</body>
</html>
==========================================================
16, 为modifyProduct.do 创建ActionForm
file --> new --> web --> ActionForm
package : ec_port_web
AcitonForm : ModifyProductForm
----> next
---> add from jsp ---> viewProduct.jsp --> finish
==========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class ModifyProductForm extends ActionForm {
private String name;
private String basePrice;
private String category;
private String desc;
private String id;
public String getName() {
return name;
}
public void setName(String Name) {
this.name = Name;
}
public String getBasePrice() {
return basePrice;
}
public void setBasePrice(String basePrice) {
this.basePrice = basePrice;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
}
}
=========================================================
17,为 modifyProduct.do 创建Action
file --> new --> web --> Action
package : ec_port_web
Action : ModifyProdcutAction
--> next
action path : /modifyProduct
formbean name : moidfyProductForm
scope : session
validate : true
input jsp : viewProduct.jsp
==========================================================
19, 修改perform() 方法
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import exceptions.ServiceLocatorException;
import delegates.ProductMgmDelegate;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import dto.*;
import java.util.Collection;
import javax.ejb.EJBException;
public class ModifyProductAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
ModifyProductForm form =
(ModifyProductForm) actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
CategoryDTO category = new CategoryDTO(
new Integer(form.getCategory() ),null,null );
ProductDTO product = new ProductDTO( form.getId(),
form.getName(),
Double.parseDouble(form.getBasePrice()),
form.getDesc(),
category);
pd.updateProduct( product);
Collection categoryCol = pd.getAllCategory();
Collection productCol = pd.getAllProduct();
request.getSession().
setAttribute( "ALL_CATEGORY" , categoryCol);
request.getSession().
setAttribute( "ALL_PRODUCT" , productCol);
return actionMapping.findForward( "success" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( EJBException e ){
return null;
}
}
}
==========================================================
20,为ModifyProdcutAction 配置forward
a) when moidfy product success
path : /productMgm.jsp
name : success
b) when some exceptions be caught
path : /fail.jsp
name : systemError
==========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -