今年IT公司薪水统计,欢迎补充(转载)

 

完成FormAction的编写后,接下来就是整理struts-config.xml文件和在applicationContext-action.xml文件中注册Action,具体操作如下:

a.       struts-config.xml中添加如下内容

i) <form-beans>标签里面,添加一个shopForm

<form-bean name="shopForm" type="cn.com.book.demo.struts.form.ShopForm" />

ii) <action-mappings>标签里面,添加一个shop Action

    <action

      attribute="shopForm"

      name="shopForm"

      parameter="method"

      path="/shop"

      scope="request"

      type="org.springframework.web.struts.DelegatingActionProxy"

      validate="false" >

      <!—定义分页查询成功后的转向目标à

      <forward name="success" path="/WEB-INF/jsp/shop/shopList.jsp"></forward>

      <!—定义往购物车中添加一个商品后的转向目标à

      <forward name="addShop" path="/WEB-INF/jsp/success.jsp"></forward>

      <!—定义显示购物车列表的转向目标à

      <forward
name="displayAddedShops" path="/WEB-INF/jsp/shop/shopCar.jsp"></forward>

      <!—定义改变购物车中某商品数量后的转向目标à

      <forward
name="changeShopAmount" path="/shop.do?method=displayAddedShops"></forward>

      <!—定义从购物车中清除某商品后的转向目标à

      <forward name="deleteShop" path="/shop.do?method=displayAddedShops"></forward>

    </action>

b.       applicationContext-action.xml添加如下内容,spring中注册对应的shop action

       <bean name="/shop" class="cn.com.book.demo.struts.action.ShopAction">

           <property name="shopService">

              <ref bean="shopService"/>

           </property>

       </bean>

1.    添加资源信息

同样,为了使用多国语言功能,我们在ShopResource-temp.properties文件中,添加如下信息的定义

shop.title=选购商品

shop.seq.no=序号

shop.name=商品名称

shop.price=价格

shop.select=选择

shop.add.shop=加入购物车

shop.no=

shop.page=

shop.first.page=首页

shop.prev.page=上一页

shop.next.page=下一页

shop.last.page=末页

shop.go=GO

 

shop.car=查看购物车

shop.amount=数量

shop.total.price=总价格

shop.operate=操作

shop.delete=删除

shop.submit.order=下单

shop.select.shop=选购其他商品

shop.add.car=加入购物车成功

 

同时执行一下encoding.bat批处理,生成一个与ShopResource-temp.properties对应的unicode编码个资源文件ShopResource_zh_CN.properties文件

2.    编写显示的jsp

  在我们Shop功能模块中,主要有两个jspshopList.jspshopCar.jsp

i)                    shopList.jsp主要是用来分页显示商品列表的页面,它里面除了可以通过上下翻页实现分页显示商品列表外,还可以将某个商品添加到购物车中去。而且尤为要注意的是,往购物车中添加商品的功能,我们在这里是使用Ajax技术进行实现的:在点击页面上“加入购物车”按钮后,会在不刷新当前页面的情况下,异步的往购物车中添加一个选定的商品。详细清单如下:

<%@ page language="java" pageEncoding="UTF-8"%>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title><bean:message bundle="shop" key="shop.title"/></title>

<script language="javascript">

    // 定义个用来发送ajax请求的全局变量

    var http_request = false;

    // 发送Ajax请求

    function makeRequest(url) {

        http_request = false;

        // 根据不同的浏览器(IE 或其他浏览器),用各自对应的方法生成ajax的发送对象

        if (window.XMLHttpRequest) { // Mozilla, Safari,...浏览器

            http_request = new XMLHttpRequest();

            if (http_request.overrideMimeType) {

                http_request.overrideMimeType('text/xml');

            }

        } else if (window.ActiveXObject) { // IE浏览器

            try {

                http_request = new ActiveXObject("Msxml2.XMLHTTP");

            } catch (e) {

                try {

                    http_request = new ActiveXObject("Microsoft.XMLHTTP");

                } catch (e) {}

            }

        }

 

        if (!http_request) {

            alert('Giving up :( Cannot create an XMLHTTP instance');

            return false;

        }

        // ajax请求的状态发生改变的时候,绑定alertContents方法

        http_request.onreadystatechange = alertContents;

        // 创建一个新的http请求

        http_request.open('GET', url, true);

        // 发送http请求

        http_request.send(null);

 

    }

 

    function alertContents() {

        if (http_request.readyState == 4) {

            if (http_request.status == 200) {

                alert('<bean:message bundle="shop" key="shop.add.car"/>');

            } else {

                alert('There was a problem with the request.');

            }

        }

    }

 

    function sendAjaxRequest(url){

        var date = new Date();

        makeRequest(url + "&test="+date);

    }

   

    function toPage(page){

        var targetPage;

        if(page == 'f'){

           targetPage=1;

        }else if(page == 'l'){

           targetPage = document.shopForm.pageAmount.value;

        }else if(page == 'g'){

           targetPage = document.shopForm.toPage.value;

              if(targetPage > document.shopForm.pageAmount.value){

                  targetPage = document.shopForm.pageAmount.value;

              }

              if(targetPage < 1){

                  targetPage = 1;

              }

              //alert(targetPage);

        }else{

           targetPage = parseInt(document.shopForm.targetPage.value) + parseInt(page);

        }

        document.shopForm.targetPage.value = targetPage;

        document.shopForm.submit();

    }

</script>

</head>

<script language="javascript">

    function _toPage(v){

        //alert(v);

        toPage(v);

    }

    function viewShopCar(){

        document.location.href="shop.do?method=displayAddedShops";

    }

</script>

<body>

<html:form action="/shop.do?method=find">

  <html:hidden property="targetPage"/>

 

  <input type="hidden" value="<bean:write name='shopForm' property='totalPage'/>" name="pageAmount"/>

<table width="731"  border="0" align="center" cellpadding="0" cellspacing="0">

  <tr>

    <td width="731">&nbsp;</td>

  </tr>

  <tr>

    <td>&nbsp;</td>

  </tr>

  <tr>

    <td><table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td width="15%"><div align="center"><strong><bean:message key="shop.seq.no" bundle="shop"/></strong></div></td>

        <td width="42%"><div align="center"><strong><bean:message key="shop.name" bundle="shop"/></strong></div></td>

        <td width="18%"><div align="center"><strong><bean:message key="shop.price" bundle="shop"/></strong></div></td>

        <td width="25%"><div align="center"><strong><bean:message key="shop.select" bundle="shop"/></strong></div></td>

      </tr>

      <logic:notEmpty name="shopForm" property="shops">

        <logic:iterate id="shop" name="shopForm" property="shops" indexId="indexid">

      <tr>

        <td><div align="center">${indexid}</div></td>

        <td><bean:write name="shop" property="name"/></td>

        <td><bean:write name="shop" property="price"/></td>

        <td><div align="center">

          <input type="button" name="button${indexid}" value="<bean:message key="shop.add.shop" bundle="shop"/>" onClick="sendAjaxRequest('shop.do?method=addShop&shopId=${shop.id}')">

        </div></td>

      </tr>

        </logic:iterate>

      </logic:notEmpty>

    </table></td>

  </tr>

  <tr>

    <td>

    <logic:greaterThan name="shopForm" parameter="totalPage" value="1" >

    <table width="98%"  border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td width="24%"><div align="right"><bean:message key="shop.no" bundle="shop"/><bean:write name="shopForm" property="targetPage"/>/<bean:write name="shopForm" property="totalPage"/></div></td>

        <td width="64%">

        <c:choose>

           <c:when test="${shopForm.hasFirst}"><a href="#" οnclick="_toPage('f')"><bean:message key="shop.first.page" bundle="shop"/></a></c:when>

           <c:otherwise><bean:message key="shop.first.page" bundle="shop"/></c:otherwise>

        </c:choose>

        <c:choose>

           <c:when test="${shopForm.hasPrev}"><a href="#" onClick="_toPage(-1)"><bean:message key="shop.prev.page" bundle="shop"/></a></c:when>

           <c:otherwise><bean:message key="shop.prev.page" bundle="shop"/></c:otherwise>

        </c:choose>

        <c:choose>

           <c:when test="${shopForm.hasNext}"><a href="#" onClick="_toPage(1)"><bean:message key="shop.next.page" bundle="shop"/></a></c:when>

           <c:otherwise><bean:message key="shop.next.page" bundle="shop"/></c:otherwise>

        </c:choose>

        <c:choose>

           <c:when test="${shopForm.hasLast}"><a href="#" onClick="_toPage('l')"><bean:message key="shop.last.page" bundle="shop"/></a></c:when>

           <c:otherwise><bean:message key="shop.last.page" bundle="shop"/></c:otherwise>

        </c:choose>

       

         <c:if test="${shopForm.totalPage>1}">

          <bean:message key="shop.no" bundle="shop"/>

          <input name="toPage" type="text" size="2"/>

          <bean:message key="shop.page" bundle="shop"/>

          <input type="button" name="goButton" value="Go" onClick="_toPage('g')"/>

         </c:if>

         </td>

        <td width="12%"><input type="button" value="查看购物车" onClick="viewShopCar()"></td>

      </tr>

    </table>

    </logic:greaterThan>

    </td>

  </tr>

</table>

</html:form>

</body>

</html>

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值