===================================
Buy.jsp
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<html>
<head>
<title>JSP for BuyForm form</title>
</head>
<body>
<html:form action="/add.do" method="post">
请您选择书本 : <html:text property="book"/><br/>
<html:submit value="添加到购物车"/>
</html:form>
<HR>
以下是您选择过的书本:<BR>
<logic:present name="cart">
<logic:iterate id="book" name="cart">
<bean:write name="book"/>
<html:link action="/delete.do" paramId="BOOK" paramName="book">删除</html:link>
<BR>
</logic:iterate>
</logic:present>
</body>
</html>
=================================
BuyForm.java
package prj11_1.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* MyEclipse Struts
* Creation date: 11-07-2007
*
* XDoclet definition:
* @struts.form name="buyForm"
*/
public class BuyForm extends ActionForm {
/*
* Generated fields
*/
/** book property */
private String book;
/*
* Generated Methods
*/
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}
/**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}
/**
* Returns the book.
* @return String
*/
public String getBook() {
return book;
}
/**
* Set the book.
* @param book The book to set
*/
public void setBook(String book) {
this.book = book;
}
}
========================================
BuyAction.java
package prj11_1.action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import prj11_1.form.BuyForm;
public class BuyAction extends MappingDispatchAction {
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BuyForm buyForm = (BuyForm) form;// TODO Auto-generated method stub
String book = buyForm.getBook();
HttpSession session = request.getSession();
ArrayList cart = (ArrayList)session.getAttribute("cart");
if(cart==null){
cart = new ArrayList();
session.setAttribute("cart", cart);
}
cart.add(book);
return new ActionForward("/buy.jsp");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String book = request.getParameter("BOOK");
HttpSession session = request.getSession();
ArrayList cart = (ArrayList)session.getAttribute("cart");
cart.remove(book);
return new ActionForward("/buy.jsp");
}
}
/*
public class BuyAction extends DispatchAction {//DispatchAction缺陷:Jsp要认识action内方法
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
BuyForm buyForm = (BuyForm) form;// TODO Auto-generated method stub
String book = buyForm.getBook();
HttpSession session = request.getSession();
ArrayList cart = (ArrayList)session.getAttribute("cart");
if(cart==null){
cart = new ArrayList();
session.setAttribute("cart", cart);
}
cart.add(book);
return new ActionForward("/buy.jsp");
}
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String book = request.getParameter("BOOK");
HttpSession session = request.getSession();
ArrayList cart = (ArrayList)session.getAttribute("cart");
cart.remove(book);
return new ActionForward("/buy.jsp");
}
}*/
======================================
Struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="buyForm" type="prj11_1.form.BuyForm" />
<form-bean name="lrForm" type="prj11_1.form.LrForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<!--
<action
attribute="buyForm"
input="/buy.jsp"
name="buyForm"
path="/buy"
parameter="method"
scope="request"
type="prj11_1.action.BuyAction" />
-->
<action
name="buyForm"
path="/add"
parameter="add"
scope="request"
type="prj11_1.action.BuyAction" />
<action
path="/delete"
parameter="delete"
type="prj11_1.action.BuyAction" />
<action
attribute="lrForm"
input="/lr.jsp"
name="lrForm"
path="/lr"
parameter="ope"
scope="request"
type="prj11_1.action.LrAction" />
</action-mappings>
<message-resources parameter="prj11_1.ApplicationResources" />
</struts-config>
=========================================================================
Lr.jsp
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for LrForm form</title>
</head>
<body>
<html:form action="/lr">
account : <html:text property="account"/><br/>
password : <html:text property="password"/><br/>
<html:submit property="ope"><bean:message key="info.tag.login"/></html:submit>
<html:submit property="ope"><bean:message key="info.tag.reg"/></html:submit>
</html:form>
</body>
</html>
==========================================================
LrAction.java
package prj11_1.action;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
import prj11_1.form.LrForm;
public class LrAction extends LookupDispatchAction {
//重写getKeyMethodMap方法
protected Map getKeyMethodMap() {//用一个map保存资源文件key和方法名的映射
Map map = new HashMap();
map.put("info.tag.login", "login");
map.put("info.tag.reg", "reg");
return map;
}
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LrForm lrForm = (LrForm) form;// TODO Auto-generated method stub
System.out.println("登陆");
return null;
}
public ActionForward reg(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LrForm lrForm = (LrForm) form;// TODO Auto-generated method stub
System.out.println("注册");
return null;
}
}