📄 day07.txt
字号:
====== 为主页添加浏览,按照类别查询,购买的功能 =====
1,修改index.html
<%
Collection categoryCol =
(Collection)session.getAttribute( "ALL_CATEGORY" );
Collection productCol =
(Collection)session.getAttribute( "CURRENT_PRODUCT" );
String currCategory =
(String)session.getAttribute( "CURRENT_CATEGORY" );
if( categoryCol == null || productCol == null ){
ProductMgmDelegate pd = new ProductMgmDelegate();
categoryCol = pd.getAllCategory();
productCol = pd.getAllProduct();
}
if( currCategory==null ){
currCategory = "all";
}
%>
<form action="selectCategory.do" method="post">
<table width="90%" border="0" cellspacing="1"
cellpadding="0" height="32" align="center" bgcolor="#D4D4D4">
<tr>
<td height="16" width="66">商品搜索:</td>
<td height="16" width="96">
<select name="category">
<%
if( currCategory.equals( "all" ) ){
%>
<option value="all" selected>ALL</option>
<%
}else{
%>
<option value="all" select>ALL</option>
<%
}
%>
<%
Iterator i = categoryCol.iterator();
while( i.hasNext() ){
CategoryDTO category = (CategoryDTO)i.next();
if( currCategory.equals( category.getId().intValue()+"" ) ){
%>
<option value='<%=category.
getId().intValue() + ""%>'
selected>
<%=category.getName()%>
</option>
<%
}
else{
%>
<option value=
'<%=category.getId().
intValue() + ""%>'>
<%=category.getName()%>
</option>
<%
}
}
%>
</select>
</td>
<td height="16" width="55">
<input type="submit" name="search" value="搜索">
</td>
</tr>
</table>
</form>
<table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="35%">
<hr noshade size="1" color="#ff9900"> </td>
<td bgcolor="#FF9900" align="center"
width="34%"><b>
<font color="#FFFFFF">
本店推荐
</font></b></td>
<td width="31%">
<hr noshade size="1" color="#ff9900"> </td>
</tr>
</table>
<table bordercolor=#FFFFFF height=362 cellspacing=3
cellpadding=3 border=0>
<tr bordercolor=#cccccc>
<td height="529" align="center">
<%
Iterator iter = productCol.iterator();
while( iter.hasNext() ){
ProductDTO product = (ProductDTO)iter.next();
%>
<form action="buyProduct.do" method="post">
<input type="hidden" name="productId"
value='<%=product.getProductId()%>'>
<table width="400" border="0" cellspacing="1"
cellpadding="4" bgcolor="3399ff">
<tr>
<td bgcolor="#ececec" width="22%" align="center" height="11">产品类别</td>
<td bgcolor="#FFFFFF" height="11">
<%=product.getCategory().getName()%>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center" height="11">产品名称
</td>
<td bgcolor="#FFFFFF" height="11">
<%=product.getName()%>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center" height="11">产品价格</td>
<td bgcolor="#FFFFFF" height="11">
<%=product.getBasePrice()%>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center" height="11">购买数量</td>
<td bgcolor="#FFFFFF" height="11">
<input type="text" name="amount">
</td>
</tr>
<tr><td colspan="2">
<p align="right">
<input type="image" src="images/che1.gif" ></p>
</td></tr>
</tr>
</table>
</form>
<%
}
%>
<td></tr></table>
==========================================================
2,为selectCategory.do 创建ActionForm
package : ec_port_web
Action Form : SelectCategoryForm
--> next ---> add
name : cateogory
type : java.lang.String
----> finish
=========================================================
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class SelectCategoryForm extends ActionForm {
private String category;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public ActionErrors validate(
ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
return null;
}
public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
}
}
=========================================================
3,为selectCategory.do 创建Action
package : ec_port_web
Action : SelectCategoryAction
---> next
action path : /selectCategory
formbean name : selectCategoryForm
scope : session
validate : true
input jsp : index.jsp
=========================================================
4,修改Perform() 方法
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import exceptions.ServiceLocatorException;
import delegates.ProductMgmDelegate;
import java.util.Collection;
public class SelectCategoryAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse httpServletResponse) {
SelectCategoryForm form =
(SelectCategoryForm)actionForm;
try{
ProductMgmDelegate pd = new ProductMgmDelegate();
Collection productCol = null;
String currentCategory = null;
if( form.getCategory().equals( "all" ) ){
productCol = pd.getAllProduct();
currentCategory = "all";
}else{
productCol =
pd.getProductsByCategory( form.getCategory());
currentCategory = form.getCategory();
}
Collection categoryCol = pd.getAllCategory();
request.getSession().
setAttribute( "CURRENT_PRODUCT" , productCol );
request.getSession().
setAttribute( "ALL_CATEGORY" , categoryCol );
request.getSession().setAttribute(
"CURRENT_CATEGORY" , urrentCategory );
return actionMapping.findForward( "success" );
}catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
=========================================================
5, 为SelectCategoryAction配置 forward
a) query success
path : /index.jsp
name : success
b) when some exceptions be caught
path : /systemerror.html
name : success
=========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -