安徽快3计划软件a@b.com (鸡啄c?http://www.vkzldl.live/software/772.htmlMon, 25 Mar 2019 16:59:37 +0800http://www.vkzldl.live/software/772.html      鸡啄c的各位朋友Q久q了Q?/p>

      因ؓq些q确实比较忙Q所以博客更新频率有所下降。看到有人问是不是出什么事情了Q在此回{,实木有Q多谢大家关心?/p>

      目前鸡啄c的团队在正常开发之余,q尚有精力可以承接外部项目。大家如果有pȝ要开发可以找鸡啄c_Q无论是C/S架构q是B/S架构Q我们都可以搞定?/p>

      Ƣ迎前来z谈合作Q名额有限?/p>

      邮箱Qjizhuomi@126.com

      QQQ?312565270

]]>
软g开?/category>http://www.vkzldl.live/software/772.html#commenthttp://www.vkzldl.live/http://www.vkzldl.live/feed.asp?cmt=772http://www.vkzldl.live/cmd.asp?act=tb&id=772&key=a8de8094白照样L的VLAN原理讲解a@b.com (鸡啄c?http://www.vkzldl.live/software/771.htmlThu, 12 Jul 2018 19:34:04 +0800http://www.vkzldl.live/software/771.htmlZ么需要VLAN

1. 什么是VLAN?

VLAN(Virtual LAN)Q翻译成中文?ldquo;虚拟局域网”。LAN可以是由数几台家用计算机构成的|络Q也可以是数以百计的计算机构成的企业|络。VLAN所指的LANҎ使用路由器分割的|络——也就是广播域?/p>

在此让我们先复习一下广播域的概c广播域Q指的是q播?目标MAC地址全部?)所能传递到的范_亦即能够直接通信的范围。严格地_q不仅仅是广播Q多播(Multicast Frame)和目标不明的单播?Unknown Unicast Frame)也能在同一个广播域中畅行无阅R?/p>

本来Q二层交换机只能构徏单一的广播域Q不q用VLAN功能后,它能够将|络分割成多个广播域?/p>

2.未分割广播域?hellip;…

那么Qؓ什么需要分割广播域?那是因ؓQ如果仅有一个广播域Q有可能会媄响到|络整体的传输性能。具体原因,请参看附囑֊q解?/p>

图中Q是一个由5C层交换机(交换??)q接了大量客h构成的网l。假设这Ӟ计算机A需要与计算机B通信。在Z以太|的通信中,必须在数据中指定目标MAC地址才能正常通信Q因此计机A必须先广?ldquo;ARPh(ARP Request)信息”Q来试获取计算机B的MAC地址?/p>

交换?收到q播?ARPh)后,会将它{发给除接收端口外的其他所有端口,也就是Flooding了。接着Q交换机2收到q播帧后也会Flooding。交换机3??也还会Flooding。最lARPh会被转发到同一|络中的所有客h上?/p>

请大家注意一下,q个ARPh原本是ؓ了获得计机B的MAC地址而发出的。也是_只要计算机B能收到就万事大吉了。可是事实上Q数据却传遍整个网l,D所有的计算机都收到了它。如此一来,一斚wq播信息消耗了|络整体的带宽,另一斚wQ收到广播信息的计算要消耗一部分CPU旉来对它进行处理。造成了网l带宽和CPUq算能力的大量无谓消耗?/p>

3. q播信息是那么经常发出的?

dq里Q您也许会问Q广播信息真是那么频J出现的?

{案是:是的!实际上广播会非帔RJ地出现。利用TCP/IP协议栈通信Ӟ除了前面出现的ARP外,q有可能需要发出DHCP、RIP{很多其他类型的q播信息?/p>

ARPq播Q是在需要与其他L通信时发出的。当客户求DHCP服务器分配IP地址Ӟ必d出DHCP的广播。而用RIP作ؓ路由协议Ӟ每隔30U\由器都会寚wq的其他路由器广播一ơ\׃息。RIP以外的其他\由协议用多播传输\׃息,q也会被交换{?Flooding)。除了TCP/IP以外QNetBEUI、IPX和Apple Talk{协议也l常需要用到广播。例如在Windows下双L开“|络计算?rdquo;时就会发出广?多播)信息?Windows XP除外……)

MQ广播就在我们n辏V下面是一些常见的q播通信Q?/p>

(1)ARPhQ徏立IP地址和MAC地址的映关pR?/p>

(2)RIPQ一U\由协议?/p>

(3)DHCPQ用于自动设定IP地址的协议?/p>

(4)NetBEUIQWindows下用的|络协议?/p>

(5)IPXQNovellNetware使用的网l协议?/p>

(6)Apple TalkQ苹果公司的Macintosh计算Z用的|络协议?/p>

如果整个|络只有一个广播域Q那么一旦发出广播信息,׃传遍整个|络Qƈ且对|络中的L带来额外的负担。因此,在设计LANӞ需要注意如何才能有效地分割q播域?/p>

4.q播域的分割与VLAN的必要?/p>

分割q播域时Q一般都必须使用到\由器。用\由器后,可以以\由器上的|络接口(LAN Interface)为单位分割广播域?/p>

但是Q通常情况下\由器上不会有太多的网l接口,其数目多??个左叟뀂随着宽带q接的普及,宽带路由?或者叫IP׃n?变得较ؓ常见Q但是需要注意的是,它们上面虽然带着多个(一般ؓ4个左?q接LAN一侧的|络接口Q但那实际上是\由器内置的交换机Qƈ不能分割q播域?/p>

况且使用路由器分割广播域的话Q所能分割的个数完全取决于\由器的网l接口个敎ͼ使得用户无法自由地根据实际需要分割广播域?/p>

与\由器相比Q二层交换机一般带有多个网l接口。因此如果能使用它分割广播域Q那么无疑运用上的灵zL会大大提高?/p>

用于在二层交换机上分割广播域的技术,是VLAN。通过利用VLANQ我们可以自p计广播域的构成,提高|络设计的自由度?/p>

实现VLAN的机?/strong>

1. 实现VLAN的机?/p>

在理解了“Z么需要VLAN”之后Q接下来让我们来了解一下交换机是如何用VLAN分割q播域的?/p>

首先Q在一台未讄MVLAN的二层交换机上,Mq播帧都会被转发l除接收端口外的所有其他端?Flooding)。例如,计算机A发送广播信息后Q会被{发给端口2???/p>

q时Q如果在交换Z生成U、蓝两个VLAN;同时讄端口1?属于U色VLAN、端??属于蓝色VLAN。再从A发出q播帧的话,交换机就只会把它转发l同属于一个VLAN的其他端?mdash;—也就是同属于U色VLAN的端?Q不会再转发l属于蓝色VLAN的端口?/p>

同样QC发送广播信息时Q只会被转发l其他属于蓝色VLAN的端口,不会被{发给属于U色VLAN的端口?/p>

pPVLAN通过限制q播帧{发的范围分割了广播域。上图中Z便于说明Q以U、蓝两色识别不同的VLANQ在实际使用中则是用“VLAN ID”来区分的?/p>

2.直观地描qVLAN

如果要更为直观地描述VLAN的话Q我们可以把它理解ؓ一C换机在逻辑上分割成了数C换机。在一C换机上生成红、蓝两个VLANQ也可以看作是将一C换机换做一U一蓝两台虚拟的交换机?/p>

在红、蓝两个VLAN之外生成新的VLANӞ可以惌成又d了新的交换机?/p>

但是QVLAN生成的逻辑上的交换机是互不盔R的。因此,在交换机上设|VLAN后,如果未做其他处理QVLAN间是无法通信的?/p>

明明接在同一C换机上,但却偏偏无法通信——q个事实也许让h难以接受。但它既是VLAN方便易用的特征,又是使VLAN令h难以理解的原因?/p>

3. 需要VLAN间通信时怎么?/p>

那么Q当我们需要在不同的VLAN间通信时又该如何是好呢?

请大家再ơ回忆一下:VLAN是广播域。而通常两个q播域之间由路由器连接,q播域之间来往的数据包都是p\由器中的。因此,VLAN间的通信也需要\由器提供中服务Q这被称?ldquo;VLAN间\?rdquo;?/p>

VLAN间\由,可以使用普通的路由器,也可以用三层交换机。其中的具体内容Q等有机会再l说吧。在q里希望大家先记住不同VLAN间互盔R信旉要用到\由功能?/p>

VLAN的访问链?Access Link)

1.交换机的端口cd

交换机的端口Q可以分Z下两U:

(1)讉K链接(Access Link)

(2)汇聚链接(Trunk Link)

接下来就让我们来依次学习q两U不同端口的特征。这一Ԍ首先学习“讉K链接”?/p>

2.讉K链接

讉K链接Q指的是“只属于一个VLANQ且仅向该VLAN转发数据?rdquo;的端口。在大多数情况下Q访问链接所q的是客h?/p>

通常讄VLAN的顺序是Q?/p>

(1)生成VLAN

(2)讑֮讉K链接(军_各端口属于哪一个VLAN)

讑֮讉K链接的手法,可以是事先固定的、也可以是根据所q的计算动态改变设定。前者被UCؓ“静态VLAN”、后者自然就?ldquo;动态VLAN”了?/p>

?静态VLAN——Z端口

静态VLAN又被UCؓZ端口的VLAN(PortBased VLAN)。顾名思义Q就是明指定各端口属于哪个VLAN的设定方法?/p>

׃需要一个个端口地指定,因此当网l中的计机数目过一定数?比如数百?后,讑֮操作׃变得烦杂无比。ƈ且,客户机每ơ变更所q端口,都必d时更改该端口所属VLAN的设?mdash;—q显然不适合那些需要频J改变拓补结构的|络?/p>

?动态VLAN

另一斚wQ动态VLAN则是Ҏ每个端口所q的计算机,随时改变端口所属的VLAN。这可以避免上q的更改讑֮之类的操作。动态VLAN可以大致分ؓ3c:

(1)ZMAC地址的VLAN(MAC Based VLAN)

(2)Z子网的VLAN(Subnet Based VLAN)

(3)Z用户的VLAN(User Based VLAN)

光的差异,主要在于ҎOSI参照模型哪一层的信息军_端口所属的VLAN?/p>

?ZMAC地址的VLAN

ZMAC地址的VLANQ就是通过查询q记录端口所q计机上网卡的MAC地址来决定端口的所属。假定有一个MAC地址“A”被交换机讑֮为属于VLAN “10”Q那么不论MAC地址?ldquo;A”的这台计机q在交换机哪个端口,该端口都会被划分到VLAN 10中去。计机q在端口1Ӟ端口1属于VLAN 10;而计机q在端口2Ӟ则是端口2属于VLAN 10?/p>

׃是基于MAC地址军_所属VLAN的,因此可以理解是一U在OSI的第二层讑֮讉K链接的办法?/p>

但是Q基于MAC地址的VLANQ在讑֮时必调查所q接的所有计机的MAC地址q加以登录。而且如果计算Z换了|卡Q还是需要更改设定?/p>

?ZIP地址的VLAN

Z子网的VLANQ则是通过所q计机的IP地址Q来军_端口所属VLAN的。不像基于MAC地址的VLANQ即使计机因ؓ交换了网卡或是其他原因导致MAC地址改变Q只要它的IP地址不变Q就仍可以加入原先设定的VLAN?/p>

因此Q与ZMAC地址的VLAN相比Q能够更为简便地改变|络l构。IP地址是OSI参照模型中第三层的信息,所以我们可以理解ؓZ子网的VLAN是一U在OSI的第三层讑֮讉K链接的方法?/p>

Z用户的VLANQ则是根据交换机各端口所q的计算Z当前d的用P来决定该端口属于哪个VLAN。这里的用户识别信息Q一般是计算机操作系l登录的用户Q比如可以是Windows域中使用的用户名。这些用户名信息Q属于OSIW四层以上的信息?/p>

ȝ来说Q决定端口所属VLAN时利用的信息在OSI中的层面高Q就适于构徏灉|多变的网l?/p>

VLAN的汇聚链?Trunk Link)

1.需要设|跨多C换机的VLAN?hellip;…

到此为止Q我们学习的都是使用单台交换|VLAN时的情况。那么,如果需要设|跨多C换机的VLAN时又如何?

在规划企业|络Ӟ很有可能会遇到隶属于同一部门的用户分散在同一座徏{物中的不同楼层的情况,q时可能需要考虑到如何跨多C换机讄VLAN的问题了。假设有如下图所C的|络Q且需要将不同楼层的A、C和B、D讄为同一个VLAN?/p>

q时最关键的就?ldquo;交换?和交换机2该如何连接才好呢?”

最单的ҎQ自然是在交换机1和交换机2上各设一个红、蓝VLAN专用的接口ƈ互联了?/p>

但是Q这个办法从扩展性和理效率来看都不好。例如,在现有网l基上再新徏VLANӞZ让这个VLAN能够互通,需要在交换机间q接新的|线。徏{物楼层间的U向布线是比较麻烦的Q一般不能由基层理人员随意q行。ƈ且,VLAN多Q楼层间(严格地说是交换机?互联所需的端口也来多Q交换机端口的利用效率低是对资源的一U浪贏V也限制了网l的扩展?/p>

Z避免q种低效率的q接方式Qh们想办法让交换机间互联的|线集中C根上Q这时用的是汇聚链接(Trunk Link)?/p>

2.何谓汇聚链接?

汇聚链接(Trunk Link)指的是能够{发多个不同VLAN的通信的端口?/p>

汇聚链\上流通的数据帧,都被附加了用于识别分属于哪个VLAN的特D信息?/p>

现在再让我们回过头来考虑一下刚才那个网l如果采用汇聚链路又会如何呢?用户只需要简单地交换机间互联的端口讑֮为汇聚链接就可以了。这时用的|线q是普通的UTPU,而不是什么其他的Ҏ布线。图例中是交换机间互联,因此需要用交叉U来q接?/p>

接下来,让我们具体看看汇聚链接是如何实现跨越交换机间的VLAN的?/p>

A发送的数据帧从交换?l过汇聚链\到达交换?Ӟ在数据上附加了表示属于U色VLAN的标记?/p>

交换?收到数据帧后Q经q检查VLAN标识发现q个数据帧是属于U色VLAN的,因此去除标记后根据需要将复原的数据只{发给其他属于U色VLAN的端口。这时的转送,是指l过认目标MAC地址q与MAC地址列表比对后只转发l目标MAC地址所q的端口。只有当数据帧是一个广播、多播或是目标不明的Ӟ它才会被转发到所有属于红色VLAN的端口?/p>

蓝色VLAN发送数据时的情Ş也与此相同?/p>

通过汇聚链\旉加的VLAN识别信息Q有可能支持标准?ldquo;IEEE 802.1Q”协议Q也可能是Cisco产品独有?ldquo;ISL(Inter Switch Link)”。如果交换机支持q些规格Q那么用户就能够高效率地构筑横跨多台交换机的VLAN?/p>

另外Q汇聚链路上通着多个VLAN的数据,自然负蝲较重。因此,在设定汇聚链接时Q有一个前提就是必L?00Mbps以上的传输速度?/p>

另外Q默认条件下Q汇聚链接会转发交换Z存在的所有VLAN的数据。换一个角度看Q可以认为汇聚链?端口)同时属于交换Z所有的VLAN。由于实际应用中很可能ƈ不需要{发所有VLAN的数据,因此Z减轻交换机的负蝲、也Z减少对带宽的费Q我们可以通过用户讑֮限制能够l由汇聚链\互联的VLAN?/p>

关于IEEE 802.1Q和ISL的具体内容,在下一讲中提到?/p>

3.讉K链接的ȝ

lg所qͼ讑֮讉K链接的手法有静态VLAN和动态VLAN两种Q其中动态VLAN又可以l细分成几个类?/p>

其中Z子网的VLAN和基于用LVLAN有可能是|络讑֤厂商使用独有的协议实现的Q不同厂商的讑֤之间互联有可能出现兼Ҏ问?因此在选择交换机时Q一定要注意事先认?/p>

下表ȝ了静态VLAN和动态VLAN的相关信息?/p>

U类解说

静态VLAN(Z端口的VLAN)交换机的各端口固定指派lVLAN

动态VLANZMAC地址的VLANҎ各端口所q计机的MAC地址讑֮

Z子网的VLANҎ各端口所q计机的IP地址讑֮

Z用户的VLANҎ端口所q计机上登录用戯?/p>

VLAN的汇聚方?mdash;—IEEE802.1Q与ISL

1.汇聚方式

在交换机的汇聚链接上Q可以通过Ҏ据附加VLAN信息Q构多C换机的VLAN?/p>

附加VLAN信息的方法,最h代表性的有:

(1)IEEE 802.1Q

(2)ISL

现在p我们看看q两U协议分别如何对数据帧附加VLAN信息?/p>

2.IEEE 802.1Q

IEEE 802.1QQ俗U?ldquo;Dot One Q”Q是l过IEEE认证的对数据帧附加VLAN识别信息的协议?/p>

在此Q请大家先回忆一下以太网数据帧的标准格式?/p>

IEEE 802.1Q所附加的VLAN识别信息Q位于数据?ldquo;发送源MAC地址”?ldquo;cd?rdquo;(Type Field)之间。具体内容ؓ2字节的TPID(Tag Protocol IDentifier)?字节的TCI(Tag Control Information)Q共?字节?/p>

在数据中添加了4字节的内容,那么CRCD然也会有所变化。这时数据上的CRC是插入TPID、TCI后,对包括它们在内的整个数据帧重新计后所得的倹{?/p>

而当数据帧离开汇聚链\ӞTPID和TCI会被去除Q这时还会进行一ơCRC的重新计?/p>

TPID字段在以太网报文中所处位|与不带VLAN Tag的报文中协议cd字段所处位|相同。TPID的值固定ؓ0x8100Q它标示|络帧承载的802.1QcdQ交换机通过它来定数据帧内附加了基于IEEE 802.1Q的VLAN信息。而实质上的VLAN IDQ是TCI中的12位元。由于d?2位,因此最多可供识?096个VLAN?/p>

ZIEEE 802.1Q附加的VLAN信息Q就像在传递物品时附加的标{。因此,它也被称?ldquo;标签型VLAN”(Tagging VLAN)?/p>

3.ISL(Inter Switch Link)

ISLQ是Cisco产品支持的一U与IEEE 802.1QcM的、用于在汇聚链\上附加VLAN信息的协议?/p>

使用ISL后,每个数据帧头部都会被附加26字节?ldquo;ISL包头(ISL Header)”Qƈ且在帧尾带上通过对包括ISL包头在内的整个数据q行计算后得到的4字节CRC倹{换而言之,是d增加?0字节的信息?/p>

在用ISL的环境下Q当数据帧离开汇聚链\Ӟ只要单地去除ISL包头和新CRC可以了。由于原先的数据帧及其CRC都被完整保留Q因此无需重新计算CRC?/p>

ISL有如用ISL包头和新CRC原数据帧整个包裹v来,因此也被UCؓ“装型VLAN”(Encapsulated VLAN)?/p>

需要注意的是,不论是IEEE802.1Q?ldquo;Tagging VLAN”Q还是ISL?ldquo;Encapsulated VLAN”Q都不是很严密的U谓。不同的书籍与参考资料中Q上q词语有可能被؜合用,因此需要大家在学习时格外注意?/p>

q且׃ISL是Cisco独有的协议,因此只能用于Cisco|络讑֤之间的互联?/p>

VLAN间\?/strong>

1.VLAN间\q必要?/p>

Ҏ目前为止学习的知识,我们已经知道两台计算机即使连接在同一C换机上,只要所属的VLAN不同无法直接通信。接下来我们要学习的就是如何在不同的VLAN间进行\由,使分属不同VLAN的主够互盔R信?/p>

首先Q先来复习一下ؓ什么不同VLAN间不通过路由无法通信。在LAN内的通信Q必d数据帧头中指定通信目标的MAC地址。而ؓ了获取MAC地址QTCP/IP协议下用的是ARP。ARP解析MAC地址的方法,则是通过q播。也是_如果q播报文无法到达Q那么就无从解析MAC地址Q亦x法直接通信?/p>

计算机分属不同的VLANQ也意味着分属不同的广播域Q自然收不到彼此的广播报文。因此,属于不同VLAN的计机之间无法直接互相通信。ؓ了能够在VLAN间通信Q需要利用OSI参照模型中更高一?mdash;—|络层的信息(IP地址)来进行\由。关于\q具体内容Q以后有Z再详l解说吧?/p>

路由功能Q一般主要由路由器提供。但在今天的局域网里,我们也经常利用带有\由功能的交换?mdash;—三层交换?Layer 3 Switch)来实现。接下来p我们分别看看使用路由器和三层交换行VLAN间\由时的情c?/p>

2.使用路由器进行VLAN间\?/p>

在用\由器q行VLAN间\由时Q与构徏横跨多台交换机的VLAN时的情况cMQ我们还是会遇到“该如何连接\由器与交换机”q个问题。\由器和交换机的接U方式,大致有以下两U:

(1)\由器与交换机上的每个VLAN分别q接

(2)不论VLAN有多个Q\由器与交换机都只用一条网U连?/p>

最Ҏ惛_的,当然q是“把\由器和交换机以VLAN为单位分别用|线q接”了。将交换Z用于和\由器互联的每个端口设问链?Access Link)Q然后分别用|线与\由器上的独立端口互联。如下图所C,交换Z?个VLANQ那么就需要在交换Z预留2个端口用于与路由器互?路由器上同样需要有2个端?两者之间用2条网U分别连接?/p>

如果采用q个办法Q大家应该不难想象它的扩展性很成问题。每增加一个新的VLANQ都需要消耗\由器的端口和交换Z的访问链接,而且q需要重新布设一条网Uѝ而\由器Q通常不会带有太多LAN接口的。新建VLANӞZ对应增加的VLAN所需的端口,必d路由器升U成带有多个LAN接口的高端品,q部分成本、还有重新布U所带来的开销Q都使得q种接线法成ZU不受欢q的办法?/p>

那么Q第二种办法“不论VLAN数目多少Q都只用一条网U连接\由器与交换机”?当用一条网U连接\由器与交换机、进行VLAN间\由时Q需要用到汇聚链接?/p>

具体实现q程为:首先用于连接\由器的交换机端口设ؓ汇聚链接(Trunk Link)Q而\由器上的端口也必L持汇聚链路。双方用于汇聚链路的协议自然也必ȝ同。接着在\由器上定义对应各个VLAN?ldquo;子接?rdquo;(Sub Interface)。尽实际与交换接的物理端口只有一个,但在理论上我们可以把它分割ؓ多个虚拟端口?/p>

VLAN交换机从逻辑上分割成了多収ͼ因而用于VLAN间\q路由器,也必L有分别对应各个VLAN的虚拟接口?/p>

采用q种Ҏ的话Q即使之后在交换Z新徏VLANQ仍只需要一条网U连接交换机和\由器。用户只需要在路由器上新设一个对应新VLAN的子接口可以了。与前面的方法相比,扩展性要强得多,也不用担心需要升ULAN接口C的路由器或是重新布Uѝ?/p>

3.同一VLAN内的通信

接下来,我们l箋学习使用汇聚链\q接交换Z路由器时QVLAN间\由是如何q行的。如下图所C,为各台计机以及路由器的子接口设定IP地址?/p>

U色VLAN(VLAN ID=1)的网l地址?92.168.1.0/24Q蓝色VLAN(VLAN ID=2)的网l地址?92.168.2.0/24。各计算机的MAC地址分别为A/B/C/DQ\由器汇聚链接端口的MAC地址为R。交换机通过对各端口所q计机MAC地址的学习,生成如下的MAC地址列表?/p>

首先考虑计算机A与同一VLAN内的计算机B之间通信时的情Ş?/p>

计算机A发出ARPh信息Q请求解析B的MAC地址。交换机收到数据帧后Q检索MAC地址列表中与收信端口同属一个VLAN的表V结果发玎ͼ计算机Bq接在端?上,于是交换机将数据帧{发给端口2Q最l计机B收到该。收发信双方同属一个VLAN之内的通信Q一切处理均在交换机内完成?/p>

4.不同VLAN间的通信

接下来是q一讲的核心内容Q不同VLAN间的通信。让我们来考虑一下计机A与计机C之间通信时的情况?/p>

计算机A从通信目标的IP地址(192.168.2.1)得出C与本Z属于同一个网Dc因此会向设定的默认|关(DefaultGatewayQGW)转发数据帧。在发送数据之前Q需要先用ARP获取路由器的MAC地址?/p>

得到路由器的MAC地址R后,接下来就是按图中所C的步骤发送往Cȝ数据帧。①的数据中,目标MAC地址是\由器的地址R、但内含的目标IP地址仍是最l要通信的对象C的地址。这一部分的内容,涉及到局域网内经q\由器转发时的通信步骤Q有Z再详l解说吧?/p>

交换机在端口1上收到①的数据后,索MAC地址列表中与端口1同属一个VLAN的表V由于汇聚链路会被看作属于所有的VLANQ因此这时交换机的端?也属于被参照对象。这样交换机q道往MAC地址R发送数据Q需要经q端?转发?/p>

从端?发送数据Ӟ׃它是汇聚链接Q因此会被附加上VLAN识别信息。由于原先是来自U色VLAN的数据Q因此如图中②所C,会被加上U色VLAN的识别信息后q入汇聚链\。\由器收到②的数据帧后Q确认其VLAN识别信息Q由于它是属于红色VLAN的数据Q因此交p责红色VLAN的子接口接收?/p>

接着Q根据\由器内部的\pQ判断该向哪里中l?/p>

׃目标|络192.168.2.0/24是蓝色VLANQ,且该|络通过子接口与路由器直q,因此只要从负责蓝色VLAN的子接口转发可以了。这Ӟ数据帧的目标MAC地址被改写成计算机C的目标地址;q且׃需要经q汇聚链路{发,因此被附加了属于蓝色VLAN的识别信息。这是图中③的数据帧?/p>

交换机收到③的数据后,ҎVLAN标识信息从MAC地址列表中检索属于蓝色VLAN的表V由于通信目标——计算机Cq接在端?上、且端口3为普通的讉K链接Q因此交换机会将数据帧去除VLAN识别信息?数据帧④)转发l端?Q最l计机C才能成功地收到这个数据?/p>

q行VLAN间通信Ӟ即通信双方都连接在同一C换机上,也必ȝq:“发送方——交换?mdash;—路由?mdash;—交换?mdash;—接收?rdquo;q样一个流E?/p>

三层交换?/strong>

1.使用路由器进行VLAN间\由时的问?/p>

现在Q我们知道只要能提供VLAN间\由,p够分属不同VLAN的计机互相通信。但是,如果使用路由器进行VLAN间\q话,随着VLAN之间量的不断增加,很可能导致\由器成ؓ整个|络的瓶颈?/p>

交换Z用被UCؓASIC(ApplicationSpecified Integrated Circuit)的专用硬件芯片处理数据的交换操作,在很多机型上都能实现以缆UK度(Wired Speed)交换。而\由器Q则基本上是Z软g处理的。即使以~线速度接收到数据包Q也无法在不限速的条g下{发出去,因此会成为速度瓉。就VLAN间\p言Q流量会集中到\由器和交换机互联的汇聚链路部分,q一部分其特别Ҏ成ؓ速度瓉。ƈ且从g上看Q由于需要分别设|\由器和交换机Q在一些空间狭的环境里可能连讄的场所都成问题?/p>

2.三层交换?Layer 3 Switch)

Z解决上述问题Q三层交换机应运而生。三层交换机Q本质上是“带有路由功能?二层)交换?rdquo;。\由属于OSI参照模型中第三层|络层的功能Q因此带有第三层路由功能的交换机才被UCؓ“三层交换?rdquo;?/p>

关于三层交换机的内部l构Q可以参照下面的图?/p>

在一台本体内Q分别设|了交换机模块和路由器模?而内|的路由模块与交换模块相同,使用ASICg处理路由。因此,与传l的路由器相比,可以实现高速\由。ƈ且,路由与交换模块是汇聚链接的,׃是内部连接,可以保相当大的带宽?/p>

?使用三层交换行VLAN间\?VLAN内通信)

在三层交换机内部数据I竟是怎样传播的呢?基本上,它和使用汇聚链\q接路由器与交换机时的情形相同?/p>

假设有如下图所C的4台计机与三层交换机互联。当使用路由器连接时Q一般需要在LAN接口上设|对应各VLAN的子接口;而三层交换机则是在内部生?ldquo;VLAN接口”(VLAN Interface)。VLAN接口Q是用于各VLAN收发数据的接口?注:在Cisco的Catalystpd交换ZQVLAN Interface被称为SVI——Switched Virtual Interface)

Z与用\由器q行VLAN间\由对比,让我们同h考虑一下计机A与计机B之间通信时的情况。首先是目标地址为B的数据被发C换机;通过索同一VLAN的MAC地址列表发现计算机Bq在交换机的端口2?因此数据转发l端??/p>

?使用三层交换行VLAN间\?VLAN间通信)

接下来设想一下计机A与计机C间通信时的情Ş。针对目标IP地址Q计机A可以判断出通信对象不属于同一个网l,因此向默认网兛_送数?Frame 1)?/p>

交换机通过索MAC地址列表后,l由内部汇聚链接Q将数据帧{发给路由模块。在通过内部汇聚链\Ӟ数据帧被附加了属于红色VLAN的VLAN识别信息(Frame 2)?/p>

路由模块在收到数据Ӟ先由数据帧附加的VLAN识别信息分L出它属于U色VLANQ据此判断由U色VLAN接口负责接收q进行\由处理。因为目标网l?92.168.2.0/24是直q\由器的网l、且对应蓝色VLAN;因此Q接下来׃从蓝色VLAN接口l由内部汇聚链\转发回交换模块。在通过汇聚链\Ӟq次数据帧被附加上属于蓝色VLAN的识别信?Frame 3)?/p>

交换机收到这个后,索蓝色VLAN的MAC地址列表Q确认需要将它{发给端口3。由于端?是通常的访问链接,因此转发前会先将VLAN识别信息去除(Frame 4)。最l,计算机C成功地收C换机转发来的数据帧?/p>

整体的流E,与用外部\由器时的情况十分怼——都需要经q?ldquo;发送方→交换模块→路由模块→交换模块→接收?rdquo;?/p>

加速VLAN间通信的手D?/strong>

1.?Flow)

Ҏ到此为止的学习,我们已经知道VLAN间\由,必须l过外部的\由器或是三层交换机的内置路由模块。但是,有时q不是所有的数据都需要经q\由器(或\由模??/p>

例如Q用FTP(File Transfer Protocol)传输定w为数MB以上的较大的文gӞ׃MTU的限ӞIP协议会将数据分割成小块后传输、ƈ在接收方重新l合。这些被分割的数据,“发送的目标”是完全相同的。发送目标相同,也就意味着同样的目标IP地址、目标端口号(注:特别一下,q里指的是TCP/UDP端口)。自Ӟ源IP地址、源端口号也应该相同。这样一q串的数据流被称?ldquo;?rdquo;(Flow)?/p>

只要流最初的数据正确地\׃后,后的数据理应也会被同样地\由?/p>

据此Q后l的数据不再需要\由器q行路由处理;通过省略反复q行的\由操作,可以q一步提高VLAN间\q速度?/p>

2.加速VLAN间\q机制

接下来,让我们具体考虑一下该如何使用三层交换行高速VLAN间\由?/p>

首先Q整个流的第一块数据,照常׃换机转发→路由器\?rarr;再次׃换机转发到目标所q端口。这Ӟ第一块数据\ql果记录到缓存里保存下来。需要记录的信息有:

(1)目标IP地址

(2)源IP地址

(3)目标TCP/UDP端口?/p>

(4)源TCP/UDP端口?/p>

(5)接收端口?交换?

(6)转发端口?交换?

(7)转发目标MAC地址

{等?/p>

同一个流的第二块以后的数据到达交换机后,直接通过查询先前保存在缓存中的信息查?ldquo;转发端口?rdquo;后就可以转发l目标所q端口了?/p>

q样一来,׃需要再一ơ次l由内部路由模块中Q而仅凭交换机内部的缓存信息就以判断应该转发的端口?/p>

q时Q交换机会对数据帧进行由路由器中l时怼的处理,例如改写MAC地址、IP包头中的TTL和Check Sum校验码信息等?/p>

通过在交换机上缓存\q果,实现了以~线速度(Wired Speed)接收发送方传输来数据的数据、ƈ且能够全速\由、{发给接收斏V?/p>

需要注意的是,cM的加速VLAN间\q手法多由各厂商独有的技术所实现Qƈ且该功能的称谓也因厂商而异。例如,在Cisco的Catalystpd交换ZQ这U功能被UCؓ“多层交换”(Multi Layer Switching)。另外,除了三层交换机的内部路由模块Q外部\由器中的某些机型也支持类似的高速VLAN间\由机制?/p>

传统型\由器存在的意?/strong>

1.路由器的必要?/p>

三层交换机的hQ在问世之初非常昂贵Q但是现在它们的h已经下降了许多。目前国外一些廉h型的售hQ折合成人民币后仅ؓ一万多元,而且q在l箋下降中?/p>

既然三层交换够提供比传统型\由器更ؓ高速的路由处理Q那么网l中q有使用路由器的必要?

{案是:“?rdquo;?/p>

使用路由器的必要性,主要表现在以下几个方面:

(1)用于与WANq接

三层交换机终I是“交换?rdquo;。也是_l大多数机型只配有LAN(以太|?接口。在数高端交换Z也有用于q接WAN的串行接口或是ATM接口Q但在大多数情况下,q接WANq是需要用到\由器?/p>

(2)保证|络安全

在三层交换机上,通过数据包过滤也能确保一定程度的|络安全。但是用\由器所提供的各U网l安全功能,用户可以构徏更ؓ安全可靠的网l?/p>

路由器提供的|络安全功能中,除了最基本的数据包qo功能外,q能ZIPSec构徏VPN(VirtualPrivate Network)、利用RADIUSq行用户认证{等?/p>

(3)支持除TCP/IP以外的异构网l架?/p>

管TCP/IP已经成ؓ当前|络协议架构的主,但还有不网l利用Novell Netware下的IPX/SPX或Macintosh下的AppleTalk{网l协议。三层交换机中,除了部分高端机型外基本上q只支持TCP/IP。因此,在需要用除TCP/IP之外其他|络协议的环境下Q\由器q是必不可少的?/p>

注:在少数高端交换机上,也能支持上述路由器的功能。例如Cisco的Catalyst 6500pdQ就可以选择与WANq接的接口模?q有可选的ZIPSec实现VPN的模?q且也能支持TCP/IP以外的其他网l协议?/p>

2.路由器和交换机配合构建LAN的实?/p>

下面让我们来看一个\由器和交换机搭配构徏LAN的实例?/p>

利用在各楼层配置的二层交换机定义VLANQ连接TCP/IP客户计算机。各楼层间的VLAN间通信Q利用三层交换机的高速\由加以实现。如果网l环境要求高可靠性,q可以考虑冗余配置三层交换机?/p>

与WAN的连接,则通过带有各种|络接口的\由器q行。ƈ且,通过路由器的数据包过滤和VPN{功能实现网l安全。此外,使用路由器还能支持Novell Netware{TCP/IP之外的网l?/p>

只有在充分掌握了二层、三层交换机以及传统路由器的基础上,才能做到物竞其用Q构{出高效率、高性h比的|络?/p>

使用VLAN设计局域网

1.使用VLAN设计局域网的特?/p>

通过使用VLAN构徏局域网Q用戯够不受物理链路的限制而自由地分割q播域?/p>

另外Q通过先前提到的\由器与三层交换机提供的VLAN间\由,能够适应灉|多变的网l构成?/p>

但是Q由于利用VLANҎD|络构成复杂化,因此也会造成整个|络的组成难以把握?/p>

可以q样_在利用VLANӞ除了?ldquo;|络构成灉|多变”q个优点外,q搭配着“|络构成复杂?rdquo;q个~点?/p>

下面Q就让我们来看看具体的实例?/p>

2.不用VLAN的局域网中网l构成的改变

假设有如图所C的?台\由器?C换机构成?ldquo;不用VLAN构徏”的网l?/p>

图中的\由器Q带?个LAN接口。左侧的|络?92.168.1.0/24Q右侧是192.168.2.0/24?/p>

现在如果惛_192.168.1.0/24q个|络上的计算机A转移?92.168.2.0/24上去Q就需要改变物理连接、将A接到右侧的交换机上?/p>

q且Q当需要新增一个地址?92.168.3.0/24的网l时Q还要在路由器上再占用一个LAN接口q添|一C换机。而由于这台\由器上只带了2个LAN接口Q因此ؓ了新增网l还必须\由器升为带?个以上LAN接口的品?/p>

3.使用VLAN的局域网中网l构成的改变

接下来再假设有一个由1台\由器?C换机构成?ldquo;使用VLAN”的局域网。交换机与交换机、交换机与\由器之间均ؓ汇聚链\;q且假设192.168.1.0/24对应U色VLAN?92.168.2.0/24对应蓝色VLAN?/p>

需要将q接在交换机1?92.168.1.0/24q个|段的计机A转属192.168.2.0/24Ӟ无需更改物理布线。只要在交换Z生成蓝色VLANQ然后将计算机A所q的端口1加入到蓝色VLAN中去Q它成问链接即可?/p>

然后Q根据需要设定计机A的IP地址、默认网关等信息可以了。如果IP地址相关的设定是由DHCP获取的,那么在客h斚w无需q行M讑֮修改Q就可以在不同网D间Ud?/p>

利用VLAN后,我们可以在免于改动Q何物理布U的前提下,自由q行|络的逻辑设计。如果所处的工作环境恰恰需要经常改变网l布局Q那么利用VLAN的优势就非常明显了?/p>

q且Q当需要新增一个地址?92.168.3.0/24的网D|Q也只需要在交换Z新徏一个对?92.168.3.0/24的VLANQƈ所需的端口加入它的访问链路就可以了?/p>

如果|络环境中还需要利用外部\由器Q则只要在\由器的汇聚端口上新增一个子接口的设定就可以完成全部操作Q而不需要消耗更多的物理接口(LAN接口)。要使用的是三层交换机内部的路由模块Q则只需要新设一个VLAN接口卛_?/p>

|络环境的成长,往往是难以预的Q很可能l常会出现需要分割现有网l或是增加新|络的情c而充分活用VLAN后,可以轻易地解决q些问题?/p>

4.利用VLAN而导致的|络l构复杂?/p>

虽然利用VLAN可以灉|地构建网l,但是同时Q它也带来了|络l构复杂化的问题?/p>

特别是由于数据流U|交错Q一旦发生故障时Q准定位ƈ排除故障会比较困难?/p>

Z便于理解数据向的复杂化Q假设有下图所C的|络。计机A向计机C发送数据时Q数据流的整体走向如下:

计算机A→交换?→路由?rarr;交换?→交换?→计算机C

首先计算机A向交换机1送出数据(?Q其后数据被转发l\由器(?q行VLAN间\由。\由后的数据,再从汇聚链\q回交换?(?。由于通信目标计算机Cq不直连在交换机1上,因此q需要经q汇聚链路{发到交换?(?。在交换?上,数据最l被转发到C所q的端口2上,q才完成整个程(??/p>

在这个例子中Q仅?C换机构成|络Q其数据已l如此复杂,如果构徏横跨多台交换机的VLAN的话Q每个数据流的流向显然会更加难以把握?/p>

5. |络的逻辑l构与物理结?/p>

Z对应日渐复杂化的数据,理员需要从“逻辑l构”?ldquo;物理l构”两方面入手,把握好网l的现状?/p>

物理l构Q指的是从物理层和数据链路层观察到的|络的现Ӟ表示了网l的物理布线形态和VLAN的设定等{?/p>

而逻辑l构Q则表示从网l层以上的层面观察到的网l结构。下面我们就试着以\由器Z心分析一个IP|络的逻辑l构?/p>

q是先前的那个例子,描绘了布UŞ态和VLAN讑֮?ldquo;物理l构”如下图所C?/p>

分析q个物理l构q{换成以\由器Z心的逻辑l构后,会得到如下的逻辑l构图。当我们需要进行\由或是数据包qo的设定时Q都必须在逻辑l构的基上进行?/p>

把握q两U网l结构图的区别是十分重要的,特别是在VLAN和三层交换机大行光的现代企业|络当中?/p>]]>软g开?/category>http://www.vkzldl.live/software/771.html#commenthttp://www.vkzldl.live/http://www.vkzldl.live/feed.asp?cmt=771http://www.vkzldl.live/cmd.asp?act=tb&id=771&key=c0a9ef08SSH电商目实战之十Q商品类基本模块的搭?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/770.html</link><pubDate>Wed, 07 Mar 2018 08:41:55 +0800</pubDate><guid>http://www.vkzldl.live/software/770.html</guid><description><![CDATA[<p>      前面我们完成了与商品cd相关的业务逻辑Q接下来我们开始做具体商品部分?/p><p>  <span style="font-size: medium;"><strong>1. 数据库徏表ƈ映射Model</strong></span></p><p>  首先我们在数据库中新Z张表Q然后用逆向工程表映射成Modelc,表如下:</p><div class="codeText"><div class="codeHead">SQL代码</div><ol start="1" class="dp-sql"> <li class="alt"><span><span>/*=============================*/    </span></span></li> <li><span>/* <span class="keyword">Table</span><span>: 商品表结?nbsp;           */    </span></span></li> <li class="alt"><span>/*=============================*/    </span></li> <li><span><span class="keyword">create</span><span> </span><span class="keyword">table</span><span> product    </span></span></li> <li class="alt"><span>(    </span></li> <li><span>   /* 商品~号,自动增长 */    </span></li> <li class="alt"><span>   id                  <span class="keyword">int</span><span> </span><span class="keyword">primary</span><span> </span><span class="keyword">key</span><span> </span><span class="op">not</span><span> </span><span class="op">null</span><span> auto_increment,    </span></span></li> <li><span>   /* 商品名称 */    </span></li> <li class="alt"><span>   <span class="keyword">name</span><span>                </span><span class="keyword">varchar</span><span>(20),    </span></span></li> <li><span>   /* 商品h */    </span></li> <li class="alt"><span>   price               <span class="keyword">decimal</span><span>(8,2),    </span></span></li> <li><span>   /* 商品囄 */    </span></li> <li class="alt"><span>   pic                 <span class="keyword">varchar</span><span>(200),    </span></span></li> <li><span>   /* 商品单介l?nbsp;*/    </span></li> <li class="alt"><span>   remark              longtext,    </span></li> <li><span>   /* 商品详细介绍 */    </span></li> <li class="alt"><span>   xremark             longtext,    </span></li> <li><span>   /* 商品生日期 */    </span></li> <li class="alt"><span>   <span class="keyword">date</span><span>                </span><span class="keyword">timestamp</span><span> </span><span class="keyword">default</span><span> </span><span class="func">CURRENT_TIMESTAMP</span><span>,    </span></span></li> <li><span>   /* 是否为推荐商?推荐商品才有可能昄在商城首?nbsp;*/    </span></li> <li class="alt"><span>   commend             bool,    </span></li> <li><span>   /* 是否为有效商?有效商品才有可能昄在商城首?nbsp;*/    </span></li> <li class="alt"><span>   <span class="keyword">open</span><span>                bool,    </span></span></li> <li><span>   /* 商品所在的cd~号*/    </span></li> <li class="alt"><span>   cid                  <span class="keyword">int</span><span>,    </span></span></li> <li><span>   <span class="keyword">constraint</span><span> cid_FK </span><span class="keyword">foreign</span><span> </span><span class="keyword">key</span><span>(cid) </span><span class="keyword">references</span><span> category(id)    </span></span></li> <li class="alt"><span>);    </span></li></ol></div><p>  使用逆向工程映射为Modelcd不赘qCQ前面有提到如何使用逆向工程生成Model?/p><p> <span style="font-size: medium;"><strong> 2. 完成商品cȝService层和Action的架?/strong></span></p><p>  <strong>2.1 商品cȝService层架?/strong></p><p>  与前面category一Pproduct也得有个service来操作与商品相关的业务逻辑Q所以我们得写一个ProductService和ProductServiceImpl的架构出来,具体如下Q?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="comment">//ProductService接口l承BaseService<Product>  </span><span>  </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">interface</span><span> ProductService </span><span class="keyword">extends</span><span> BaseService<Product> {    </span></span></li> <li class="alt"><span>        </span></li> <li><span>}    </span></li> <li class="alt"><span>    </span></li> <li><span><span class="comment">//ProductServiceImpl实现cȝ承BaseServiceImpl<Product>Qƈ实现上面的ProductService接口  </span><span>  </span></span></li> <li class="alt"><span><span class="annotation">@Service</span><span>(</span><span class="string">"productService"</span><span>)    </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ProductServiceImpl </span><span class="keyword">extends</span><span> BaseServiceImpl<Product> </span><span class="keyword">implements</span><span> ProductService {    </span></span></li> <li class="alt"><span>    </span></li> <li><span>}    </span></li></ol></div><p>  <strong>2.2 商品cȝAction架构</strong></p><p>  首先得完善一下BaseAction中关于Service层的注解</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"baseAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> BaseAction<T> </span><span class="keyword">extends</span><span> ActionSupport </span><span class="keyword">implements</span><span> RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {    </span></span></li> <li><span>    </span></li> <li class="alt"><span>    <span class="annotation">@Resource</span><span>    </span></span></li> <li><span>    <span class="keyword">protected</span><span> ProductService productService;    </span></span></li> <li class="alt"><span>    </span></li> <li><span>        <span class="comment">//其他代码省略Q还是原来的代码……    </span><span>  </span></span></li> <li class="alt"><span>}  </span></li></ol></div><p>  然后我们写一个ProductActionl承该方法:</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ProductAction </span><span class="keyword">extends</span><span> BaseAction<Product> {    </span></span></li> <li><span>        </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  xQ关于商品的后台架构基本搭建好了,接下来就是完善里面的具体功能和业务逻辑了?/p><p>  <span style="font-size: medium;"><strong>3. 完成前台的基本结?/strong></span></p><p>  前台的基本结构和商品cȝ一P我们看一下已l完成的商品cȝ前台都有哪些文gQ?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160510214952052.png" alt="SSH电商目实战之十Q商品类基本模块的搭? /></p><p>  我们先根据其商品cȝ前台文gQ拷贝一份到product文g夹中Q然后我们再做相应的修改。先来分析一下流E:首先index.jsp到aindex.jsp昄左侧菜单栏,当点ȝ别管理时Q进入category/query.jsp面右侧昄所有商品类别信息,搜烦和删除功能均在此面Q不需要弹出新的窗口,d弹出save.jspH口Q更新弹出update.jspH口。当点击商品理的时候,q入product/query.jsp面右侧昄所有商品信息,搜烦和删除功能均在此面完成Q添加和更新分别弹出save.jsp和update.jsp。接下来我们把各个页面的框架搭徏好,然后往相应的部分填东西卛_?/p><p>  首先在aindex.jsp中添加如下代码:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160510215734655.png" width="600" height="281" alt="SSH电商目实战之十Q商品类基本模块的搭? /></p><p>  接下来,我们完成query.jsp的框Ӟ</p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        body {    </span></li> <li><span>            margin: 1px;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>        .searchbox {    </span></li> <li class="alt"><span>          margin: -3;    </span></li> <li><span>        }    </span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        $(function(){    </span></li> <li><span>            $('#dg').datagrid({       </span></li> <li class="alt"><span>                //url地址改ؓhproductAction中的queryJoinCategoryҎ    </span></li> <li><span>                url:'product_queryJoinCategory.action',    </span></li> <li class="alt"><span>                loadMsg:'Loading......',    </span></li> <li><span>                queryParams:{name:''},//q里参数ҎnameQ参数gؓI,表示我们要显C所有商品,后台是根据商品name属性查询的    </span></li> <li class="alt"><span>                //width:300,    </span></li> <li><span>                fitColumns:true,    </span></li> <li class="alt"><span>                striped:true,    </span></li> <li><span>                nowrap:true,    </span></li> <li class="alt"><span>                singleSelect:false,    </span></li> <li><span>                pagination:true,    </span></li> <li class="alt"><span>                pageSize:5,    </span></li> <li><span>                pageList:[5,10,15,20],    </span></li> <li class="alt"><span>                idField:'id',//指定id为标识字D,在删除,更新的时候有用,如果配置此字D,在翻|Q换不会媄响选中的项    </span></li> <li><span>                    </span></li> <li class="alt"><span>                //toolbar定义d、删除、更新按钮以及搜索框    </span></li> <li><span>                toolbar: [{    </span></li> <li class="alt"><span>                    iconCls: 'icon-add',    </span></li> <li><span>                    text:'d商品',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                        //d触发代码    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                 },'-',{    </span></li> <li class="alt"><span>                    iconCls: 'icon-edit',    </span></li> <li><span>                    text:'更新商品',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                                            //d触发代码    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                 },'-',{    </span></li> <li class="alt"><span>                    iconCls: 'icon-remove',    </span></li> <li><span>                     text:'删除商品',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                        //d触发代码                        </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                },'-',{ //查询按钮不是LinkButtonQ它有语法,但是也支持解析HTML标签    </span></li> <li class="alt"><span>                    text:"<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">'ss'</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">'serach'</span><span> </span><span class="tag">/></span><span>"    </span></span></li> <li><span>                }],    </span></li> <li class="alt"><span>                rowStyler: function(index,row){    </span></li> <li><span>                    console.info("index" + index + "," + row)    </span></li> <li class="alt"><span>                    if(index % <span class="attribute">2</span><span> == 0) {    </span></span></li> <li><span>                        return 'background-color:#fff;';    </span></li> <li class="alt"><span>                    } else {    </span></li> <li><span>                        return 'background-color:#c4e1e1;';    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                        </span></li> <li class="alt"><span>                 },    </span></li> <li><span>                frozenColumns:[[    </span></li> <li class="alt"><span>                     {field:'checkbox',checkbox:true},    </span></li> <li><span>                    {field:'id',title:'商品~号',width:100}       </span></li> <li class="alt"><span>                 ]],    </span></li> <li><span>                columns:[[                         </span></li> <li class="alt"><span>                    {field:'name',title:'商品名称',width:100},        </span></li> <li><span>                    {field:'price',title:'商品h',width:100},    </span></li> <li class="alt"><span>                    {field:'remark',title:'单描q?,width:100},    </span></li> <li><span>                    {field:'xremark',title:'详细描述',width:100},    </span></li> <li class="alt"><span>                    {field:'date',title:'上架旉',width:100},    </span></li> <li><span>                    {field:'commend',title:'推荐商品',width:100,      </span></li> <li class="alt"><span>                        formatter: function(value,row,index){    </span></li> <li><span>                            if(value) {    </span></li> <li class="alt"><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">checked</span><span>=</span><span class="attribute-value">'checked'</span><span> </span><span class="attribute">disabled</span><span>=</span><span class="attribute-value">'true'</span><span>";    </span></span></li> <li><span>                            } else {    </span></li> <li class="alt"><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">disabled</span><span>=</span><span class="attribute-value">'true'</span><span>";    </span></span></li> <li><span>                            }    </span></li> <li class="alt"><span>                         }    </span></li> <li><span>                    },    </span></li> <li class="alt"><span>                    {field:'open',title:'有效商品',width:100,      </span></li> <li><span>                        formatter: function(value,row,index){    </span></li> <li class="alt"><span>                            if(value) {    </span></li> <li><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">checked</span><span>=</span><span class="attribute-value">'checked'</span><span> </span><span class="attribute">disabled</span><span>=</span><span class="attribute-value">'true'</span><span>";    </span></span></li> <li class="alt"><span>                            } else {    </span></li> <li><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">disabled</span><span>=</span><span class="attribute-value">'true'</span><span>";    </span></span></li> <li class="alt"><span>                            }    </span></li> <li><span>                        }    </span></li> <li class="alt"><span>                     },    </span></li> <li><span>                    {field:'category.type',title:'所属商品类?,width:200, //category.type是商品类?nbsp;   </span></li> <li class="alt"><span>                        formatter: function(value,row,index){    </span></li> <li><span>                            if(row.category != null && row.category.type != null) {    </span></li> <li class="alt"><span>                                return row.category.type; //如果商品cd不ؓI,q回商品cd    </span></li> <li><span>                            } else {    </span></li> <li class="alt"><span>                                return "此商品暂时未分类";    </span></li> <li><span>                            }    </span></li> <li class="alt"><span>                         }      </span></li> <li><span>                    }    </span></li> <li class="alt"><span>                ]]        </span></li> <li><span>            });     </span></li> <li class="alt"><span>            //把普通的文本框{化ؓ查询搜烦文本?nbsp;   </span></li> <li><span>            $('#ss').searchbox({     </span></li> <li class="alt"><span>                //触发查询事g    </span></li> <li><span>                 searcher:function(value,name){ //value表示输入的?nbsp;   </span></li> <li class="alt"><span>                    //d触发代码    </span></li> <li><span>                },     </span></li> <li class="alt"><span>                prompt:'误入搜索关键字'     </span></li> <li><span>            });     </span></li> <li class="alt"><span>        });    </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li><span>      </span></li> <li class="alt"><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"dg"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        </span></li> <li><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  接下来我们完成productAction中的queryJoinCategoryҎQ在q之前,先要完成service部分Q我们都是先从底层慢慢往上开发的Q?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="comment">//ProductService接口  </span><span>  </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">interface</span><span> ProductService </span><span class="keyword">extends</span><span> BaseService<Product> {    </span></span></li> <li class="alt"><span>        </span></li> <li><span>    <span class="comment">//查询商品信息Q联类?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> List<Product> queryJoinCategory(String type, </span><span class="keyword">int</span><span> page, </span><span class="keyword">int</span><span> size); </span><span class="comment">//使用商品的名U查?nbsp; </span><span>  </span></span></li> <li><span>    <span class="comment">//Ҏ关键字查询总记录数  </span><span>  </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> Long getCount(String type);    </span></span></li> <li><span>}    </span></li> <li class="alt"><span>    </span></li> <li><span><span class="annotation">@SuppressWarnings</span><span>(</span><span class="string">"unchecked"</span><span>)    </span></span></li> <li class="alt"><span><span class="annotation">@Service</span><span>(</span><span class="string">"productService"</span><span>)    </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ProductServiceImpl </span><span class="keyword">extends</span><span> BaseServiceImpl<Product> </span><span class="keyword">implements</span><span> ProductService {    </span></span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> List<Product> queryJoinCategory(String name, </span><span class="keyword">int</span><span> page, </span><span class="keyword">int</span><span> size) {    </span></span></li> <li><span>        String hql = <span class="string">"from Product p left join fetch p.category where p.name like :name"</span><span>;    </span></span></li> <li class="alt"><span>        <span class="keyword">return</span><span> getSession().createQuery(hql)    </span></span></li> <li><span>                .setString(<span class="string">"name"</span><span>, </span><span class="string">"%"</span><span> + name + </span><span class="string">"%"</span><span>)    </span></span></li> <li class="alt"><span>                .setFirstResult((page-<span class="number">1</span><span>) * size) </span><span class="comment">//从第几个开始显C?nbsp; </span><span>  </span></span></li> <li><span>                .setMaxResults(size) <span class="comment">//昄几个  </span><span>  </span></span></li> <li class="alt"><span>                .list();    </span></li> <li><span>    }    </span></li> <li class="alt"><span>        </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> Long getCount(String name) {    </span></span></li> <li><span>        String hql = <span class="string">"select count(p) from Product p where p.name like :name"</span><span>;    </span></span></li> <li class="alt"><span>        <span class="keyword">return</span><span> (Long) getSession().createQuery(hql)    </span></span></li> <li><span>            .setString(<span class="string">"name"</span><span>, </span><span class="string">"%"</span><span> + name + </span><span class="string">"%"</span><span>)    </span></span></li> <li class="alt"><span>            .uniqueResult(); <span class="comment">//q回一条记?总记录数  </span><span>  </span></span></li> <li><span>    }    </span></li> <li class="alt"><span>    </span></li> <li><span>}    </span></li></ol></div><p>  下面可以完成productAction中的queryJoinCategoryҎ了:</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"productAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ProductAction </span><span class="keyword">extends</span><span> BaseAction<Product> {    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="keyword">public</span><span> String queryJoinCategory() {    </span></span></li> <li><span>        System.out.println(<span class="string">"name:"</span><span> + model.getName());    </span></span></li> <li class="alt"><span>        System.out.println(<span class="string">"pageQ?quot;</span><span> + page);    </span></span></li> <li><span>        System.out.println(<span class="string">"rowsQ?quot;</span><span> + rows);    </span></span></li> <li class="alt"><span>            </span></li> <li><span>        <span class="comment">//用来存储分页的数?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        pageMap = <span class="keyword">new</span><span> HashMap<String, Object>();    </span></span></li> <li><span>            </span></li> <li class="alt"><span>        <span class="comment">//Ҏ关键字和分页的参数查询相应的数据  </span><span>  </span></span></li> <li><span>        List<Product> productList = productService.queryJoinCategory(model.getName(), page, rows);    </span></li> <li class="alt"><span>        pageMap.put(<span class="string">"rows"</span><span>, productList); </span><span class="comment">//存储为JSON格式  </span><span>  </span></span></li> <li><span>        <span class="comment">//Ҏ关键字查询总记录数  </span><span>  </span></span></li> <li class="alt"><span>        Long total = productService.getCount(model.getName());    </span></li> <li><span><span class="comment">//      System.out.println(total);  </span><span>  </span></span></li> <li class="alt"><span>        pageMap.put(<span class="string">"total"</span><span>, total); </span><span class="comment">//存储为JSON格式  </span><span>  </span></span></li> <li><span>        <span class="keyword">return</span><span> </span><span class="string">"jsonMap"</span><span>;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  接下来在struts.xml中进行配|,跟之前的商品cML程Q到q里可以看出Q开发好了一个,下面一个就快了Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"product_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"productAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"jsonMap"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"json"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"root"</span><span class="tag">></span><span>pageMap</span><span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"excludeProperties"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="comments"><!-- rows[0].category.account --></span><span>    </span></span></li> <li><span>            <span class="comments"><!-- 把所有accountqo掉,否则会出现懒加蝲问题Q该部分下面截图 --></span><span>             </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li></ol></div><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160510232107192.png" width="600" height="45" alt="SSH电商目实战之十Q商品类基本模块的搭? /></p><p>  q样后台E序写好了,然后开启tomcatQ测试一下,当我们点d侧菜单栏的商品管理时Q会弹出双如下H口Q?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160510230756250.png" width="600" height="233" alt="SSH电商目实战之十Q商品类基本模块的搭? /></p><p>  q样我们完成了商品理H口的框架了?/p><p> </p>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/770.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=770</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=770&key=2398712e</trackback:ping></item><item><title>SSH电商目实战之九Q添加和更新商品cd功能的实?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/769.html</link><pubDate>Tue, 24 Oct 2017 14:14:05 +0800</pubDate><guid>http://www.vkzldl.live/software/769.html</guid><description><![CDATA[<p>  上一节我们做完了查询和删除商品的功能Q这一节我们做一下添加和更新商品的功能?/p><p>  <span style="font-size: medium;"><strong>1. d商品cd</strong></span></p><p>  <strong>1.1 dcd的UI设计</strong></p><p>  我们先说一下思\Q首先当用户点击“d商品”Ӟ我们应该弹出一?ldquo;d商品”的UIH口Q注意这里不是蟩转到新的jspQEasyUI只有一个页面)Q弹?ldquo;d商品”的窗口后Q应该锁住它父类的所有窗口(即点d他地Ҏ效,只能操作d商品的窗口)Q等用户填好了信息后Q在新弹出来的窗口上点击“d”后,请求发送给struts2Q然后struts2拿到h你参敎ͼ从数据库中执行添加动作,q样后台操作完成Q同时前台那边要h一下当前页面,重新昄所有商品?/p><p>  我们查看EasyUI的文档,发现新徏一个窗口有两种ҎQ要么用标{ֈ建,要么使用js创徏Q我们这里用js创徏Q但是需要一?lt;div>盒子Q如下:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160509095158085.png" alt="SSH电商目实战之九Q添加和更新商品cd功能的实? /></p><p>  另外Q我们创建的新的H口不需要最化Q最大化Q但是要锁屏。所以这些属性都在div中设|好Q这里要注意的就是锁屏的功能Q因?lt;div>攄地方不同Q锁住的屏幕范围也不同,我们要锁住全屏,所以要?lt;div>攑ֈaindex.jsp中,应ؓaindex.jsp中生了query.jsp的内容(包括d按钮Q,query.jsp中生了save.jsp的内容(是我们要显C的dH口UIQ,所以弹出窗口后Q我们要把aindex.jsp的范围都锁住Q所?lt;div>应该攑ֈaindex.jsp中,具体实现如下Q?/p><p>  在aindex.jsp?lt;body>中新d一?lt;div></p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"win"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"collapsible:false,minimizable:false,maximizable:false,modal:true"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>     </span></span></li></ol></div><p>  然后我们完善query.jsp中添加类别的部分Q?/p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span>{    </span></span></li> <li><span>    iconCls: <span class="string">'icon-add'</span><span>,    </span></span></li> <li class="alt"><span>    text:<span class="string">'dcd'</span><span>,    </span></span></li> <li><span>    handler: <span class="keyword">function</span><span>(){    </span></span></li> <li class="alt"><span>        parent.$(<span class="string">"#win"</span><span>).window({ </span><span class="comment">//因ؓ<div>攑֜了aindex.jsp中,所以这里创建窗口要先调用parent  </span><span>  </span></span></li> <li><span>            title:<span class="string">"dcd"</span><span>,    </span></span></li> <li class="alt"><span>            width:350,    </span></li> <li><span>            height:150,    </span></li> <li class="alt"><span>            content:<span class="string">'<iframe src="send_category_save.action" frameborder="0" width="100%" height="100%"/>'</span><span>    </span></span></li> <li><span>        });    </span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  从上面的dcd代码中可以看出,d后的UIQ我们引入WEB-INF/category目录下的save.jsp文g中的内容Q接下来我们完成save.jspQ?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>      </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>      </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>      </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>      </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>        form div {      </span></li> <li><span>            margin:5px;      </span></li> <li class="alt"><span>        }      </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>      </span></span></li> <li><span>        $(function(){      </span></li> <li class="alt"><span>            $("input[<span class="attribute">name</span><span>=</span><span class="attribute-value">type</span><span>]").validatebox({ //q里?ldquo;cd名称”的验证功能,如果用户没填好就提交的话Q会有提C?nbsp;     </span></span></li> <li><span>                required:true,      </span></li> <li class="alt"><span>                missingMessage:'误入类别名U? //提示的内?nbsp;     </span></li> <li><span>            });           </span></li> <li class="alt"><span>      </span></li> <li><span>                    //对管理员的下拉列表框q行q程加蝲      </span></li> <li class="alt"><span>                    $("#cc").combobox({         </span></li> <li><span>                        //请求发送给accountAction中的queryҎ处理Q这里需要将处理好的数据q回到这Ҏ昄?nbsp;Q所以后台需要将数据打包成json格式发过?nbsp;     </span></li> <li class="alt"><span>                        url:'account_query.action',        </span></li> <li><span>                        valueField:'id',          </span></li> <li class="alt"><span>                        textField:'login', //我们下拉列表中显C的是管理员的登录名      </span></li> <li><span>                        panelHeight:'auto', //自适应高度      </span></li> <li class="alt"><span>                        panelWidth:120,//下拉列表是两个组件组成的      </span></li> <li><span>                        width:120, //要同时设|两个宽度才?nbsp;     </span></li> <li class="alt"><span>                        editable:false //下拉框不允许~辑      </span></li> <li><span>                     });      </span></li> <li class="alt"><span>      </span></li> <li><span>            //H体弹出默认是禁用验证,因ؓ刚弹出的H口Q用戯没填显C的话,太丑      </span></li> <li class="alt"><span>            $("#ff").form("disableValidation");      </span></li> <li><span>            //注册button的事件。即当用L?ldquo;d”的时候做的事      </span></li> <li class="alt"><span>            $("#btn").click(function(){      </span></li> <li><span>                //开启验?nbsp;     </span></li> <li class="alt"><span>                $("#ff").form("enableValidation");      </span></li> <li><span>                //如果验证成功Q则提交数据      </span></li> <li class="alt"><span>                if($("#ff").form("validate")) {      </span></li> <li><span>                    //调用submitҎ提交数据      </span></li> <li class="alt"><span>                    $("#ff").form('submit', {      </span></li> <li><span>                        url: 'category_save.action', //请求提交给categoryAction中的saveҎ处理      </span></li> <li class="alt"><span>                        success: function(){ //成功?nbsp;     </span></li> <li><span>                            //如果成功了,关闭当前H口      </span></li> <li class="alt"><span>                            parent.$("#win").window("close");      </span></li> <li><span>                            //h面Q刚刚添加的显C出来了?nbsp;     </span></li> <li class="alt"><span>                                                        //获取aindex--<span class="tag">></span><span>iframe--</span><span class="tag">></span><span>datagrid      </span></span></li> <li><span>                            parent.$("iframe[<span class="attribute">title</span><span>=</span><span class="attribute-value">'cd理'</span><span>]").get(0).contentWindow.$("#dg").datagrid("reload");      </span></span></li> <li class="alt"><span>                        }      </span></li> <li><span>                    });      </span></li> <li class="alt"><span>                }      </span></li> <li><span>            });      </span></li> <li class="alt"><span>        });      </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>      </span></span></li> <li><span>        </span></li> <li class="alt"><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>      </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"ff"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span class="tag">></span><span>         </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>         </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">label</span><span> </span><span class="attribute">for</span><span>=</span><span class="attribute-value">"name"</span><span class="tag">></span><span>商品名称:</span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span> </span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"type"</span><span> </span><span class="tag">/></span><span>         </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>         </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>                <span class="tag"><</span><span class="tag-name">label</span><span class="tag">></span><span>所属管理员Q?/span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span>       </span></span></li> <li><span>                <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"cc"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"account.id"</span><span class="tag">/></span><span>      </span></span></li> <li class="alt"><span>            <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>      </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>         </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">label</span><span> </span><span class="attribute">for</span><span>=</span><span class="attribute-value">"hot"</span><span class="tag">></span><span>热点:</span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span>         </span></span></li> <li><span>                ?span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"hot"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="tag">/></span><span>       </span></span></li> <li class="alt"><span>                ?nbsp;<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"hot"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="tag">/></span><span>      </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>        </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>      </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"btn"</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"easyui-linkbutton"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"iconCls:'icon-add'"</span><span class="tag">></span><span>d</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>        </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>        </span></span></li> <li><span>    <span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span>         </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>      </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span></span></li></ol></div><p>  前台的显CZ及发送请求都做完了,接下来就是做后台的程序了?/p><p>  <strong>1.2 dcd的逻辑实现</strong></p><p>  前台会把数据发送给categoryAction中的saveҎL行,所以我们去写Actionp了,因ؓ后台只需要将cddq数据库Q不需要向前台q回数据Q所以比较简单,直接写好actionp了:</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"categoryAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryAction </span><span class="keyword">extends</span><span> BaseAction<Category> {    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="comment">//省略其他代码……  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> save() {    </span></span></li> <li class="alt"><span>        System.out.println(model);    </span></li> <li><span>        categoryService.save(model);    </span></li> <li class="alt"><span>    }    </span></li> <li><span>    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  q样数据存入数据库了,完了后,前台那边h昄Q就能将新添加的商品cd昄出来了,我们看一?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160511212557944.png" width="600" height="278" alt="SSH电商目实战之九Q添加和更新商品cd功能的实? /></p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160511212734837.png" style="text-align: start;" width="600" height="259" alt="SSH电商目实战之九Q添加和更新商品cd功能的实? /></p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160509101443157.png" alt="SSH电商目实战之九Q添加和更新商品cd功能的实? style="text-align: start;" /></p><p>  d商品cdq一块我们就做完了。下面做更新商品cd那块?/p><p>  <span style="font-size: medium;"><strong>2. 更新商品cd</strong></span></p><p>  <strong>2.1 更新cd的UI设计</strong></p><p>  更新商品cd的思\和上面的d的基本相同,首先也是弹出一个UIH口Q然后用户填好数据发送给后台Q后台更新数据库Q前台再h昄。我们仍焉用上面的Ҏ创徏一个UIH口?lt;div>盒子不用改动什么,我们需要做的就是在query.jsp中完善更“更新cd”部分的代码:</p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span>{    </span></span></li> <li><span>    iconCls: <span class="string">'icon-edit'</span><span>,    </span></span></li> <li class="alt"><span>    text:<span class="string">'更新cd'</span><span>,    </span></span></li> <li><span>    handler: <span class="keyword">function</span><span>(){    </span></span></li> <li class="alt"><span>        <span class="comment">//判断是否有选中行记录,使用getSelections获取选中的所有行  </span><span>  </span></span></li> <li><span>        <span class="keyword">var</span><span> rows = $(</span><span class="string">"#dg"</span><span>).datagrid(</span><span class="string">"getSelections"</span><span>);    </span></span></li> <li class="alt"><span>        <span class="keyword">if</span><span>(rows.length == 0) {    </span></span></li> <li><span>            <span class="comment">//弹出提示信息  </span><span>  </span></span></li> <li class="alt"><span>            $.messager.show({ <span class="comment">//语法cM于java中的静态方法,直接对象调用  </span><span>  </span></span></li> <li><span>                title:<span class="string">'错误提示'</span><span>,    </span></span></li> <li class="alt"><span>                msg:<span class="string">'臛_要选择一条记?</span><span>,    </span></span></li> <li><span>                timeout:2000,    </span></li> <li class="alt"><span>                showType:<span class="string">'slide'</span><span>,    </span></span></li> <li><span>            });    </span></li> <li class="alt"><span>        }<span class="keyword">else</span><span> </span><span class="keyword">if</span><span>(rows.length != 1) {    </span></span></li> <li><span>            <span class="comment">//弹出提示信息  </span><span>  </span></span></li> <li class="alt"><span>            $.messager.show({ <span class="comment">//语法cM于java中的静态方法,直接对象调用  </span><span>  </span></span></li> <li><span>                title:<span class="string">'错误提示'</span><span>,    </span></span></li> <li class="alt"><span>                msg:<span class="string">'每次只能更新一条记?</span><span>,    </span></span></li> <li><span>                timeout:2000,    </span></li> <li class="alt"><span>                showType:<span class="string">'slide'</span><span>,    </span></span></li> <li><span>            });    </span></li> <li class="alt"><span>        } <span class="keyword">else</span><span>{    </span></span></li> <li><span>            <span class="comment">//1. 弹出更新的页?nbsp; </span><span>  </span></span></li> <li class="alt"><span>            parent.$(<span class="string">"#win"</span><span>).window({    </span></span></li> <li><span>                title:<span class="string">"dcd"</span><span>,    </span></span></li> <li class="alt"><span>                width:350,    </span></li> <li><span>                height:250,    </span></li> <li class="alt"><span>                content:<span class="string">'<iframe src="send_category_update.action" frameborder="0" width="100%" height="100%"/>'</span><span>    </span></span></li> <li><span>            });    </span></li> <li class="alt"><span>            <span class="comment">//2.   </span><span>  </span></span></li> <li><span>        }    </span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  我们分析一下上面的js代码Q首先获取用户勾选要更新的行Q如果没有选中则提C用戯需要选中一纪录去更新Q如果选中的不止一条纪录,也得提示用户每次只能更新一条纪录。当q些判断都结束后Q保证了用户N了一条纪录,那么我们开始创建新的UIH口了,q里q是跟上面一P引入WEB-INF/category目录下的update.jsp面的内容,我们来看下update.jsp面内容Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        form div {    </span></li> <li><span>            margin:5px;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li><span>        $(function(){    </span></li> <li class="alt"><span>            //iframe中的datagrid对象    </span></li> <li><span>            var <span class="attribute">dg</span><span> = </span><span class="attribute-value">parent</span><span>.$("iframe[</span><span class="attribute">title</span><span>=</span><span class="attribute-value">'cd理'</span><span>]").get(0).contentWindow.$("#dg");    </span></span></li> <li class="alt"><span>                </span></li> <li><span>            //对管理员的下拉列表框q行q程加蝲    </span></li> <li class="alt"><span>            $("#cc").combobox({       </span></li> <li><span>                //请求发送给accountAction中的queryҎ处理Q这里需要将处理好的数据q回到这Ҏ昄?nbsp;Q所以后台需要将数据打包成json格式发过?nbsp;   </span></li> <li class="alt"><span>                url:'account_query.action',      </span></li> <li><span>                valueField:'id',        </span></li> <li class="alt"><span>                textField:'login', //我们下拉列表中显C的是管理员的登录名    </span></li> <li><span>                panelHeight:'auto', //自适应高度    </span></li> <li class="alt"><span>                panelWidth:120,//下拉列表是两个组件组成的    </span></li> <li><span>                width:120, //要同时设|两个宽度才?nbsp;   </span></li> <li class="alt"><span>                editable:false //下拉框不允许~辑    </span></li> <li><span>             });      </span></li> <li class="alt"><span>                </span></li> <li><span>            // 完成数据的回显,更新Ӟ用户肯定先选择了要更新的那一行,首先我们得拿到那一?nbsp;   </span></li> <li class="alt"><span>            var <span class="attribute">rows</span><span> = </span><span class="attribute-value">dg</span><span>.datagrid("getSelections");    </span></span></li> <li><span>            //拿到的那一行对应的数据字段加蝲到表单里Q实现回?nbsp;   </span></li> <li class="alt"><span>            $("#ff").form('load',{    </span></li> <li><span>                id:rows[0].id,    </span></li> <li class="alt"><span>                type:rows[0].type,    </span></li> <li><span>                    hot:rows[0].hot,    </span></li> <li class="alt"><span>                'account.id':rows[0].account.id //EasyUI不支持account.idq种Ҏ作,所以要加个引号    </span></li> <li><span>            });    </span></li> <li class="alt"><span>    </span></li> <li><span>            //回显完了数据后,讄一下验证功?nbsp;   </span></li> <li class="alt"><span>            $("input[<span class="attribute">name</span><span>=</span><span class="attribute-value">type</span><span>]").validatebox({    </span></span></li> <li><span>                required:true,    </span></li> <li class="alt"><span>                missingMessage:'误入类别名U?    </span></li> <li><span>            });         </span></li> <li class="alt"><span>            //H体弹出默认时禁用验?nbsp;   </span></li> <li><span>            $("#ff").form("disableValidation");    </span></li> <li class="alt"><span>            //注册button的事?nbsp;   </span></li> <li><span>            $("#btn").click(function(){    </span></li> <li class="alt"><span>                //开启验?nbsp;   </span></li> <li><span>                $("#ff").form("enableValidation");    </span></li> <li class="alt"><span>                //如果验证成功Q则提交数据    </span></li> <li><span>                if($("#ff").form("validate")) {    </span></li> <li class="alt"><span>                    //调用submitҎ提交数据    </span></li> <li><span>                    $("#ff").form('submit', {    </span></li> <li class="alt"><span>                        url: 'category_update.action', //提交时将h传给categoryAction的updateҎ执行    </span></li> <li><span>                        success: function(){    </span></li> <li class="alt"><span>                            //如果成功了,关闭当前H口Qƈh面    </span></li> <li><span>                            parent.$("#win").window("close");    </span></li> <li class="alt"><span>                            dg.datagrid("reload");    </span></li> <li><span>                        }    </span></li> <li class="alt"><span>                    });    </span></li> <li><span>                }    </span></li> <li class="alt"><span>            });    </span></li> <li><span>        });    </span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>      </span></li> <li><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"ff"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span class="tag">></span><span>       </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">label</span><span> </span><span class="attribute">for</span><span>=</span><span class="attribute-value">"name"</span><span class="tag">></span><span>cd名称:</span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span> </span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"type"</span><span> </span><span class="tag">/></span><span>       </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">label</span><span> </span><span class="attribute">for</span><span>=</span><span class="attribute-value">"hot"</span><span class="tag">></span><span>热点:</span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>                ?span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"hot"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="tag">/></span><span>     </span></span></li> <li><span>                ?span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"radio"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"hot"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"false"</span><span> </span><span class="tag">/></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>      </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">label</span><span> </span><span class="attribute">for</span><span>=</span><span class="attribute-value">"account"</span><span class="tag">></span><span>所属管理员:</span><span class="tag"></</span><span class="tag-name">label</span><span class="tag">></span><span>    </span></span></li> <li><span>             <span class="comments"><!-- 下拉列表我们采用q程加蝲的方法加载管理员数据 --></span><span>    </span></span></li> <li class="alt"><span>             <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"cc"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"account.id"</span><span> </span><span class="tag">/></span><span>    </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"btn"</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"easyui-linkbutton"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"iconCls:'icon-edit'"</span><span class="tag">></span><span>更新</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>      </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"hidden"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"id"</span><span> </span><span class="tag">/></span><span>    </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>  `    </span></span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span>       </span></span></li> <li><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  更新与添加不同的地方在于Q首先得数据回显Q然后还有个下拉列表昄理员数据,因ؓ所属管理员也要更新。我们看看上面的代码Q首先用远E加载的Ҏ加蝲理员数据,先向后台发送一个请求,后台accountAction的queryҎ处理完后Q管理员数据打包成json格式q回回来Q这样就能拿到管理员数据了,拿到后,可以进行数据的回显了。我们看一下后台的E序Q?/p><p>  <strong>2.2 更新cd的逻辑实现</strong></p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"baseAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> BaseAction<T> </span><span class="keyword">extends</span><span> ActionSupport </span><span class="keyword">implements</span><span> RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {    </span></span></li> <li><span>    </span></li> <li class="alt"><span>    <span class="comment">//用来装有要被打包成json格式q回l前台的数据Q下面要实现getҎ  </span><span>  </span></span></li> <li><span>    <span class="keyword">protected</span><span> List<T> jsonList = </span><span class="keyword">null</span><span>;    </span></span></li> <li class="alt"><span>    <span class="comment">//省略其他Ҏ……  </span><span>  </span></span></li> <li><span>        </span></li> <li class="alt"><span>}    </span></li> <li><span>    </span></li> <li class="alt"><span><span class="comment">//AccountAction  </span><span>  </span></span></li> <li><span><span class="annotation">@Controller</span><span>(</span><span class="string">"accountAction"</span><span>)    </span></span></li> <li class="alt"><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> AccountAction </span><span class="keyword">extends</span><span> BaseAction<Account> {    </span></span></li> <li class="alt"><span>        </span></li> <li><span>    <span class="keyword">public</span><span> String query() {    </span></span></li> <li class="alt"><span>        jsonList = accountService.query();    </span></li> <li><span>        <span class="keyword">return</span><span> </span><span class="string">"jsonList"</span><span>;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  接下来我们配|一下struts.xml文gQ?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"account_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"accountAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"jsonList"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"json"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"root"</span><span class="tag">></span><span>jsonList</span><span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"excludeProperties"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="comments"><!-- [0].pass, [1].pass --></span><span>    </span></span></li> <li><span>            <span class="comments"><!-- 正则表达式显C有bugQ我下面截个?nbsp;--></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li></ol></div><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160509105831494.png" alt="SSH电商目实战之九Q添加和更新商品cd功能的实? /></p><p>       完成回显后,是更新操作了,当然也有验证的功能,和添加一LQ更新操作将h传给categoryAction的updateҎ执行Q比较简单:</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"categoryAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryAction </span><span class="keyword">extends</span><span> BaseAction<Category> {    </span></span></li> <li><span>    <span class="comment">//省略其他Ҏ……  </span><span>  </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> update() {    </span></span></li> <li><span>        System.out.println(model);    </span></li> <li class="alt"><span>        categoryService.update(model);    </span></li> <li><span>    }    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  到此Q我们完成了商品cd的添加和更新操作?/p><p> </p>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/769.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=769</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=769&key=ba59295b</trackback:ping></item><item><title>SSH电商目实战之八Q查询和删除商品cd功能的实?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/768.html</link><pubDate>Wed, 11 Oct 2017 09:18:20 +0800</pubDate><guid>http://www.vkzldl.live/software/768.html</guid><description><![CDATA[<p>  上一节我们完成了使用DataGrid昄所有商品信息,q节我们开始添加几个功能:d、更新、删除和查询。首先我们实C前台的显C,然后再做后台获取数据?/p><p>  <span style="font-size: medium;"><strong>1. d、更新、删除和查询功能的前台实?/strong></span></p><p>  DataGrid控g里有个toolbar属性,是添加工h的,我们可以在toolbar属性中dq些按钮来实现相应的功能。先看一下官Ҏ档对toolbar的定义:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160508101650902.png" width="600" height="334" alt="SSH电商目实战之八Q查询和删除商品cd功能的实? /></p><p>  我们使用数组的方式定义工hQ在query.jsp面中新d如下代码Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        body {    </span></li> <li><span>            margin: 1px;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>                .searchbox {    </span></li> <li class="alt"><span>                        margin: -3;    </span></li> <li><span>                }    </span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        $(function(){    </span></li> <li><span>            $('#dg').datagrid({       </span></li> <li class="alt"><span>                //url地址改ؓhcategoryAction    </span></li> <li><span>                url:'category_queryJoinAccount.action',    </span></li> <li class="alt"><span>    </span></li> <li><span>                singleSelect:false, //如果为真Q只允许单行昄Q全选功能失?nbsp;   </span></li> <li class="alt"><span>                //讄分页    </span></li> <li><span>                pagination:true,    </span></li> <li class="alt"><span>                //讄每页昄的记录数Q默认是10?nbsp;   </span></li> <li><span>                pageSize:5,    </span></li> <li class="alt"><span>                //讄可选的每页记录敎ͼ供用户选择Q默认是10,20,30,40...    </span></li> <li><span>                pageList:[5,10,15,20],    </span></li> <li class="alt"><span>                idField:'id',//指定id为标识字D,在删除,更新的时候有用,如果配置此字D,在翻|Q换不会媄响选中的项    </span></li> <li><span>    </span></li> <li class="alt"><span>                     /*********************d的代?**********************/    </span></li> <li><span>                toolbar: [{    </span></li> <li class="alt"><span>                    iconCls: 'icon-add',    </span></li> <li><span>                    text:'dcd',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                        alert('--加添cd--');    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                },'-',{    </span></li> <li class="alt"><span>                    iconCls: 'icon-edit',    </span></li> <li><span>                    text:'更新cd',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                        alert('--更新cd--');    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                },'-',{    </span></li> <li class="alt"><span>                    iconCls: 'icon-remove',    </span></li> <li><span>                    text:'删除cd',    </span></li> <li class="alt"><span>                    handler: function(){    </span></li> <li><span>                        //判断是否有选中行记录,使用getSelections获取选中的所有行    </span></li> <li class="alt"><span>                        var <span class="attribute">rows</span><span> = $("#dg").datagrid("getSelections");    </span></span></li> <li><span>                        //q回被选中的行Q如果没有Q何行被选中Q则q回I数l?nbsp;   </span></li> <li class="alt"><span>                        if(<span class="attribute">rows.length</span><span> == 0) {    </span></span></li> <li><span>                            //弹出提示信息    </span></li> <li class="alt"><span>                            $.messager.show({ //语法cM于java中的静态方法,直接对象调用    </span></li> <li><span>                                title:'错误提示',    </span></li> <li class="alt"><span>                                msg:'臛_要选择一条记?,    </span></li> <li><span>                                timeout:2000,    </span></li> <li class="alt"><span>                                showType:'slide',    </span></li> <li><span>                            });    </span></li> <li class="alt"><span>                        } else {    </span></li> <li><span>                            //提示是否认删除Q如果确认则执行删除的逻辑    </span></li> <li class="alt"><span>                            $.messager.confirm('删除的确认对话框', '您确定要删除此项吗?', function(r){    </span></li> <li><span>                                if (r){    </span></li> <li class="alt"><span>                                // 退出操?    </span></li> <li><span>                                    alert("--删除操作--")    </span></li> <li class="alt"><span>                                }    </span></li> <li><span>                            });    </span></li> <li class="alt"><span>                        }                           </span></li> <li><span>                    }    </span></li> <li class="alt"><span>                 },'-',{ //查询按钮不是LinkButtonQ它有语法,但是也支持解析HTML标签    </span></li> <li><span>                     text:"<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">'ss'</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">'serach'</span><span> </span><span class="tag">/></span><span>"    </span></span></li> <li class="alt"><span>                 }],    </span></li> <li><span>                   </span></li> <li class="alt"><span>                //把普通的文本框{化ؓ查询搜烦文本?nbsp;   </span></li> <li><span>                $('#ss').searchbox({     </span></li> <li class="alt"><span>                    //触发查询事g    </span></li> <li><span>                    searcher:function(value,name){ //value表示输入的?nbsp;   </span></li> <li class="alt"><span>                                           //查询操作    </span></li> <li><span>                    },     </span></li> <li class="alt"><span>                    prompt:'误入搜索关键字' //默认的显C?nbsp;   </span></li> <li><span>                });     </span></li> <li class="alt"><span>               /*********************************************************************/    </span></li> <li><span>    </span></li> <li class="alt"><span>        });    </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li><span>      </span></li> <li class="alt"><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"dg"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  q样我们搭好了d、更新、删除和查询的前台框架了Q现在可以在前台昄了,后台没有数据q来Q只是弹Z提示框,不过昄功能已经完成Q看一下效果:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160508103319475.png" width="600" height="296" alt="SSH电商目实战之八Q查询和删除商品cd功能的实? /></p><p>  接下来我们逐个来完成相应的功能?/p><p>  <span style="font-size: medium;"><strong>2. DataGridcd查询的实?/strong></span></p><p>  查询的实现是最单的Q在搜素框中输入关键字,然后关键字作ؓ参数传给actionQ然后Service从数据库中拿出数据,打包成json格式传到前台来显C即可,q个q程跟前面显C所有商品信息是一LQ我们只需要在上面jsp中添加搜索部分的代码卛_Q其他不用改变,d的代码如下:</p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span class="comment">//把普通的文本框{化ؓ查询搜烦文本?nbsp; </span><span>  </span></span></li> <li><span>$(<span class="string">'#ss'</span><span>).searchbox({     </span></span></li> <li class="alt"><span>    <span class="comment">//触发查询事g  </span><span>  </span></span></li> <li><span>    searcher:<span class="keyword">function</span><span>(value,name){ </span><span class="comment">//value表示输入的?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        <span class="comment">//alert(value + "," + name)  </span><span>  </span></span></li> <li><span>        <span class="comment">//获取当前查询的关键字Q通过DataGrid加蝲相应的信息,使用load加蝲和显C第一늚所有行?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        <span class="comment">//如果指定了参敎ͼ它将取代'queryParams'属性。通常可以通过传递一些参数执行一ơ查询,通过调用q个Ҏ会向上面url指定的actiond送请求,从服务器加蝲新数据?nbsp; </span><span>  </span></span></li> <li><span>        $(<span class="string">'#dg'</span><span>).datagrid(</span><span class="string">'load'</span><span>,{    </span></span></li> <li class="alt"><span>            type: value    </span></li> <li><span>        });    </span></li> <li class="alt"><span>    </span></li> <li><span>    },     </span></li> <li class="alt"><span>    prompt:<span class="string">'误入搜索关键字'</span><span>     </span></span></li> <li><span>});     </span></li></ol></div><p>  loadҎ可以加蝲昄W一늚所有行Q它有个参数Q如果指定了Q就会去带上么的queryParamsQ否则默认传递上面的queryParams指定的参敎ͼ我们在这里将type讄成value的|即用戯入的查询关键字,然后传到actionQ后台根据用戯入的value在数据库中查找,q返回给前台。执行结果如下:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160508104755868.png" width="600" height="239" alt="SSH电商目实战之八Q查询和删除商品cd功能的实? /></p><p>  q样我便完成了搜索的功能了,比较单?/p><p>  <span style="font-size: medium;"><strong>3. DataGridcd删除的实?/strong></span></p><p>  现在我们来实现删除功能,从上面的jsp中可以看出,删除前判断用h没有选中某条记录Q如果没有则l用户一个提C,如果有选中Q则弹出H口让用L认,如果为真Q则执行删除功能。有个细节要注意下,如果惌一ơ性删除多条记录,那么上面的singleSelect属性要讄成false?/p><p>  首先Q我们把上面query.jsp中删除部分的代码补充完,见下面:</p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span>{    </span></span></li> <li><span>    iconCls: <span class="string">'icon-remove'</span><span>,    </span></span></li> <li class="alt"><span>    text:<span class="string">'删除cd'</span><span>,    </span></span></li> <li><span>    handler: <span class="keyword">function</span><span>(){    </span></span></li> <li class="alt"><span>        <span class="comment">//判断是否有选中行记录,使用getSelections获取选中的所有行  </span><span>  </span></span></li> <li><span>        <span class="keyword">var</span><span> rows = $(</span><span class="string">"#dg"</span><span>).datagrid(</span><span class="string">"getSelections"</span><span>);    </span></span></li> <li class="alt"><span>        <span class="comment">//q回被选中的行Q如果没有Q何行被选中Q则q回I数l?nbsp; </span><span>  </span></span></li> <li><span>        <span class="keyword">if</span><span>(rows.length == 0) {    </span></span></li> <li class="alt"><span>            <span class="comment">//弹出提示信息  </span><span>  </span></span></li> <li><span>            $.messager.show({ <span class="comment">//语法cM于java中的静态方法,直接对象调用  </span><span>  </span></span></li> <li class="alt"><span>                title:<span class="string">'错误提示'</span><span>,    </span></span></li> <li><span>                msg:<span class="string">'臛_要选择一条记?</span><span>,    </span></span></li> <li class="alt"><span>                timeout:2000,    </span></li> <li><span>                showType:<span class="string">'slide'</span><span>,    </span></span></li> <li class="alt"><span>            });    </span></li> <li><span>        } <span class="keyword">else</span><span> {    </span></span></li> <li class="alt"><span>            <span class="comment">//提示是否认删除Q如果确认则执行删除的逻辑  </span><span>  </span></span></li> <li><span>            $.messager.confirm(<span class="string">'删除的确认对话框'</span><span>, </span><span class="string">'您确定要删除此项吗?'</span><span>, </span><span class="keyword">function</span><span>(r){    </span></span></li> <li class="alt"><span>                <span class="keyword">if</span><span> (r){    </span></span></li> <li><span>                    <span class="comment">//1. 从获取的记录中获取相应的的id,拼接id的|然后发送后?,2,3,4  </span><span>  </span></span></li> <li class="alt"><span>                    <span class="keyword">var</span><span> ids = </span><span class="string">""</span><span>;    </span></span></li> <li><span>                    <span class="keyword">for</span><span>(</span><span class="keyword">var</span><span> i = 0; i < rows.length; i ++) {    </span></span></li> <li class="alt"><span>                        ids += rows[i].id + <span class="string">","</span><span>;    </span></span></li> <li><span>                    }    </span></li> <li class="alt"><span>                    ids = ids.substr(0, ids.lastIndexOf(<span class="string">","</span><span>));    </span></span></li> <li><span>                    <span class="comment">//2. 发送ajaxh  </span><span>  </span></span></li> <li class="alt"><span>                    $.post(<span class="string">"category_deleteByIds.action"</span><span>,{ids:ids},</span><span class="keyword">function</span><span>(result){    </span></span></li> <li><span>                        <span class="keyword">if</span><span>(result == </span><span class="string">"true"</span><span>) {    </span></span></li> <li class="alt"><span>                                                <span class="comment">//刚刚选中的记录删除,要不然会影响后面更新的操?nbsp; </span><span>  </span></span></li> <li><span>                                                $(<span class="string">"#dg"</span><span>).datagrid(</span><span class="string">"uncheckAll"</span><span>);    </span></span></li> <li class="alt"><span>                            <span class="comment">//h当前,查询的时候我们用的是loadQ刷新第一,reload是刷新当前页  </span><span>  </span></span></li> <li><span>                            $(<span class="string">"#dg"</span><span>).datagrid(</span><span class="string">"reload"</span><span>);</span><span class="comment">//不带参数默认Z面的queryParams  </span><span>  </span></span></li> <li class="alt"><span>                        } <span class="keyword">else</span><span> {    </span></span></li> <li><span>                            $.messager.show({     </span></li> <li class="alt"><span>                                title:<span class="string">'删除异常'</span><span>,    </span></span></li> <li><span>                                msg:<span class="string">'删除p|Q请查操?</span><span>,    </span></span></li> <li class="alt"><span>                                timeout:2000,    </span></li> <li><span>                                showType:<span class="string">'slide'</span><span>,    </span></span></li> <li class="alt"><span>                            });    </span></li> <li><span>                        }    </span></li> <li class="alt"><span>                    },<span class="string">"text"</span><span>);    </span></span></li> <li><span>                }    </span></li> <li class="alt"><span>            });    </span></li> <li><span>        }                           </span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  如果用户选择删除Q首先会弹出一个对话框Q当用户定要删除后Q我们首先要获取用户所N的商品的idQ将q些id拼接成一个字W串Q然后向后台发送ajaxhQ?.post中的W一个参数是发送到那个actionQ第二个参数是发送的参数Q第三个参数是回调函敎ͼ卛_除成功后执行该函数里面的ҎQ该函数的参数result是从后台传过来的Q第四个参数可有可无Q是q回数据的类型。我们重点看?.post中的内容Q当后台q回一?quot;true"表示删除成功了,那么我们调用DataGrid里面的reloadҎ重新h面Qreload和前面查询时用的load是一LQ不同的地方在于reloadh后停留在当前面Q而load则显C第一c?/p><p>  好了Q前台页面部分写好了Q接下来完成后台的相应方法,首先在categoryService中添加deleteByIdsҎQƈ在其实现ccategoryServceImpl中实现该ҎQ?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="comment">//categoryService接口  </span><span>  </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">interface</span><span> CategoryService </span><span class="keyword">extends</span><span> BaseService<Category> {    </span></span></li> <li class="alt"><span>    <span class="comment">//查询cd信息Q联管理员  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> List<Category> queryJoinAccount(String type, </span><span class="keyword">int</span><span> page, </span><span class="keyword">int</span><span> size); </span><span class="comment">//使用cd的名U查?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    <span class="comment">//Ҏ关键字查询总记录数  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> Long getCount(String type);    </span></span></li> <li class="alt"><span>    <span class="comment">//Ҏids删除多条记录  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> deleteByIds(String ids);    </span></span></li> <li class="alt"><span>}    </span></li> <li><span>    </span></li> <li class="alt"><span><span class="comment">//categoryServiceImpl实现c?nbsp; </span><span>  </span></span></li> <li><span><span class="annotation">@SuppressWarnings</span><span>(</span><span class="string">"unchecked"</span><span>)    </span></span></li> <li class="alt"><span><span class="annotation">@Service</span><span>(</span><span class="string">"categoryService"</span><span>)    </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryServiceImpl </span><span class="keyword">extends</span><span> BaseServiceImpl<Category> </span><span class="keyword">implements</span><span> CategoryService {    </span></span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="comment">//其他Ҏ省略不写?hellip;…可以参照前面的相应章节内?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> deleteByIds(String ids) {    </span></span></li> <li><span>        String hql = <span class="string">"delete from Category c where c.id in ("</span><span> + ids + </span><span class="string">")"</span><span>;    </span></span></li> <li class="alt"><span>        getSession().createQuery(hql).executeUpdate();    </span></li> <li><span>    }    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  写好了Service部分Q接下来开始写Action部分了。因为我们要获取前台传进来的ids数据Q所以在action中得有一个实Cget和setҎ的变量来接收q个数据Q另外,我们要将l果传给前台Q前面章节中我们做联查询的时候,使用的方法是struts把查询的l果数据打包成json格式传给前台Q所以需要一个MapQ然后将通过配置文g中的配置Q将Map转换成json格式。这里我们传到前台的数据比较单,入股删除成功我们传一?quot;true"卛_Q所以不用打包成json格式Q我们通过的ҎM送,道理和前面的一P首相我们得有一个流的对象去保存q个"true"的字节,然后通过配置Q将q个对象通过传到前台。这两个对象我们q是写在BaseAction中,如下Q?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"baseAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> BaseAction<T> </span><span class="keyword">extends</span><span> ActionSupport </span><span class="keyword">implements</span><span> RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {    </span></span></li> <li><span>    </span></li> <li class="alt"><span>    <span class="comment">//获取要删除的idsQ要有get和setҎ  </span><span>  </span></span></li> <li><span>    <span class="comment">//是用来惛_台返回数据的Q这个数据是让struts获取的,然后通过的形式传到前台Q所以实现getҎ卛_  </span><span>  </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> String ids;    </span></span></li> <li><span>    <span class="keyword">protected</span><span> InputStream inputStream;    </span></span></li> <li class="alt"><span>            </span></li> <li><span>        <span class="comment">//下面省略……  </span><span>  </span></span></li> <li class="alt"><span>}    </span></li></ol></div><p>  对应的CategoryAction中的Ҏ如下Q?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"categoryAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryAction </span><span class="keyword">extends</span><span> BaseAction<Category> {    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="keyword">public</span><span> String queryJoinAccount() {    </span></span></li> <li><span>                <span class="comment">//?hellip;…  </span><span>  </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="keyword">public</span><span> String deleteByIds() {    </span></span></li> <li><span>        System.out.println(ids);    </span></li> <li class="alt"><span>        categoryService.deleteByIds(ids);    </span></li> <li><span>        <span class="comment">//如果删除成功׃往下执行,我们?quot;true"以流的Ş式传l前?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        inputStream = <span class="keyword">new</span><span> ByteArrayInputStream(</span><span class="string">"true"</span><span>.getBytes()); </span><span class="comment">//?quot;true"的字节存到流inputStream?nbsp; </span><span>  </span></span></li> <li><span>        <span class="keyword">return</span><span> </span><span class="string">"stream"</span><span>;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  接下来看struts.xml中相应的配置Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">struts</span><span class="tag">></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">constant</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"struts.devMode"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="tag">/></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">package</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"shop"</span><span> </span><span class="attribute">extends</span><span>=</span><span class="attribute-value">"json-default"</span><span class="tag">></span><span class="comments"><!-- jason-defaultl承了struts-default --></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">global-results</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"aindex"</span><span class="tag">></span><span>/WEB-INF/main/aindex.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">global-results</span><span class="tag">></span><span>    </span></span></li> <li><span>    </span></li> <li class="alt"><span>        <span class="comments"><!-- class对应的是Spring中配|该Action的id|因ؓ要交lSpring理 --></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"category_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"categoryAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"jsonMap"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"json"</span><span class="tag">></span><span>    </span></span></li> <li><span>                <span class="comments"><!-- ?nbsp;--></span><span>    </span></span></li> <li class="alt"><span>            <span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"stream"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"stream"</span><span class="tag">></span><span> </span><span class="comments"><!-- 以stream的Ş式,type为stream --></span><span>    </span></span></li> <li class="alt"><span>                <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"inputName"</span><span class="tag">></span><span>inputStream</span><span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span> </span><span class="comments"><!-- imputStream中有要传的数?nbsp;--></span><span>    </span></span></li> <li><span>             <span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li><span>            </span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"account_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"accountAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"index"</span><span class="tag">></span><span>/index.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li><span>            </span></li> <li class="alt"><span>        <span class="comments"><!-- 用来完成pȝ h转发的action,所有的h都交lexecute--></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"send_*_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"sendAction"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"send"</span><span class="tag">></span><span>/WEB-INF/{1}/{2}.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">package</span><span class="tag">></span><span>    </span></span></li> <li><span>    </span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">struts</span><span class="tag">></span><span>   </span></span></li></ol></div><p>  q样我们做好了删除的操作了Q看一下效果:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160508150956670.png" width="600" height="260" alt="SSH电商目实战之八Q查询和删除商品cd功能的实? /></p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160508151059629.png" style="text-align: start;" width="600" height="231" alt="SSH电商目实战之八Q查询和删除商品cd功能的实? /></p><p>  试成功Q我们也可以一ơ性选择多项d除,xQ删除功能做完了?/p>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/768.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=768</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=768&key=f8b5b906</trackback:ping></item><item><title>SSH电商目实战之七QStruts2和Json的整?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/767.html</link><pubDate>Wed, 27 Sep 2017 09:12:47 +0800</pubDate><guid>http://www.vkzldl.live/software/767.html</guid><description><![CDATA[<p>  上一节我们完成了DataGrid昄jason数据Q但是没有和后台联系在一P只是单纯地显CZ我们自己弄的json数据Q这一节我们将json和Struts2整合Q打通EasyUI和Struts2之间的交互?/p><p>  <span style="font-size: medium;"><strong>1. json环境的搭?/strong></span></p><p>  json环境搭徏很简单,导入json的jar包即可,如下Q?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160506215356091.png" alt="SSH电商目实战之七QStruts2和Json的整? /></p><p>  Q注Qjson-lib-2.4的jar包下载地址Q?a rel="nofollow" href="http://download.csdn.net/detail/eson_15/9514985" target="_blank">http://download.csdn.net/detail/eson_15/9514985</a> Q?/p><p>  <span style="font-size: medium;"><strong>2. 完善Action</strong></span></p><p>  在DataGrid控g中有个属性是urlQ可以指定请求数据的url地址Q在上一节我们将q个地址直接讄成了一个具体的json文gQ这里我们将q个url讄成一个actionQ如url:'category_queryJoinAccount.action',表示会去hcategoryAction的queryJoinAccountҎQ文章最后会l出query.jsp的代码)。所以我们需要去完成categoryAction中的queryJoinAccountҎ?/p><p>  在Struts2和json整合前,我们先看一下之前显CZơjson数据都发了哪些请求:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160507000025532.png" alt="SSH电商目实战之七QStruts2和Json的整? /></p><p>  因ؓtype是Categorycȝ一个属性,我们在BaseAction中已l实CModelDriven<Category>接口Q所以这个type会被装到model中,我们不需要管它,可以通过model来获取,但是EasyUI自动发过来的page和rows参数我们需要自p取了Q所以我们可以在BaseModel中增加两个成员变量page和rowsq实现get和setҎQ最后还要考虑一点,q些参数都获得了后,我们Ҏq些参数L据库中查询数据,那么我们查出来的数据攑ֈ哪呢Q而且q要打包成json格式发到前台才能被DataGrid昄。我们先不考虑查询到的数据如何打包成json格式Q我们先考虑把这些数据放C个地方,很自然的惛_了用MapQ因为json格式的数据就是key-value形式的。想到这里,我们l箋完善BaseActionQ?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"baseAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> BaseAction<T> </span><span class="keyword">extends</span><span> ActionSupport </span><span class="keyword">implements</span><span> RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {    </span></span></li> <li><span>    </span></li> <li class="alt"><span>    <span class="comment">//page和rows和分|养IpageMap存放查询的数据,然后打包成json格式用的  </span><span>  </span></span></li> <li><span>    <span class="comment">//page和rows实现get和setҎQpageMap只需要实现getҎ卛_Q因为pageMap不是接收前台参数的,是让struts获取?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> Integer page;    </span></span></li> <li><span>    <span class="keyword">protected</span><span> Integer rows;    </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> Map<String, Object> pageMap = </span><span class="keyword">null</span><span>;</span><span class="comment">//让不同的Action自己d?nbsp; </span><span>  </span></span></li> <li><span>        <span class="comment">//省略get和setҎ……  </span><span>  </span></span></li> <li class="alt"><span>        </span></li> <li><span>    <span class="comment">/******************* 下面q是原来BaseAction部分 *************************/</span><span>    </span></span></li> <li class="alt"><span>    <span class="comment">//service对象  </span><span>  </span></span></li> <li><span>    <span class="annotation">@Resource</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> CategoryService categoryService;    </span></span></li> <li><span>    <span class="annotation">@Resource</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> AccountService accountService;    </span></span></li> <li><span>    </span></li> <li class="alt"><span>    <span class="comment">//域对?nbsp; </span><span>  </span></span></li> <li><span>    <span class="keyword">protected</span><span> Map<String, Object> request;    </span></span></li> <li class="alt"><span>    <span class="keyword">protected</span><span> Map<String, Object> session;    </span></span></li> <li><span>    <span class="keyword">protected</span><span> Map<String, Object> application;    </span></span></li> <li class="alt"><span>            </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setApplication(Map<String, Object> application) {    </span></span></li> <li><span>        <span class="keyword">this</span><span>.application = application;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setSession(Map<String, Object> session) {    </span></span></li> <li><span>        <span class="keyword">this</span><span>.session = session;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> </span><span class="keyword">void</span><span> setRequest(Map<String, Object> request) {    </span></span></li> <li><span>        <span class="keyword">this</span><span>.request = request;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="comment">//ModelDriven  </span><span>  </span></span></li> <li><span>    <span class="keyword">protected</span><span> T model;    </span></span></li> <li class="alt"><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li><span>    <span class="keyword">public</span><span> T getModel() {    </span></span></li> <li class="alt"><span>        ParameterizedType type = (ParameterizedType)<span class="keyword">this</span><span>.getClass().getGenericSuperclass();    </span></span></li> <li><span>        Class clazz = (Class)type.getActualTypeArguments()[<span class="number">0</span><span>];    </span></span></li> <li class="alt"><span>        <span class="keyword">try</span><span> {    </span></span></li> <li><span>            model = (T)clazz.newInstance();    </span></li> <li class="alt"><span>        } <span class="keyword">catch</span><span> (Exception e) {    </span></span></li> <li><span>            <span class="keyword">throw</span><span> </span><span class="keyword">new</span><span> RuntimeException(e);    </span></span></li> <li class="alt"><span>        }       </span></li> <li><span>        <span class="keyword">return</span><span> model;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  好,完善了BaseCategory后,我们可以写categoryAction中的queryJoinAccountҎ了,我们categoryAction中原来的Ҏ全删掉,因ؓ那些都是之前搭徏环境时候测试用的,都不用了Q现在真正开始项目代码了Q?/p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="annotation">@Controller</span><span>(</span><span class="string">"categoryAction"</span><span>)    </span></span></li> <li><span><span class="annotation">@Scope</span><span>(</span><span class="string">"prototype"</span><span>)    </span></span></li> <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryAction </span><span class="keyword">extends</span><span> BaseAction<Category> {    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="keyword">public</span><span> String queryJoinAccount() {    </span></span></li> <li><span>    </span></li> <li class="alt"><span>        <span class="comment">//用来存储分页的数?nbsp; </span><span>  </span></span></li> <li><span>        pageMap = <span class="keyword">new</span><span> HashMap<String, Object>();    </span></span></li> <li class="alt"><span>            </span></li> <li><span>        <span class="comment">//Ҏ关键字和分页的参数查询相应的数据。这个方法我们在Service中写q了Q当时完成联查?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        List<Category> categoryList = categoryService.queryJoinAccount(model.getType(), page, rows);    </span></li> <li><span>        pageMap.put(<span class="string">"rows"</span><span>, categoryList); </span><span class="comment">//存储为JSON格式Q从上一节的json文g可以看出Q一个key是total,一个key是rowsQ这里先把rows存放?nbsp; </span><span>  </span></span></li> <li class="alt"><span>        <span class="comment">//Ҏ关键字查询总记录数  </span><span>  </span></span></li> <li><span>        Long total = categoryService.getCount(model.getType()); <span class="comment">//q个Ҏ没写Q我们等会儿去Service层完善一?nbsp; </span><span>  </span></span></li> <li class="alt"><span><span class="comment">//      System.out.println(total);  </span><span>  </span></span></li> <li><span>        pageMap.put(<span class="string">"total"</span><span>, total); </span><span class="comment">//存储为JSON格式Q再把total存放?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    </span></li> <li><span>        <span class="keyword">return</span><span> </span><span class="string">"jsonMap"</span><span>;    </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>}    </span></li></ol></div><p>  q样Action我们写好了Q现在Action拿到前台传来的参敎ͼ然后Ҏ参数查询了指定type的总记录数Q以及指定type的所有商品,q且按照json中指定的keyQ即total和rowsQ进行存放,攑֜HashMap中了Q之后只要将q个HashMap中的数据打包成json格式发送到前台可以被DataGrid昄了。我们先把这个HashMap放这Q先d善了Service层的代码后,再来打包q个HashMap中的数据?/p><p>  <span style="font-size: medium;"><strong>3. 完善categoryService</strong></span></p><p>  从上面的categoryAction中可知,需要在categoryService中增加一个getCountҎQƈ且要在具体实现类中实现好Q实现如下:</p><div class="codeText"><div class="codeHead">Java代码</div><ol start="1" class="dp-j"> <li class="alt"><span><span class="comment">//CategoryService接口  </span><span>  </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">interface</span><span> CategoryService </span><span class="keyword">extends</span><span> BaseService<Category> {    </span></span></li> <li class="alt"><span>    <span class="comment">//查询cd信息Q联管理员  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> List<Category> queryJoinAccount(String type, </span><span class="keyword">int</span><span> page, </span><span class="keyword">int</span><span> size); </span><span class="comment">//使用cd的名U查?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    <span class="comment">//Ҏ关键字查询总记录数  </span><span>  </span></span></li> <li><span>    <span class="keyword">public</span><span> Long getCount(String type);    </span></span></li> <li class="alt"><span>}    </span></li> <li><span>    </span></li> <li class="alt"><span><span class="comment">//CategoryServiceImpl实现c?nbsp; </span><span>  </span></span></li> <li><span><span class="annotation">@SuppressWarnings</span><span>(</span><span class="string">"unchecked"</span><span>)    </span></span></li> <li class="alt"><span><span class="annotation">@Service</span><span>(</span><span class="string">"categoryService"</span><span>)    </span></span></li> <li><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> CategoryServiceImpl </span><span class="keyword">extends</span><span> BaseServiceImpl<Category> </span><span class="keyword">implements</span><span> CategoryService {    </span></span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> List<Category> queryJoinAccount(String type, </span><span class="keyword">int</span><span> page, </span><span class="keyword">int</span><span> size) {    </span></span></li> <li><span>        String hql = <span class="string">"from Category c left join fetch c.account where c.type like :type"</span><span>;    </span></span></li> <li class="alt"><span>        <span class="keyword">return</span><span> getSession().createQuery(hql)    </span></span></li> <li><span>                .setString(<span class="string">"type"</span><span>, </span><span class="string">"%"</span><span> + type + </span><span class="string">"%"</span><span>)    </span></span></li> <li class="alt"><span>                .setFirstResult((page-<span class="number">1</span><span>) * size) </span><span class="comment">//从第几个开始显C?nbsp; </span><span>  </span></span></li> <li><span>                .setMaxResults(size) <span class="comment">//昄几个  </span><span>  </span></span></li> <li class="alt"><span>                .list();    </span></li> <li><span>    }    </span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="annotation">@Override</span><span>    </span></span></li> <li class="alt"><span>    <span class="keyword">public</span><span> Long getCount(String type) {    </span></span></li> <li><span>        String hql = <span class="string">"select count(c) from Category c where c.type like :type"</span><span>;    </span></span></li> <li class="alt"><span>        <span class="keyword">return</span><span> (Long) getSession().createQuery(hql)    </span></span></li> <li><span>            .setString(<span class="string">"type"</span><span>, </span><span class="string">"%"</span><span> + type + </span><span class="string">"%"</span><span>)    </span></span></li> <li class="alt"><span>            .uniqueResult(); <span class="comment">//q回一条记?总记录数  </span><span>  </span></span></li> <li><span>    }    </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  到现在ؓ止,q个数据库中数据的获取这条\打通了Q前面两步完成了从前?->数据?->取数据,接下来就开始打包HashMap中存攄数据Q然后发l前C?/p><p>  <span style="font-size: medium;"><strong>4. 配置struts.xml</strong></span></p><p>  在struts.xml中通过配置可以完成对指定数据的打包,我们先看一下struts.xml中的配置Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">struts</span><span class="tag">></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">constant</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"struts.devMode"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"true"</span><span> </span><span class="tag">/></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">package</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"shop"</span><span> </span><span class="attribute">extends</span><span>=</span><span class="attribute-value">"json-default"</span><span class="tag">></span><span class="comments"><!-- jason-defaultl承了struts-default --></span><span>    </span></span></li> <li><span>        </span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">global-results</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"aindex"</span><span class="tag">></span><span>/WEB-INF/main/aindex.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">global-results</span><span class="tag">></span><span>    </span></span></li> <li><span>    </span></li> <li class="alt"><span>        <span class="comments"><!-- class对应的是Spring中配|该Action的id|因ؓ要交lSpring理 --></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"category_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"categoryAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="comments"><!-- 必须要先djson包,然后上面l承json-default --></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"jsonMap"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"json"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                <span class="comments"><!-- 要{换成json对象的数?nbsp;--></span><span>    </span></span></li> <li><span>                <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"root"</span><span class="tag">></span><span>pageMap</span><span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                <!-- 配置黑名单,qo不需要的选项 Q支持正则表辑ּ    </span></li> <li><span>                json格式Q{total:3,rows:[{account:{id:2,login:"user",name:"客服A",pass:"user"},hot:true,id:3,…}]}    </span></li> <li class="alt"><span>                --<span class="tag">></span><span>    </span></span></li> <li><span>                <span class="tag"><</span><span class="tag-name">param</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"excludeProperties"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                    <span class="comments"><!-- rows[0].account.pass--></span><span>    </span></span></li> <li><span>                                        <span class="comments"><!-- q里昄不了正则表达式, CSDN的一个bugQ我接个图放下面 --></span><span>    </span></span></li> <li class="alt"><span>                <span class="tag"></</span><span class="tag-name">param</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li><span>            </span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"account_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"accountAction"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"{1}"</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"index"</span><span class="tag">></span><span>/index.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li><span>            </span></li> <li class="alt"><span>        <span class="comments"><!-- 用来完成pȝ h转发的action,所有的h都交lexecute--></span><span>    </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">action</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"send_*_*"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"sendAction"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">result</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"send"</span><span class="tag">></span><span>/WEB-INF/{1}/{2}.jsp</span><span class="tag"></</span><span class="tag-name">result</span><span class="tag">></span><span>    </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">action</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">package</span><span class="tag">></span><span>    </span></span></li> <li><span>    </span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">struts</span><span class="tag">></span><span>    </span></span></li></ol></div><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160506235402701.png" alt="SSH电商目实战之七QStruts2和Json的整? /></p><p>  从上面的配置可以看出Q首先package要承json-defaultQ因为json-defaultl承了struts-defaultQ因为在json的jar包里有个struts2-json-plugin-2.3.24.1.jarQ打开卛_看到里面有个struts-plugin.xmlQ打开卛_看到json-default是承了struts-defaultQ?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160507000224276.png" width="600" height="369" alt="SSH电商目实战之七QStruts2和Json的整? /></p><p>  接下来我配置<result>Qname是刚刚actionq回的字W串Qtype一定要配成json。然后就是result中的参数了,首先必须要配的就是name为root的参敎ͼq个参数要配成刚刚需要{换的HashMap对象Q即我们定义的pageMapQ有了这个参数的配置Qstruts才会pageMap中的数据打包成json格式。然后就是配|黑名单Q黑名单的意思就是告诉struts在打包的时候,哪些字段不需要打包,比如理员密码之cȝ信息Q由上面注释中的jason格式可以看出rows[0].account.pass表示密码字段Q但是数据肯定不止一条,所以我们得用正则表辑ּ来表C,q样所有密码都不会被打包到json中?/p><p>  <span style="font-size: medium;"><strong>5. 修改query.jsp内容</strong></span></p><p>  到此Q我们已l将数据打包成了json格式了,接下来我们完善一下前台query.jsp的内容就可以让DataGrid正确昄了:</p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        $(function(){    </span></li> <li><span>            $('#dg').datagrid({       </span></li> <li class="alt"><span>                //url地址改ؓhcategoryAction    </span></li> <li><span>                url:'category_queryJoinAccount.action',    </span></li> <li class="alt"><span>                loadMsg:'Loading......',    </span></li> <li><span>                queryParams:{type:''},//type参数Q这里不需要传具体的typeQ因为我们要昄所有的    </span></li> <li class="alt"><span>                //width:300,    </span></li> <li><span>                fitColumns:true,    </span></li> <li class="alt"><span>                striped:true,    </span></li> <li><span>                nowrap:true,    </span></li> <li class="alt"><span>                singleSelect:true,    </span></li> <li><span>                pagination:true,    </span></li> <li class="alt"><span>                rowStyler: function(index,row){    </span></li> <li><span>                    console.info("index" + index + "," + row)    </span></li> <li class="alt"><span>                    if(index % <span class="attribute">2</span><span> == 0) {    </span></span></li> <li><span>                        return 'background-color:#fff;';    </span></li> <li class="alt"><span>                    } else {    </span></li> <li><span>                        return 'background-color:#ff0;';    </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                        </span></li> <li class="alt"><span>                },          </span></li> <li><span>                frozenColumns:[[    </span></li> <li class="alt"><span>                    {field:'checkbox',checkbox:true},    </span></li> <li><span>                    {field:'id',title:'~号',width:200}    //q里的field字段要和json数据中的一?nbsp;                </span></li> <li class="alt"><span>                ]],    </span></li> <li><span>                columns:[[                         </span></li> <li class="alt"><span>                    {field:'type',title:'cd名称',width:100, //字段type    </span></li> <li><span>                        formatter: function(value,row,index){    </span></li> <li class="alt"><span>                            return "<span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">" + value + "</span><span class="tag">></span><span>" + value + "</span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span>";    </span></span></li> <li><span>                        }    </span></li> <li class="alt"><span>                    },        </span></li> <li><span>                    {field:'hot',title:'热卖',width:100,  //字段hot    </span></li> <li class="alt"><span>                        formatter: function(value,row,index){    </span></li> <li><span>                            if(value) { //如果是hotQ该gؓtrueQvalue是boolean型变?nbsp;   </span></li> <li class="alt"><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">checked</span><span>=</span><span class="attribute-value">'checked'</span><span> </span><span class="attribute">disabled</span><span>=</span><span class="attribute-value">'true'</span><span>"; //N?nbsp;   </span></span></li> <li><span>                            } else {    </span></li> <li class="alt"><span>                                return "<span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">'checkbox'</span><span> </span><span class="attribute">disable</span><span>=</span><span class="attribute-value">'true'</span><span>"; //不勾?nbsp;   </span></span></li> <li><span>                            }    </span></li> <li class="alt"><span>                        }    </span></li> <li><span>                    },    </span></li> <li class="alt"><span>                    {field:'account.login',title:'所属管理员',width:200, //account.login理员登录名    </span></li> <li><span>                        formatter: function(value,row,index){    </span></li> <li class="alt"><span>                            if(row.account != null && row.account.login != null) {    </span></li> <li><span>                                return row.account.login; //如果d名不为空Q显C登录名    </span></li> <li class="alt"><span>                            } else {    </span></li> <li><span>                                return "此类别没有管理员";    </span></li> <li class="alt"><span>                            }    </span></li> <li><span>                    }       </span></li> <li class="alt"><span>                    }    </span></li> <li><span>                ]]        </span></li> <li class="alt"><span>            });     </span></li> <li><span>        });    </span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>      </span></li> <li><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"dg"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  <span style="font-size: medium;"><strong>6.  试昄l果</strong></span></p><p>  最后我们测试一下DataGrid的显C结果,如下Q?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160507000536723.png" width="600" height="258" alt="SSH电商目实战之七QStruts2和Json的整? /></p><p>  到这里,我们成功整合了Struts2和jsonQ现在可以和前台传输json格式的数据了?/p><p> </p><p> </p>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/767.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=767</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=767&key=d3fa2df2</trackback:ping></item><item><title>长文Q内容业的赢家与输?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/internet/766.html</link><pubDate>Wed, 20 Sep 2017 10:36:58 +0800</pubDate><guid>http://www.vkzldl.live/internet/766.html</guid><description><![CDATA[<p>  <span style="font-size: medium;"><strong>一</strong></span></p><p>  自微信公?2q开始以来,一波内容创业兴赗?/p><p>  在内容创业大潮的推动下,涌现Z一些被UCؓ“头部”的大受有些大h从博客迁U而来Q有些大号则本n׃直是机构媒体或成名大VQ还有些大号Q在若干q前Ҏc籍无名?/p><p>  从零到年入百万千万,估值动辄上亿,当然是赢Ӟq是不小的赢家?/p><p>  但对于业来_Z我一向的“渠道为王 内容为本”的观点,一个内容生产源Q比起^台渠道,影响力还是弱很多?/p><p>  所以,本篇着重谈的,依然是渠?q_cȝ赢家。(以图文ؓ主,视音频类Ҏ再思考思考,再码一)</p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  W一个大赢家Q今日头条?/p><p>  今日头条创徏?2q?月,五年的时_当下估值已q入百亿元׃部,2017q收入过一百五十亿人民币,应无悬念?/p><p>  今日头条的早期崛P和内容创业关pdƈ不是太大?/p><p>  2014q?月,今日头条宣布完成1亿美金融资,估?ѝ这个新d起了整个媒体圈的xQ也引发了后来的所谓版权争议。但在当Ӟ内容创业虽已悄然hQ不q谈不上是什么风口。我们基金曾l以100万h民币估值的hQ还投资了一个号。在今天Q是很难扑ֈq种估值项目的?/p><p>  但今日头条后来更为彪悍的发展Q与内容创业颇有关pM。因为大多数内容创业者,即便重心在微信公号上Q依然会在头条同步更新?/p><p>  q给头条带来了巨大的内容数量Q对应的Q也带来了巨大的广告位数量?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  插播?/p><p>  观察一个以q告Z要收入模式的内容生意Q?ldquo;q告位数?rdquo;是一个非帔R要的研判指标?/p><p>  一般意义上所谓的媒体——也就是一个内容生产端——通常会被判定?ldquo;体量有限”。体量有限的原因在于,它的q告位是有限的?/p><p>  q告依附于内容之上,但内Ҏ量存在天花板后,q告收入׃定会存在天花ѝ而要提升内容数量Q对于内容生产型的媒体来_q就意味着提高成本?/p><p>  q就是ؓ什么传l媒体大多都?ldquo;集团式生?rdquo;的原因,一个组l办了好多媒体,商业上,是x升广告位数量?/p><p>  不过内容q_和内容生端不同:它的内容存在两个特点Q?、相对于一个独立的生端,数量极其巨大Q?、成本非怽?/p><p>  互联|的几个做出巨大生意的内容^収ͼ都是q样的。从桌面的门户到搜烦Q从Ud的内容聚合APP到社会化媒体?/p><p>  它们的广告位数量甚至可以?ldquo;无穷”来表达,只要天下依然有生产内容的人。一茬一茬的韭菜Q一波一波的毛Q取之不用之不竭?/p><p>  q段插播其实是很单的媒体商业入门知识Q但有鉴于至今还有h认ؓ“内容为王”Q必d出来以正视听?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  头条q不是扉KL衣钵Q虽然它看上d门户有点像。而且它当qv家的时候,主要的竞品也都是门户做的Q比如搜狐的新闻客户端。至今,L腾讯门户的OMGQ网l媒体事业群Q还是把今日头条视ؓ最重要的竞争对手之一?/p><p>  但本质上Q张一鸣接q的Q是李R宏的旗帜?/p><p>  一个很表象的证据就是,今日头条q不惌|ȝ辑这U岗位(后来Z某些原因讄了)Q但门户恐怕组建团队的W一件事是锚定一个ȝ辑?/p><p>  你知道百度(不是癑ֺ新闻q种业务U)的ȝ辑是谁么Q(W)</p><p>  核心关键词是Q匹配?/p><p>  癑ֺ的方式是用户发v一个关键字Q然后进行相兛_容匹配?/p><p>  头条的方式是挖掘用户的属性指标,推送内容进行匹配?/p><p>  一个是拉取QpullQ一个是推送(pushQ,但只是由于时代原因造成的技术用不同。根子上Q都是与用户兴趣匚w加蝲内容?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  头条的崛P当然有主观努力、战术用得当之cȝ原因——M一个品崛起都M开自n努力。但我还是想讲一讲大ѝ?/p><p>  头条占了很大的移动互联网的便宜?/p><p>  癑ֺ搜烦和移动互联网屏是不匚w的。在桌面Q百度是最大的量入口Q真的没有之一。但在移动小屏,q个力量被消解了?/p><p>  癑ֺ作ؓ量入口Q一家就掌握了中国整个互联网营销盘子?-3成的投放费用。对于甲方而言Q我UC?ldquo;枢纽式投?rdquo;。移动小屏消解了它的力量Q等同于部分释放一大笔投放费用。而百度一q营收数百亿人民币,即便是点零头Q也够一个startup公司吃了?/p><p>  甲方的一部分预算向了公P直接和内容生产端ҎQ另外有一部分Q就在寻找新的枢U式投放。在腾讯整个q告体系q没有搭载v来之前,q个枢纽Q内容聚合类APP是一个不错的选择?/p><p>  头条准确地掌握住了这个大势,而另外一个内容APPQ搜狐新闻,虽然是最早达到装亿的Q显然战略判断失误,没有l箋p?/p><p>  C头条的第五个q头Q也是今年Q正如当q百度在W五个年头创建百度知道(2005q?月)一P头条推出了自q问答产品Q悟I问{?/p><p>  q个产品遭到了业内至是口头上的dQ包括知乎和微博。而这一变化Q也可以让我们观察到Q今日头条的ҎQ已l不再是当年叫喊着版权的内容生产端Q媒体)Q也不完全是诸家门户的聚合新闻APP?/p><p>  它的敌h更多了?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  W二个赢Ӟ新浪微博?/p><p>  微博?9q开始内运营,到现在已l八q。与头条不同Q这是一个横跨桌面到Ud阶段的品:微博一开始还是很重视桌面表现的,现在应该说几乎所有力量都在移动上?/p><p>  微博当下市值已l超了它的老师Qtwitter?/p><p>  微博从初创到J荣到衰落又到繁荣,充分证明了一点,内容q_的核心要务是信息整合。微博盛于v量的W三方信息供l,C信息q于嘈杂Q又盛于部落化后信息相对有效的分拣与触达。这里面也包括对营销信息的控制?/p><p>  微博分ؓ两个阶段Q中间大致以2013qؓ分割Uѝ?/p><p>  W一个阶D,大V{略。在q个阶段中,几乎所有h都看好微博,甚至喊出“围观改变中国”q类不乏天真的口受?/p><p>  W二个阶D,部落化策略,也就是扶持中V。这个阶D微博努力下沉,q开始收~或整肃W三方营销势力。但q个阶段刚开始非常艰难,以至于在北上q很多会议论坛,都会听到“现在没h用微?rdquo;了之cȝ说法?/p><p>  我和大多数评不同,W一个阶D|看空W二阶段我看多。而空d的一个重要原因,在于一个指标?/p><p>  与前文所提及的广告位数量一Pq个指标也是观察一个以q告Z要收入模型的内容业态的关键数据Q广告主数量?/p><p>  12q四季度的时候,曹国伟在披露微博数据时提刎ͼ后者的q告L量是80?mdash;—嗯,真的Q没写个零?/p><p>  时至今日Q已l坐拥数十万q告主,是非常标准的“生意的大故?rdquo;?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  微博的传播模型是一个很h的方式。它既有用户到用L所谓社会化传播Q也有微博运营方自己做的所谓主题议E设|。你很难说微博场域里是中心化的,也不能简单地一刀切下去说q是M心化的?/p><p>  p于新这L门户Q其实微博无法摆脱骨子里媒体公司的基因。所以对微博正确的描q是Q社会化媒体Qsocial mediaQ,而不是社交网l(social networkQ。可以说Q本文说的四大赢家中Q最接近媒体的,是微博?/p><p>  头条上的内容来自于外部内Ҏ?同步Q以及,直接在头条^C推送(或自行同步其它^台帐P。后者被UC为头条号。头条号目前总量已经辑ֈ80万的规模Q其中有八成Q属于江湖意义上?ldquo;自媒?rdquo;——一般理解ؓ正统传统媒体开讄媒体帐号?/p><p>  微博׃此非怸同。微博几乎没有来自第三方同步或抓取的内容。几乎所有的微博用户Q都在微博上生、传播内宏V故而微博很碰到所谓版权争议的问题?/p><p>  也正因ؓ此,微博视自׃ؓ一个内容生产部门,对头条的同步来不能忍。才会有最q两天发生的一ơ昏招。这个匪h思的对用戯作权的权利主张,其背后动因就在于Q广告是附着于内容上的。微博大概以为,打掉了头条(包括悟空问答Q对微博的同步,可能会减后者可卖的q告位吧?/p><p>  直至今日Q微博还是有很强的从新浪传承q来的内Ҏ维的媄子,而不是如头条、百度那般,骨子里认己是一家技术公司。其实微信公号^台方也从来不认ؓ自己是一家内容公司,虽然表象上如微博般,也是量的第三方在^C生内容?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  W三个大赢家Q腾讯?/p><p>  的确Q类似朋友圈q告、广炚w之c,和WXGQ微信事业群Q关pM大,也的OMGq在苦苦L自己的存在感Q但我说的是腾讯Q一个整体的腾讯?/p><p>  腾讯主要的收入依然在游戏上,所以它的市值飙升和内容赢家是否有关联还不好讌Ӏ依然看一下广告的表现?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20170920103019.jpg" alt="长文Q内容业的赢家与输? /></p><p>  q张表格能够清晰地反映出Q广告占整个腾讯收入盘子的比重在逐年上升?/p><p>  同时Q我们也能从两栏同比增幅中注意到Q腾讯广告收入的增长相当快,跑过整个腾讯收入大盘?/p><p>  那么Q腾讯广告收入发力在哪里呢?</p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20170920103045.jpg" alt="长文Q内容业的赢家与输? /></p><p>  q张表统计的是腾讯两个业务(游戏和广告)?014qQ1?017qQ2的逐季表现?/p><p>  首先我们能看到的是,游戏在腾讯整体收入的占比最q五个季度已l长期低?成?017q现象游戏王者荣耀的耀D玎ͼ使得Q1游戏占比L了一直下滑的占比Q但C二季度,度过q告行业营收最惨E的Q1之后Q占比重新回落?/p><p>  15q开始的历个季度Q广告单季度同比增幅非常可观。细查下来,腾讯q告两翼Q其效果q告Q?017q略调了l计口径Q改名ؓC交及其它广告)同比增幅越其品牌广告(2017q略调了l计口径Q改名ؓ媒体q告Q?/p><p>  而效果广告,主要的构成部分就是朋友圈q告、微信公号上的广炚w等?/p><p>  2013q开始酝?4q正式兴L内容创业Q公h量从十万U到百万U到今天的千万Qؓq点通广告提供了大量的可售卖q告位置。而微信用户对朋友圈的_着?mdash;—按照腾讯U技企鹅智酷的说法,朋友圈力压点对点通讯Q是微信头号使用频率的功?mdash;—也得朋友圈q告得以大卖?/p><p>  所谓的品牌q告Q腾讯官方说法是主要反映来自Ud端^収ͼ如腾讯新闻)、腾讯视频等收入。也是_q个部分Q的可以基本对标今日头条。整?016q_收入也过百亿?017q调整口径后Q被定义为媒体广告收入(主要包括新闻、视频及音乐的广告位产生的收入)Q上半年也已l斩?5亿余人民币,而广告行业一般下半年高于上半q_全年收入q一百五十亿也应无疑问?/p><p>  q样的成l单Q说腾讯要提防今日头条倒也没差Q但要说被头条压Ӟ昄属于杞h忧天了?/p><p>  腾讯Q理所当然的,是内容行业里的大赢家?/p><p>  <span style="font-size: medium;"><strong>?/strong></span></p><p>  W四个大赢家Q阿里?/p><p>  与类亚马逊的京东所不同的是Q阿里本质上的业务ƈ不是零售Q而是q告Q商家的营销费用Q。号U免费的淘宝靠帮助商家推q获取利润,品牌扎堆的天猫,则是抽取交易佣金?/p><p>  京东的交易量QGMVQ越大,q不一定会形成利润Q因为它是差h式,有进货成本。但阉KpGMV大Q自w的收入也就高QGMV到达一定规模后Q以Ş成利润?/p><p>  但阿里这?ldquo;虚拟商业C”的模式,伴随着Ud兴v的小屏化后,面严峻的考验Q推荐位营销位广告位相对PC端还要不够用。如果应对不当,会D极速推高^C商家的营销成本Q得所有商家进入囚徒困境,最l整个生态崩盘?/p><p>  阉K的应Ҏ式是Q引入广告化的内容,一斚w增强用户_度Q一斚w创造无I多的广告位|?/p><p>  淘宝达hdQ发展到极盛Ӟ有百万之众?/p><p>  <span style="font-size: medium;"><strong>十一</strong></span></p><p>  早期的所谓内容生产极其简单,被称为双列清单:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20170920103126.jpg" alt="长文Q内容业的赢家与输? /></p><p>  一句话+若干个商品图片,可以抽佣。前提是Q达人清晰这里的游戏规则Q哪些商品{化率高、䄦金高。据U有q入可达数百万?/p><p>  但这些内定w常轻Q而且制作者通常会偏向于所谓爆ƾ。这使得淘系量依然会向头部商品聚焦Q无法对q_上大部分商家形成普惠式的量供给?/p><p>  阉K开始改变策略?/p><p>  <span style="font-size: medium;"><strong>十二</strong></span></p><p>  有必要略微介l一下淘pd容生态?/p><p>  今天你打开手机淘宝Q与其说是打开了一个网l购物应用,不如说是打开了一本消费类电子刊物。这个刊物的内容相当庞杂量Q天量内容?ldquo;_֓购物指南”?/p><p>  首屏所出现?ldquo;淘宝头条”Q理所当然的是一U内容,或可定位成消贚w域的今日头条?/p><p>  但往下的cM有好货、必买清单、男范Q如果是女生打开淘宝Q看到的是爱逛街Q等{,都是它的内容板块。只不过淘宝头条的内容偏“资讯”c,而这些板块的内容?ldquo;D”cR䄦金结方式前者以CPCZQ后者以CPSZ?/p><p>  q些内容按照阉K的定义,被称?ldquo;公域内容”?/p><p>  而底部导航的W二个按?ldquo;微淘”Q所指向的,被称?ldquo;U域内容”。公域和U域内容所产生的䄦金,l算方式q不相同?/p><p>  之所以前者被UCؓ公域内容Q是因ؓ内容生者所q行的内容生产,是有固定格式的,且用户也是根据算法匹配的。而后者的U域内容Q生产格式ƈ不强调,用户也是因ؓx了某个内ҎQ可能就是个商铺Q也可能是一个内容生产者)才会被这些内容触达?/p><p>  与消费密切相关的内容Q大部分情况下,q没有太强的时效性。一对某皮衣进行介l的文字Q有可能?ldquo;有好?rdquo;或?ldquo;必买清单”之类的板块中有长辑֍q的曝光期,换而言之,内容生者可以指望这文章长辑֍q的佣金获取。这是淘pd容,与其它内容^台非怸同的地方?/p><p>  <span style="font-size: medium;"><strong>十三</strong></span></p><p>  阉K的策略调整在于两个方面:</p><p>  1、鼓励有一定篇q的内容生Q当然也包括视频、直播)Q一句话加一堆商品照片的双列清单在今天已l很见了?/p><p>  2、在公域内容里的佣金计算上做了大刀阔斧的调整。一份CPSQ扣除阿里系提走?成,剩下?成,内容生者只能立刻获得其中的10%Q也是整个CPS?%Q,q有9成(相当?3%Q被|入资金池,由阿里进行动态分配。大致上Q就是对所谓好内容但可能{化不高进行扶持?/p><p>  阉K于今q启动daren.taobao.comQ中文名字叫阉K创作q_。号U要在三q里投入100亿来扶持内容?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20170920103154.jpg" alt="长文Q内容业的赢家与输? /></p><p>  h意上方的频道投稿?/p><p>  Zw䆾标签——官方Ҏ具体情况Q赋予不同的标签Q比如图文类、短视频cR主播类Q不开店)、红人类Q开店的Q、淘女郎Q一般背后有l纪公司Q等{。据_q种w䆾标签高达上百?mdash;—内容生者可以申L应的频道开通,qҎ地q行写作。比如投?ldquo;有好?rdquo;频道Q内容就有可能出现手淘首늚有好货板块。但频道投稿有非怸格的格式要求Q不是像微信公号那样Q想怎么写就怎么写,x么排版怎么排版?/p><p>  另外商家也可以发布VdQ由内容生者去申领Q其l算方式以CPMZ?/p><p>  当下Q月入过十万的生产者,已经过1500?mdash;—其实q个数字q不太低。微信公h入过十万的,又有多少呢?</p><p>  而对于阿里来_内容有效~解了整个淘p营销资源紧缺Q商家囚徒困境的局面。我个h认ؓQ最C季胦报的亮眼表现Q其背后Q与内容是分不开的?/p><p>  <span style="font-size: medium;"><strong>十四</strong></span></p><p>  有赢家就有输家?/p><p>  癑ֺ是当仁不让的输家之一Q在贴吧事g之后则西事件之前,癑ֺ的胦务表现就Z问题。我专门写过《这两年癑ֺ发生了什么》分析它13?4?5q的状况?/p><p>  癑ֺ更冤的一件事是:多少公号狗拿着癑ֺ写(pinQ稿子,营销收入赚得盆满钉|Q却和百度一点关p都没有?/p><p>  癑ֺq有Z么?</p><p>  我不知道?/p><p>  <span style="font-size: medium;"><strong>十五</strong></span></p><p>  有个公司,我看有机会?/p><p>  q家公司的创始人叫徐达内,公司名字叫新榜,一向是?B的生意,估值最q在一轮华人、华盖投完后Q也快近十亿人民币了——比v上面的赢家输Ӟ当然q是公司?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20170920103222.jpg" alt="长文Q内容业的赢家与输? /></p><p>  q张囑֏映了?C?B的不同。做2C的,做得好的Q可以万古流芻I?B比较默默无M?/p><p>  徐达内出w媒体hQ对万古芳q种事很看重?/p><p>  于是Q他军_启动一?C的项目。原来想?ldquo;见识”Q不巧正好和另外一家也是华人文化基金投资的华尔街见d的一个品撞名,所以现在改名叫“微见”?/p><p>  q个产品的核心就是汇聚一帮喜Ƣ扯淡的kol们,对各U新闅R文章进行评头论뀂用户主要是ȝ他们评头的?/p><p>  如果你知道徐辑ֆ早年写过媒体札记qg事的话,你会看到q个事在微见里的影子的?/p><p> 转自Q微信公众号 扯E?/p>]]></description><category>IT互联|?/category><comments>http://www.vkzldl.live/internet/766.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=766</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=766&key=83824101</trackback:ping></item><item><title>SSH电商目实战之六Q基于DataGrid的数据显C?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/765.html</link><pubDate>Tue, 19 Sep 2017 08:54:19 +0800</pubDate><guid>http://www.vkzldl.live/software/765.html</guid><description><![CDATA[<p>  EasyUI中DataGrid以表格Ş式展C数据,q提供了丰富的选择、排序、分l和~辑数据的功能支持。DataGrid的设计用于羃短开发时_q且使开发h员不需要具备特定的知识。它是轻量的且功能丰富。单元格合ƈ、多列标题、冻l列和页脚只是其中的一部分功能?/p><p>  <span style="font-size: medium;"><strong>1. 回顾一下第4节内?/strong></span></p><p>  在第4节中Q我们用EasyUI搭徏好了左侧菜单栏,q且通过点击菜单选项在右边弹出对应的选项卡。这节我们来使用DataGrid把右边的选项卡部分做好。先看一下第4节中最后的aindex.jsp文gQ也可参见第4节中的内容)Q?/p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506164649701" width="600" height="668" alt="" /></p><p>  <span style="font-size: medium;"><strong>2. 创徏DataGrid控g的几U方?/strong></span></p><p>  DataGrid昄数据是json格式的,所以我们首先要把从后台获取到的数据打包成Jason格式Q然后传到前台来让DataGrid来显C,q一节我们先不从后台获取数据Q先自己准备一?json文gQ里面有ison格式的数据,然后我们来让DataGird昄Q先把显C功能做好,再请求后台数据?/p><p>  我们先从EasyUI的参考文档中看一下DataGrid昄的格式是什么样的,如下图所C:</p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506173210502" width="600" height="453" alt="" /></p><p>  我们沿着参考文档往下看Q我们发现DataGridI间是通过<table>来创建的Q有三种创徏方式Q?/p><p>  W一U:从现有的表格元素创徏DataGridQ在HTML中定义列、行和数据?/p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506174815336" width="600" height="256" alt="" /></p><p>  W二U:通过<table>标签创徏DataGrid控g。在表格内?lt;th>标签定义列?/p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506174930658" width="600" height="180" alt="" /></p><p>  W三U:使用Javascriptd建DataGrid控g?/p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506175122364" width="600" height="182" alt="" /></p><p>  我们采取W三U,用jsd建DataGrid控gQ首先我们得先准备一个存储了json格式数据的文Ӟ在WebRoot/jquery-easyui-1.3.5/demo/datagrid/下面有几个json文gQ我们选择一个datagrid_data1.jsonQ拷贝到WebRoot目录下,修改一下参敎ͼ{会我们要来昄q个json文g里的数据。如下:</p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span>{</span><span class="string">"total"</span><span>:10,</span><span class="string">"rows"</span><span>:[    </span></span></li> <li><span>    {<span class="string">"code"</span><span>:</span><span class="string">"FI-SW-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Koi"</span><span>,</span><span class="string">"price"</span><span>:10.00},    </span></span></li> <li class="alt"><span>    {<span class="string">"code"</span><span>:</span><span class="string">"K9-DL-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Dalmation"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li><span>    {<span class="string">"code"</span><span>:</span><span class="string">"RP-SN-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Rattlesnake"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li class="alt"><span>    {<span class="string">"code"</span><span>:</span><span class="string">"RP-LI-02"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Iguana"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li><span>    {<span class="string">"code"</span><span>:</span><span class="string">"FL-DSH-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Manx"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li class="alt"><span>    {<span class="string">"code"</span><span>:</span><span class="string">"FL-DSH-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Manx"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li><span>    {<span class="string">"code"</span><span>:</span><span class="string">"FL-DLH-02"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Persian"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li class="alt"><span>    {<span class="string">"code"</span><span>:</span><span class="string">"FL-DLH-02"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Persian"</span><span>,</span><span class="string">"price"</span><span>:12.00},    </span></span></li> <li><span>    {<span class="string">"code"</span><span>:</span><span class="string">"AV-CB-01"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Amazon Parrot"</span><span>,</span><span class="string">"price"</span><span>:92.00},    </span></span></li> <li class="alt"><span>    {<span class="string">"code"</span><span>:</span><span class="string">"AV-CB-03"</span><span>,</span><span class="string">"productname"</span><span>:</span><span class="string">"Amazon Parrot"</span><span>,</span><span class="string">"price"</span><span>:92.00}    </span></span></li> <li><span>]}    </span></li></ol></div><p>  我们可以看到Qjson数据格式是:"key1": value1, "key2":value2。每个value里面又可以是数组Q数l中保存新的Jason数据?/p><p>  有了json文gQ我们接下来可以设计DataGrid控g了,整个DataGrid都是在query.jsp中设计的Q因昄的内容就是query.jsp中的内容。我们来看看query.jsp面Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        $(function(){    </span></li> <li><span>            $('#dg').datagrid({       </span></li> <li class="alt"><span>                //h数据的url地址Q后面会Ҏh我们自己的url    </span></li> <li><span>                url:'datagrid_data.json',    </span></li> <li class="alt"><span>                loadMsg:'Loading......',    </span></li> <li><span>                queryParams:{type:''},//参数    </span></li> <li class="alt"><span>                //width:300,    </span></li> <li><span>                fitColumns:true,//水^自动展开Q如果设|此属性,则不会有水^滚动条,演示ȝ列时Q该参数不要讄    </span></li> <li class="alt"><span>                //昄斑马U?nbsp;   </span></li> <li><span>                striped:true,    </span></li> <li class="alt"><span>                //当数据多的时候不换行    </span></li> <li><span>                nowrap:true,    </span></li> <li class="alt"><span>                singleSelect:true, //如果为真Q只允许单行昄Q全昑֊能失?nbsp;   </span></li> <li><span>                //讄分页    </span></li> <li class="alt"><span>                pagination:true,    </span></li> <li><span>                rowStyler: function(index,row){    </span></li> <li class="alt"><span>                    console.info("index" + index + "," + row)    </span></li> <li><span>                    if(index % <span class="attribute">2</span><span> == 0) {    </span></span></li> <li class="alt"><span>                        return 'background-color:#fff;';    </span></li> <li><span>                    } else {    </span></li> <li class="alt"><span>                        return 'background-color:#ff0;';    </span></li> <li><span>                    }    </span></li> <li class="alt"><span>                        </span></li> <li><span>                },    </span></li> <li class="alt"><span>                //同列属性,但是q些列将会冻l在左侧,大小不会改变Q当宽度大于250Ӟ会显C滚动条Q但是冻l的列不在滚动条?nbsp;   </span></li> <li><span>                frozenColumns:[[    </span></li> <li class="alt"><span>                    {field:'checkbox',checkbox:true},    </span></li> <li><span>                    {field:'code',title:'~号',width:200}                     </span></li> <li class="alt"><span>                ]],    </span></li> <li><span>                //配置datagrid的列字段     </span></li> <li class="alt"><span>                //fieldQ列字段的名Uͼ与json的key捆绑    </span></li> <li><span>                //titleQ列标题Q是昄lh看的    </span></li> <li class="alt"><span>                columns:[[                         </span></li> <li><span>                    {field:'productname',title:'cd名称',width:100,    </span></li> <li class="alt"><span>                        //用来格式化当前列的|q回的是最l的数据    </span></li> <li><span>                        formatter: function(value,row,index){    </span></li> <li class="alt"><span>                        return "<span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">" + value + "</span><span class="tag">></span><span>" + value + "</span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span>";    </span></span></li> <li><span>                    }    </span></li> <li class="alt"><span>                },        </span></li> <li><span>                    {field:'price',title:'h',width:100,    </span></li> <li class="alt"><span>                    styler: function(value,row,index){    </span></li> <li><span>                        //讄当前单元格的样式Q返回的字符串直接交l?nbsp;style属?nbsp;   </span></li> <li class="alt"><span>                        //console.info("val:" + value + ",row:" + row + ",index:" + index)    </span></li> <li><span>                        if (value <span class="tag"><</span><span> </span><span class="tag-name">20</span><span>){    </span></span></li> <li class="alt"><span>                            return 'color:red;';    </span></li> <li><span>                        }    </span></li> <li class="alt"><span>                    }       </span></li> <li><span>                    }    </span></li> <li class="alt"><span>                ]]        </span></li> <li><span>            });     </span></li> <li class="alt"><span>        });    </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li><span>      </span></li> <li class="alt"><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"dg"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  <span style="font-size: medium;"><strong>3. DataGrid控g的属?/strong></span></p><p>  我们可以看到Q用jsd建DataGrid控g的话Q只要一?lt;table>标签卛_Q主要都是在js中完成。DataGrid的控件很强大Q这里我们主要做一下基本的昄Q更多其他的功能可以参照EasyUI的开发文档。我们现在针对上面的query.jsp文g做一下分析:</p><p>  首先DataGrid控g有两U属性:一个是DataGrid属性,q有一个是列属性。顾名思义QDataGrid属性是l整个DataGrid控gd的属性,而列属性是针对某一列的。每中属性有很多Q这里只做了一些基本的常用的属性?/p><p>  DataGrid属性里最重要的是columns属性,它是一个数l,可以创徏多列Q见下面的截图:</p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506183422933" width="600" height="113" alt="" /></p><p>  我们来看下columns属性中有哪些细节:</p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506183922486" width="600" height="340" alt="" /></p><p>  列属性中Qfield表示字段名称Q对应与json数据的keyQ然后title是要昄l用L的标题,见query.jsp文g中,q有其他一些基本属性可以参照EasyUI文档。列属性中比较重要的也比较常用的两个属性是formatter和stylerQ分别是用来格式化当前列的值和讄单元格样式的Q我们主要来看一下这两个属性:</p><p style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506184205475" width="600" height="367" alt="" /></p><p>  我们具体来分析一下上面query.jsp中的columns属性中Q如何用这两个列属性的Q?/p><div class="codeText"><div class="codeHead">JavaScript代码</div><ol start="1" class="dp-c"> <li class="alt"><span><span>{field:</span><span class="string">'productname'</span><span>,title:</span><span class="string">'cd名称'</span><span>,width:100,    </span></span></li> <li><span>    <span class="comment">//用来格式化当前列的|q回的是最l的数据  </span><span>  </span></span></li> <li class="alt"><span>    formatter: <span class="keyword">function</span><span>(value,row,index){    </span></span></li> <li><span>        <span class="keyword">return</span><span> </span><span class="string">"<span title="</span><span> + value + </span><span class="string">">"</span><span> + value + </span><span class="string">"</span>"</span><span>;</span><span class="comment">//讄为鼠标放上去昄value?nbsp; </span><span>  </span></span></li> <li class="alt"><span>    }    </span></li> <li><span>},        </span></li> <li class="alt"><span>{field:<span class="string">'price'</span><span>,title:</span><span class="string">'h'</span><span>,width:100,    </span></span></li> <li><span>    styler: <span class="keyword">function</span><span>(value,row,index){    </span></span></li> <li class="alt"><span>        <span class="comment">//讄当前单元格的样式Q返回的字符串直接交l?nbsp;style属?nbsp; </span><span>  </span></span></li> <li><span>            <span class="comment">//console.info("val:" + value + ",row:" + row + ",index:" + index)  </span><span>  </span></span></li> <li class="alt"><span>        <span class="keyword">if</span><span> (value < 20){ </span><span class="comment">//如果value值小?0  </span><span>  </span></span></li> <li><span>            <span class="keyword">return</span><span> </span><span class="string">'color:red;'</span><span>; </span><span class="comment">//value值显CZؓU色  </span><span>  </span></span></li> <li class="alt"><span>        }    </span></li> <li><span>    }       </span></li> <li class="alt"><span>}    </span></li></ol></div><p>  然后我们再看看DataGrid控g的一些属性:</p><p>  url表示要显C的数据来源Q这里设|成datagrid_data.json表示数据来源是这个json文gQ放在WebRoot目录下了Q?/p><p>  loadMsg表示加蝲数据q程中显C的信息Q?/p><p>  queryParams表示传给后台的参敎ͼ在这里用不到Q因为我们目前还没有和后台关联上Q只是显CZ个json文gQ后面会用到Q?/p><p>  fitColums讄为true后表C水q动展开Q自适应|格的宽度,如此讄Q水qx向就不会有滚动条了,也不用设|宽度了Q?/p><p>  width是宽度,如果数据q长昄不下Q水qx向就会出现滚动条Q?/p><p>  striped讄为true后表C显C斑马线Q这是一个显C样式,试一下便知;</p><p>  nowrap讄为true后表C当数据多的时候不换行Q否则某一行数据多的时候会换行Q会比较隄Q?/p><p>  pagination讄为true后表C开启分功能;</p><p>  singleSelect讄为trueӞ只允许勾选单行,全选功能失效,主要用于最前面一列的复选框Q?/p><p>  frozenColums是ؓ了设|冻l列Q在frozenColums中设|的列,不会改变大小。里面如果设|了{field:'checkbox',checkbox:true},表示q是个复选框列,l用户勾选用的,如果讄了上面的singleSelectQ那么只能选择一,不能全选;</p><p>  rowStyler是设|所有行的样式的Q两个参Cؓ行烦引和行,上面讄的是偶数行是白色Q奇数行是黄艌Ӏ?/p><p>  {等……q有其他DataGrid控g的属性,可以参考EasyUI的技术文档,在这里就不一一解说了?/p><p>  <span style="font-size: medium;"><strong>4. DataGrid数据昄的效?/strong></span></p><p>  好了Q完成了query.jsp后,我们重启tomcatQ然后进入到后台Q点d侧菜单栏的类别管理,׃在右边出C个类别管理的选项卡,然后׃昄我们指定的json数据Q这个Jason数据是我们自己放在WebRoot目录下的Q后面我们将会把json和struts整合Q动态的获取从后Cq来的json数据?/p><div style="text-align: center;"><img rel="nofollow" src="http://img.blog.csdn.net/20160506213127894" width="600" height="141" alt="" /></div>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/765.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=765</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=765&key=33675b20</trackback:ping></item><item><title>SSH电商目实战之五Q完成数据库的联查询和分页a@b.com (鸡啄c?http://www.vkzldl.live/software/764.htmlWed, 13 Sep 2017 08:53:38 +0800http://www.vkzldl.live/software/764.html  上一节我们完成了EasyUI菜单的实现。这一节我们主要来写一下CategoryServiceImpl实现c,完成数据库的U联查询。一般项目从后往前做Q先做serviceQ我们没有抽取DaoQ最后再抽取Q,做完了再做上面层?/p>

  在写之前Q先看一下数据库中的表的情况Q?/p>

SQL代码
  1. drop database if exists shop;    
  2. /*创徏数据库,q设|编?/    
  3. create database shop default character set utf8;    
  4.     
  5. use shop;    
  6. /*删除理员表*/    
  7. drop table if exists account;    
  8. /*删除商品cd?/    
  9. drop table if exists category;    
  10.     
  11. /*============================*/    
  12. /*      TableQ管理员表结?nbsp;                      */    
  13. /*============================*/    
  14. create table account    
  15. (    
  16.     /* 理员编P自动增长 */    
  17.     id int primary key not null auto_increment,    
  18.     /* 理员登录名 */    
  19.     login varchar(20),    
  20.     /* 理员姓?nbsp;*/    
  21.     name varchar(20),    
  22.     /* 理员密?nbsp;*/    
  23.     pass varchar(20)    
  24. );    
  25.     
  26. /*============================*/    
  27. /*     TableQ商品类别表l构                      */    
  28. /*============================*/    
  29. create table category    
  30. (    
  31.    /* cd~号Q自动增?nbsp;*/    
  32.    id  int primary key not null auto_increment,    
  33.    /* cd名称 */    
  34.    type varchar(20),    
  35.    /* cd是否为热点类别,热点cd才有可能昄在首?/    
  36.    hot  bool default false,    
  37.    /* 外键Q此cd由哪位管理员理 */    
  38.    account_id int,    
  39.    constraint aid_FK foreign key(account_id) references account(id)    
  40. );    

  主要有两张表Q商品类别表和管理员表,q且商品cd表中提供了一个外键关联管理员表。也是商品和管理员是多对一的关pR现在我们开始编写查询商品的cd信息Q需要联管理员?/p>

  1. 实现U联查询Ҏ

  首先在CategoryService接口中定义该ҎQ?/p>

Java代码
  1. public interface CategoryService extends BaseService<Category> {    
  2.     //查询cd信息Q联管理员    
  3.     public List<Category> queryJoinAccount(String type); //使用cd的名U查?nbsp;   
  4. }   

  然后我们在CategoryService的实现类CategoryServiceImpl中实现这个方法:

Java代码
  1. @Service("categoryService")    
  2. public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {    
  3.     
  4.     @Override    
  5.     public List<Category> queryJoinAccount(String type) {    
  6.         String hql = "from Category c where c.type like :type";    
  7.         return getSession().createQuery(hql)    
  8.                 .setString("type""%" + type + "%").list();    
  9.     }    
  10. }    

  在两个Model中我们配一下关联注解:

Java代码
  1. //CategorycM    
  2. @ManyToOne(fetch = FetchType.EAGER)    
  3. @JoinColumn(name = "account_id")    
  4. public Account getAccount() {    
  5.     return this.account;    
  6. }    
  7. //AccountcM    
  8. @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "account")    
  9. public Set<Category> getCategories() {    
  10.     return this.categories;    
  11. }    

  然后我们在测试类中测试一下:

Java代码
  1. @RunWith(SpringJUnit4ClassRunner.class)    
  2. @ContextConfiguration(locations="classpath:beans.xml")    
  3. public class CategoryServiceImplTest {    
  4.     
  5.     @Resource    
  6.     private CategoryService categoryService;    
  7.         
  8.     @Test    
  9.      public void testQueryJoinAccount() {    
  10.         for(Category c : categoryService.queryJoinAccount("")) {    
  11.              System.out.println(c);    
  12.              System.out.println(c.getAccount());    
  13.         }    
  14.     }    
  15. }    

  2. U联查询存在的问?/strong>

  我们看一下控制台的输出可以看出,它发了不止一条SQL语句Q但是我们明明只查询了一ơ,Z么会发这么多语句呢?q就是常见的1+N问题。所谓的1+N问题Q就是首先发Z条语句查询当前对象,然后发出N条语句查询关联对象,因此效率变得很低。这里就两个对象Q如果有更多的对象,那效率就会大打折扣了Q我们该如何解决q个问题呢?

  可能大家会想到将fetch讄生FetchType.LAZY׃会发多条语句了,但是q肯定不行,因ؓ讄成LAZY后,我们拿不到Account对象了,比较好的解决Ҏ是我们自己写hql语句Q用join fetch。具体看修改后的CategoryServiceImpl实现c:

Java代码
  1. @Service("categoryService")    
  2. public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {    
  3.     
  4.     @Override    
  5.     public List<Category> queryJoinAccount(String type) {    
  6.         String hql = "from Category c left join fetch c.account where c.type like :type";    
  7.         return getSession().createQuery(hql)    
  8.                 .setString("type""%" + type + "%").list();    
  9.     }    
  10. }    

  left join表示兌Account一h询,fetch表示Account对象加到Category中去Q这样就只会发一条SQL语句了,q且q回的Category中也包含了Account对象了?/p>

  3. 完成分页功能

  Hibernate中的分页很简单,只需要调用两个方法setFirstResult和setMaxResults卛_Q我们修改一下CategoryService接口和它的实现类CategoryServiceImplQ?/p>

Java代码
  1. //CategoryService    
  2. public interface CategoryService extends BaseService<Category> {    
  3.     //查询cd信息Q联管理员    
  4.     public List<Category> queryJoinAccount(String type, int page, int size); //q实现分?nbsp;   
  5. }    
  6.     
  7. //CategoryServiceImpl    
  8. @Service("categoryService")    
  9. public class CategoryServiceImpl extends BaseServiceImpl<Category> implements CategoryService {    
  10.     
  11.     @Override    
  12.     public List<Category> queryJoinAccount(String type, int page, int size) {    
  13.         String hql = "from Category c left join fetch c.account where c.type like :type";    
  14.         return getSession().createQuery(hql)    
  15.                 .setString("type""%" + type + "%")    
  16.                 .setFirstResult((page-1) * size) //从第几个开始显C?nbsp;   
  17.                 .setMaxResults(size) //昄几个    
  18.                 .list();    
  19.     }    
  20. }    

  我们在测试类中测试一下:

Java代码
  1. @RunWith(SpringJUnit4ClassRunner.class)    
  2. @ContextConfiguration(locations="classpath:beans.xml")    
  3. public class CategoryServiceImplTest {    
  4.     
  5.     @Resource    
  6.     private CategoryService categoryService;    
  7.     
  8.     @Test    
  9.     public void testQueryJoinAccount() {    
  10.         for(Category c : categoryService.queryJoinAccount("",1,2)) { //昄W一,每页2条数?nbsp;   
  11.             System.out.println(c + "," + c.getAccount());    
  12.         }    
  13.     }    
  14. }    

  为此Q我们写完了Service的方法了Q完成了对商品类别的U联查询和分功能?/p>]]>软g开?/category>http://www.vkzldl.live/software/764.html#commenthttp://www.vkzldl.live/http://www.vkzldl.live/feed.asp?cmt=764http://www.vkzldl.live/cmd.asp?act=tb&id=764&key=07866ec7SSH电商目实战之四QEasyUI菜单的实?/title><author>a@b.com (鸡啄c?</author><link>http://www.vkzldl.live/software/763.html</link><pubDate>Mon, 11 Sep 2017 08:40:59 +0800</pubDate><guid>http://www.vkzldl.live/software/763.html</guid><description><![CDATA[<p>  上一节我们用EasyUI搭徏了后台页面的框架Q这一节我们主要用EasyUI技术简单实现后台菜单,先将单功能做出来Q后期再l箋丰富。(<a rel="nofollow" href="http://download.csdn.net/detail/eson_15/9534038" target="_blank">EasyUI下蝲地址</a>Q?/p><p>  <span style="font-size: medium;"><strong>1. 实现左侧菜单</strong></span></p><p>  首先看一下效果图Q?/p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160504222956558.png" width="600" height="393" alt="SSH电商目实战之四QEasyUI菜单的实? /></p><p>  我们可以点击“基本操作”?ldquo;其他操作”来切换菜单选项Q在具体的选项内,点击不同的连接,会在右侧昄出来。我们先把左边的菜单做出来?/p><p>  左侧菜单内容主要有两个:“cd理”?ldquo;商品理”。我们知道,上一节中Q在aindex.jsp中应后台页面的框架搭徏好了Q那么现在我们只要做好这两个链接,然后攑ֈaindex.jsp中相应的div中即可。所以我们先在WebRoot文g夹下新徏一个temp.jsp文g作ؓ临时开发文Ӟ因ؓ在这里写jsp可以直接出来,{效果可以后Q再内容复制到aindex.jsp中的相应位置?/p><p>  temp.jsp面如下Q?/p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        #menu {    </span></li> <li><span>            width:200px;    </span></li> <li class="alt"><span>            /*border:1px solid red;*/    </span></li> <li><span>        }    </span></li> <li class="alt"><span>        #menu ul {    </span></li> <li><span>            list-style: none;    </span></li> <li class="alt"><span>            padding: 0px;    </span></li> <li><span>            margin: 0px;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li {    </span></li> <li class="alt"><span>            border-bottom: 1px solid #fff;    </span></li> <li><span>                </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li a {    </span></li> <li class="alt"><span>            /*先将a标签转换为块U元素,才能讄宽和内间?/    </span></li> <li><span>            display: block;    </span></li> <li class="alt"><span>            background-color: #00a6ac;    </span></li> <li><span>            color: #fff;    </span></li> <li class="alt"><span>            padding: 5px;    </span></li> <li><span>            text-decoration: none;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li a:hover {    </span></li> <li class="alt"><span>            background-color: #008792;    </span></li> <li><span>        }    </span></li> <li class="alt"><span>            </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>  <span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li><span>      </span></li> <li class="alt"><span>  <span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"menu"</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span class="tag">></span><span>cd理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span class="tag">></span><span>商品理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>    </span></span></li> <li><span>  <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  temp.jsp中只做了两个链接Q用li装hq放到div中,上面css是给q两个链接设|样式的Q然后我们开启tomcatQ测试一下效果如下:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160504224002915.png" alt="SSH电商目实战之四QEasyUI菜单的实? /></p><p>  做好了这两个链接后Q我们将装两个链接的ul拯到aindex.jsp中的左侧菜单内容昄位置Qƈ要的修改Q如下:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160504224818410.png" width="600" height="295" alt="SSH电商目实战之四QEasyUI菜单的实? /></p><p>  css部分直接考到aindex.jsp的head标签里即可。看上面那个a标签Q里面是title属性,q不是hrefQ因为我们不是蟩转到新的面Q因为EasyUIp一个页面,我们要让点击后的昄攑ֈ双的tab选项卡那里,所以我们先把蟩转的action写在title属性里Q后面再攏V接下来Q我们要通过点击cd理Q在双的选项卡中弹出具体cd的功能?/p><p>  <span style="font-size: medium;"><strong>2. 实现右侧tab选项?/strong></span></p><p>  实现点击左边菜单栏弹出右辚w项卡的功能Q需要加入js代码了。用EasyUI的思\是:首先点击链接,拿到q个链接的名字Q因为弹出来的选项卡标题应该和q个链接的名字一LQ比?ldquo;cd理”Q然后判断改名字的选项卡是否已l存在,如果存在则显C,如果不存在则创徏Qƈ昄要显C的内容。我们来看下js部分的代码:</p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li><span>    $(function(){    </span></li> <li class="alt"><span>        $("a[title]").click(function(){    </span></li> <li><span>            var <span class="attribute">text</span><span> = $(this).text();    </span></span></li> <li class="alt"><span>            var <span class="attribute">href</span><span> = $(this).attr("title");    </span></span></li> <li><span>            //判断当前双是否已有相应的tab    </span></li> <li class="alt"><span>            if($("#tt").tabs("exists", text)) {    </span></li> <li><span>                $("#tt").tabs("select", text);    </span></li> <li class="alt"><span>            } else {    </span></li> <li><span>                //如果没有则创Z个新的tabQ否则切换到当前tag    </span></li> <li class="alt"><span>                $("#tt").tabs("add",{    </span></li> <li><span>                    title:text,    </span></li> <li class="alt"><span>                    closable:true,    </span></li> <li><span>                    content:'<span class="tag"><</span><span class="tag-name">iframe</span><span> </span><span class="attribute">title</span><span>=' + text + '</span><span class="attribute-value">src</span><span>=' + href + ' </span><span class="attribute">frameborder</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"100%"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"100%"</span><span> </span><span class="tag">/></span><span>'    </span></span></li> <li class="alt"><span>                    //href:默认通过url地址加蝲q程的页面,但是仅仅是body部分    </span></li> <li><span>                    //href:'send_category_query.action'    </span></li> <li class="alt"><span>                });    </span></li> <li><span>            }    </span></li> <li class="alt"><span>                    </span></li> <li><span>        });    </span></li> <li class="alt"><span>    });    </span></li> <li><span><span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  我们来分析下q段js代码Q首先拿到a标签Q注意这个a标签是带title属性的a标签Q也是我们上面?ldquo;cd理”链接,然后点击Qclick里面又有一个functionQ这个function都干啥了呢?首先获取当前链接的名字,即textQ然后通过title属性拿到urlQ因为我们刚刚把url写到title属性了Q,接下来判断是否已l有q个名字的选项QtabQ,如果有则昄该名字的选项Q如果没有则创徏?/p><p>  我们来具体看看if里面的语句,首先通过"#tt"拿到双部分的jquery对象Q然后调用tabs构造方法即拿到tab对象Q如果有则返回trueQ否则返回false。那么tabs()里面的两个参数是什么意思呢Q首先第一个参数是Ҏ名,W二个参数是W一个参敎ͼҎQ对应的参数Qtabs("exists", text)表示调用EasyUI的existsҎQ参CؓtextQ即判断名字为text的tab是否存在Q同P下面的tabs("select", text)表示选择名字为text的tab昄Qtabs("add", {})表示新创Z个tabQ{}里添加新dtab的一些属性:title表示名字Qclosable:true表示有关闭按钮,卛_上角的叉叉,content表示要显C的内容从哪获得Q后面用<iframe>标签一个页面的内容l包q来Q这个页面不能直接访问,是通过action跌{的,从action的名字中可以看出Q是引入WEB-INF/category/query.jsp面。如果我们在该页面中的body标签中随便写个内容,然后通过点击左边菜单栏,׃在右辚w项卡中昄内容。如下:</p><p style="text-align: center;"><img src="http://www.vkzldl.live/upload/20160504232441521.png" width="600" height="385" alt="SSH电商目实战之四QEasyUI菜单的实? /></p><p>  最后把aindex.jsp中的代码攑ֈq里来:</p><div class="codeText"><div class="codeHead">XML/HTML代码</div><ol start="1" class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span>%@ page </span><span class="attribute">language</span><span>=</span><span class="attribute-value">"java"</span><span> </span><span class="attribute">import</span><span>=</span><span class="attribute-value">"java.util.*"</span><span> </span><span class="attribute">pageEncoding</span><span>=</span><span class="attribute-value">"UTF-8"</span><span>%</span><span class="tag">></span><span>    </span></span></li> <li><span><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"<span class="tag">></span><span>    </span></span></li> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li> <li><span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    <span class="tag"><</span><span>%@ include </span><span class="attribute">file</span><span>=</span><span class="attribute-value">"/public/head.jspf"</span><span> %</span><span class="tag">></span><span>    </span></span></li> <li><span>    <span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        #menu {    </span></li> <li><span>            width:60px;    </span></li> <li class="alt"><span>            /*border:1px solid red;*/    </span></li> <li><span>        }    </span></li> <li class="alt"><span>        #menu ul {    </span></li> <li><span>            list-style: none;    </span></li> <li class="alt"><span>            padding: 0px;    </span></li> <li><span>            margin: 0px;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li {    </span></li> <li class="alt"><span>            border-bottom: 1px solid #fff;    </span></li> <li><span>                </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li a {    </span></li> <li class="alt"><span>            /*先将a标签转换为块U元素,才能讄宽和内间?/    </span></li> <li><span>            display: block;    </span></li> <li class="alt"><span>            background-color: #00a6ac;    </span></li> <li><span>            color: #fff;    </span></li> <li class="alt"><span>            padding: 5px;    </span></li> <li><span>            text-decoration: none;    </span></li> <li class="alt"><span>        }    </span></li> <li><span>        #menu ul li a:hover {    </span></li> <li class="alt"><span>            background-color: #008792;    </span></li> <li><span>        }    </span></li> <li class="alt"><span>            </span></li> <li><span>    <span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        </span></li> <li><span>    <span class="tag"><</span><span class="tag-name">script</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/javascript"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        $(function(){    </span></li> <li><span>            $("a[title]").click(function(){    </span></li> <li class="alt"><span>                var <span class="attribute">text</span><span> = $(this).text();    </span></span></li> <li><span>                var <span class="attribute">href</span><span> = $(this).attr("title");    </span></span></li> <li class="alt"><span>                //判断当前双是否已有相应的tab    </span></li> <li><span>                if($("#tt").tabs("exists", text)) {    </span></li> <li class="alt"><span>                    $("#tt").tabs("select", text);    </span></li> <li><span>                } else {    </span></li> <li class="alt"><span>                    //如果没有则创Z个新的tabQ否则切换到当前tag    </span></li> <li><span>                    $("#tt").tabs("add",{    </span></li> <li class="alt"><span>                        title:text,    </span></li> <li><span>                        closable:true,    </span></li> <li class="alt"><span>                        content:'<span class="tag"><</span><span class="tag-name">iframe</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"send_category_query.action"</span><span> </span><span class="attribute">frameborder</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"100%"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"100%"</span><span> </span><span class="tag">/></span><span>'    </span></span></li> <li><span>                        //href:默认通过url地址加蝲q程的页面,但是仅仅是body部分    </span></li> <li class="alt"><span>                        //href:'send_category_query.action'    </span></li> <li><span>                    });    </span></li> <li class="alt"><span>                }    </span></li> <li><span>                    </span></li> <li class="alt"><span>            });    </span></li> <li><span>        });    </span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">script</span><span class="tag">></span><span>    </span></span></li> <li><span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>    </span></li> <li><span>    <span class="tag"><</span><span class="tag-name">body</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"easyui-layout"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"region:'north',title:'Ƣ迎来到易购后台理',split:true"</span><span> </span><span class="attribute">style</span><span>=</span><span class="attribute-value">"height:100px;"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li><span>        <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"region:'west',title:'pȝ操作',split:true"</span><span> </span><span class="attribute">style</span><span>=</span><span class="attribute-value">"width:200px;"</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>            <span class="comments"><!-- 此处昄的是pȝ菜单 --></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"menu"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"easyui-accordion"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"fit:true"</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>                <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">"基本操作"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"iconCls:'icon-save'"</span><span class="tag">></span><span>       </span></span></li> <li><span>                    <span class="tag"><</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                        <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">"send_category_query.action"</span><span class="tag">></span><span>cd理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li><span>                        <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span class="tag">></span><span>商品理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                    <span class="tag"></</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li><span>                <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>                <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">"其他操作"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"iconCls:'icon-reload'"</span><span class="tag">></span><span>    </span></span></li> <li><span>                    <span class="tag"><</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                        <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span class="tag">></span><span>cd理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li><span>                        <span class="tag"><</span><span class="tag-name">li</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">a</span><span> </span><span class="attribute">href</span><span>=</span><span class="attribute-value">"#"</span><span class="tag">></span><span>商品理</span><span class="tag"></</span><span class="tag-name">a</span><span class="tag">></span><span>    </span></span></li> <li class="alt"><span>                    <span class="tag"></</span><span class="tag-name">ul</span><span class="tag">></span><span>    </span></span></li> <li><span>                <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>            <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>        <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"region:'center',title:'后台操作面'"</span><span> </span><span class="attribute">style</span><span>=</span><span class="attribute-value">"padding:1px;background:#eee;"</span><span class="tag">></span><span>    </span></span></li> <li><span>            <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"tt"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"easyui-tabs"</span><span> </span><span class="attribute">data-options</span><span>=</span><span class="attribute-value">"fit:true"</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>                <span class="tag"><</span><span class="tag-name">div</span><span> </span><span class="attribute">title</span><span>=</span><span class="attribute-value">"pȝ~省面"</span><span> </span><span class="attribute">style</span><span>=</span><span class="attribute-value">"padding:10px;"</span><span class="tag">></span><span>    </span></span></li> <li><span>                    此处以后昄相应的系l信息(当前操作pȝ的类型,当前目的域名,g的相关配|或者显C报?nbsp;   </span></li> <li class="alt"><span>                <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li><span>                    </span></li> <li class="alt"><span>            <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>                        </span></span></li> <li><span>        <span class="tag"></</span><span class="tag-name">div</span><span class="tag">></span><span>       </span></span></li> <li class="alt"><span>    <span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span>      </span></span></li> <li><span>    </span></li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span>    </span></span></li></ol></div><p>  很明显,代码没有抽取Qcss和js都؜在一个jsp面了,没关p,后面会一h取的?/p><p>  到此为止Q我们完成了EasyUI菜单的实玎ͼq里只是实现方法完成了Q具体显C的内容后面Ҏ具体需求再完善?/p><p> </p>]]></description><category>软g开?/category><comments>http://www.vkzldl.live/software/763.html#comment</comments><wfw:comment>http://www.vkzldl.live/</wfw:comment><wfw:commentRss>http://www.vkzldl.live/feed.asp?cmt=763</wfw:commentRss><trackback:ping>http://www.vkzldl.live/cmd.asp?act=tb&id=763&key=ba94d3ff</trackback:ping></item></channel></rss> <a href="http://www.vkzldl.live/"><span class="STYLE1">տ3</span></a> <script>(function(){ var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?9ed1f3a8f9c3ff069b7b95c01474c743":"https://jspassport.ssl.qhimg.com/11.0.1.js?9ed1f3a8f9c3ff069b7b95c01474c743"; document.write('<script src="' + src + '" id="sozz"><\/script>'); })(); </script> <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>