zoukankan      html  css  js  c++  java
  • JAVA EE Demo[购物商城 Strust2]

      为了搞定作业,我开始了J2EE的Strust2框架实现一个简单的商城Demo

      先创建Java Web Service项目。添加JDBC驱动,导入Strust2框架得到这个:

                            

    啧啧。既然是购物商城我们继续沿用上篇文章的结构,欢迎页+商城物品列表+购物车+登录 ,则很明显我们需要一个导航栏

    创建一个导航栏:head.html

    这样我们以后就可以利用JSP标签将这个导航栏嵌入到任何需要的界面了

    而导航栏的内容包括欢迎页 商城物品列表 购物车 登录 登出 这些选项 这个时候弄完这个先得到以下:

    然后我们开始对第一个欢迎页进行搞事:hello.jsp(为了美观弄了几个CSS)

    并且把导航栏中Hello加入链接定向到Hello.jsp

    得到以下:

    接下来确定登录页面:

    很明显,登录界面的动作我们利用Struts2的Action类

    login.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>login</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta name="keywords" content="expand, form, css3, jquery, animate, width, height, adapt, unobtrusive javascript"> <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon"> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type="text/javascript"> Cufon.replace('h1',{ textShadow: '1px 1px #fff'}); Cufon.replace('h2',{ textShadow: '1px 1px #fff'}); Cufon.replace('h3',{ textShadow: '1px 1px #000'}); Cufon.replace('.back'); </script> </head> <body> <%@include file="head.jsp" %> <div class="wrapper"> <div class="content"> <div id="form_wrapper" class="form_wrapper" style=" 350px; height: 315px;"> <form action="login" method="post" class="login active" name="myform"> <h3>Login</h3> <div> <label>Username:</label> <input type="text" name="name"> </div> <div> <label>Password:</label> <input type="password" name="password"> </div> <div class="bottom"> <input type="submit" value="Login"> <input type="reset" value="Reset" > <div class="clear"></div> </div> </form> </div> </div> </div> <s:fielderror/> </body> </html>

     而购物商城的核心是购物车和商品显示,在View层我们通过登录定向到商品显示界面:

    suc.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>product</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="css/style.css" type="text/css"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <%@include file="head.jsp" %> <div class="wrapper"> <center> <h1><font color="#FF7F00">Please Chose things</font></h1> <div id="container"> <table class="zebra" action="show" method="post"> <thead> <tr> <td>Product ID</td> <td>Product Name</td> <td>Product Content</td> <td>Product Price</td> <td>Buy Num</td> <td>ShopCar</td> </tr> </thead> <s:iterator value="products"> <form action="car" method="post"> <tr> <td><input type="text" value="${id }" maxLength="2" size="2" name="id" /></td> <td><s:property value="name"/></td> <td><s:property value="content"/></td> <td><s:property value="price"/></td> <td><input type="text" name="count" size="2"/></td> <td><input type="submit" value="Add to Car"/></td> </tr> </form> </s:iterator> <s:url var="last" value="show.action"> <s:param name="pageNow" value="pageNow-1"/> </s:url> <s:url var="next" value="show.action"> <s:param name="pageNow" value="pageNow+1"/> </s:url> <h2><s:a href="%{last}" >Last</s:a> <s:a href="%{next}">Next</s:a></h2> </table> </div> </center> </div> <s:fielderror></s:fielderror> </body> </html>

      而有了商品显示界面View层只剩最后一件核心:购物车显示

    car.jsp:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <%@ page import="com.wss.Dao.Car" %> <%@ page import="java.lang.*" %> <%@ page import="java.util.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'car.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="css/style.css" type="text/css"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript"> function forword(){ window.location.href="suc.jsp"; } </script> </head> <body> <%@include file="head.jsp" %> <center> <div id="container"> <h2><font color="#FF7F00">ShopCar</font></h2> <form action="pay_success.jsp" method="post"> <table class="zebra" border="2" > <thead> <tr> <td>Product ID</td> <td>Product Name</td> <td>Product Content</td> <td>Product Price</td> <td>Buy Num</td> </tr> </thead> <s:iterator value="#session.ids" var="car" > <tbody> <tr> <td><s:property value="#car.product.id"/></td> <td><s:property value="#car.product.name"/></td> <td><s:property value="#car.product.content"/></td> <td><s:property value="#car.product.price"/></td> <td><s:property value="#car.count"/></td> </tr> </tbody> </s:iterator> <tr > <td><font color="red">Result</font></td> <td colspan="3" color="#FF7F00"><font color="red">      The total value of all commodities:<s:property value="#session.totalPrice"/>$</font></td> <td><input type="submit" value="Go to pay!"/></td> </tr> </table> <td><input type="button" value="Back to list!" onClick="forword()"/></td> </form> </div> </center> </body> </html>

      而view层只是用户所能见的界面。后台的逻辑大体分为两个部分:

    一、Dao层

       1、user类:控制用户变量和连接用户库

       2、production类:控制产品变量和链接产品数据库

       3、car类:控制购物车内产品数目变量

    二、Action层

       1、LoginAction:控制登录检测

       2、LogoutAction:控制登出检测

       3、showproductionAction:控制产品分页显示

       4、carAction:产品数目逻辑

    搞清这些我们就可以轻松实现:

      

    而最关键的则是.xml文件的配置:

    1.web.xml
    <?
    xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>

       

    2.struts.xml
    <?
    xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <constant name="struts.i18n.encoding" value="gb2312"></constant> <package name="shop" namespace="/" extends="struts-default"> <action name="login" class="com.String.action.LoginAction"> <result name="success" type="chain">show</result> <result name="error">/login.jsp</result> </action> <action name="show" class="com.String.action.ShowAction"> <result name="success">/suc.jsp</result> <result name="error">/error.jsp</result> </action> <action name="car" class="com.String.action.CarAction"> <result name="success">/car.jsp</result> </action> </package> </struts>

     配置好这些之后我们进行测试:

    能力有限-暂时这样了

  • 相关阅读:
    Erlang 督程 启动和结束子进程
    cocos2d-x 3.0 内存管理机制
    c语言基本数据类型
    4星|《剑桥中国经济史:古代到19世纪》:经济学视角看中国古代史
    孟晚舟三种结局;共享单车大败局;失业潮不会来:4星|《财经》2018年第30期
    2018左其盛差评榜,罕见的差书榜
    2018左其盛好书榜,没见过更好的榜单
    罗振宇时间的朋友2018跨年演讲中最重要的35句话
    中国土地制度与房价走势相关9本书
    2星|水木然《世界在变软》:肤浅的朋友圈鸡汤文
  • 原文地址:https://www.cnblogs.com/Stringair/p/6753528.html
Copyright © 2011-2022 走看看