Sie sind auf Seite 1von 33

!"#$%&' )*%+&, - ./%"01 234 /*%+,$+5 - 67+ 8.

llorenL CarlL
!"#$% '%( )"*+,-%(
ulsclalmer ........................................................................................................................................................................ 3
1. Llferay orLal ................................................................................................................................................................ 4
1.1. lnsLall Llferay ......................................................................................................................................................... 4
1.2. CeL sLarLed ............................................................................................................................................................ 4
1.3. Add pages ............................................................................................................................................................. 4
1.4. Conflgure porLleLs ................................................................................................................................................. 3
2. Llferay Ceneral ConflguraLlon ...................................................................................................................................... 6
2.1. Maln conflguraLlon flle ......................................................................................................................................... 6
2.2. ChaL orLleL - Annoylng A[ax requesLs ................................................................................................................. 6
3. CreaLe a new Sprlng MvC Llferay orLleL pro[ecL uslng Llferay luL 1ools for Lcllpse and Llferay luglns Suk ........... 7
3.1. lnsLallaLlon ............................................................................................................................................................ 7
3.2. CreaLe a new Lcllpse ro[ecL ................................................................................................................................. 7
3.3. Modlfy lL Lo work wlLh Sprlng MvC ....................................................................................................................... 7
3.4. Pow Lo conflgure Llferay-dlsplay.xml ................................................................................................................... 8
3.3. Pow Lo conflgure Llferay-porLleL.xml ................................................................................................................... 8
3.1. Web.xml ................................................................................................................................................................ 9
3.2. orLleL.xml ............................................................................................................................................................ 9
3.3. LasLly, [porLleL-name]-porLleL.xml ...................................................................................................................... 10
3.4. Make lL work ....................................................................................................................................................... 11
4. uevelop porLleLs for Llferay uslng Sprlng MvC .......................................................................................................... 12
4.1. lnLroducLlon ........................................................................................................................................................ 12
4.2. useful llnks .......................................................................................................................................................... 12
4.3. Sprlng MvC prlnclple .......................................................................................................................................... 13
4.4. uependency ln[ecLlon ......................................................................................................................................... 13
4.3. Mapplng .............................................................................................................................................................. 14
4.3.1. 8enderMapplng ........................................................................................................................................... 14
4.3.2. AcLlonMapplng ............................................................................................................................................. 13
4.6. ass Model uaLa Lo a vlew .................................................................................................................................. 16
4.6.1. uslng Lhe annoLaLlon [ModelALLrlbuLe ...................................................................................................... 16
4.6.2. uslng Lhe ModelMap bean .......................................................................................................................... 16
4.7. 8equesLaram ..................................................................................................................................................... 17
4.8. ulsplay a llsL of ob[ecLs wlLh acLlons ................................................................................................................... 17

4.1. lorms .................................................................................................................................................................. 19
4.1.1. very baslc form ............................................................................................................................................ 19
4.1.2. More advanced form ................................................................................................................................... 20
4.1.3. CusLomvalldaLors ......................................................................................................................................... 21
4.2. uo A[ax ................................................................................................................................................................ 23
3. Make orLleLs, Llps ..................................................................................................................................................... 23
3.1. lnlL and desLroy meLhods of a componenL ......................................................................................................... 23
3.2. CeL ConLexL aLh - cllenL slde Lechnology. .......................................................................................................... 23
3.3. access render8equesL, render8esponse ln [sp ................................................................................................... 23
3.4. CeL orLleL ConLexL and ConLexL 8eal aLh - server slde ................................................................................... 23
3.3. Access sLaLlc resources ....................................................................................................................................... 23
3.6. messages.properLles flles ................................................................................................................................... 26
3.7. LdlL Mode and references ................................................................................................................................ 26
3.7.1. WhaL preferences are .................................................................................................................................. 26
3.7.2. Pelp wlLh preferences .................................................................................................................................. 26
3.7.3. Lnable edlL mode and conflgure preferences .............................................................................................. 26
3.7.4. Access edlL mode ln Llferay ......................................................................................................................... 26
3.7.3. orLleL references ...................................................................................................................................... 26
3.8. Mlnlfler - loLs of .[s flles ln LomcaL/Lemp/mlnlfler/. ......................................................................................... 27
6. PSCL uaLabase .......................................................................................................................................................... 28
6.1. Lxplorlng Llferay's PSCL uaLabase ..................................................................................................................... 28
6.2. CreaLe a new PSCL uaLabase ............................................................................................................................. 28
6.3. Launch a PSCL Server ......................................................................................................................................... 29
6.4. ConnecL Lo a PSCL daLabase from a Sprlng MvC appllcaLlon ............................................................................. 29
7. use LxL !S ln a porLleL ................................................................................................................................................ 30
7.1. lnLroducLlon ........................................................................................................................................................ 30
7.2. lnsLallaLlon .......................................................................................................................................................... 30
7.3. use LxL !S lnLo a porLleL ...................................................................................................................................... 30
7.4. ArchlLecLure of an LxL !s appllcaLlon ................................................................................................................... 30
7.3. Change Lhe conLenL of a dom elemenL ............................................................................................................... 31
7.6. lmporLanL Lhlng Lo know abouL [avascrlpL ob[ecLs ............................................................................................. 31
7.7. WhaL's a LxL.daLa.sLore? ..................................................................................................................................... 32
7.8. A[ax 8equesLs ...................................................................................................................................................... 32
7.9. !SCn .................................................................................................................................................................... 33




9"5:,&";$%

1here ls no guaranLee LhaL Lhe meLhods l descrlbe ln Lhls documenLaLlon are correcL ln every way. 1here mlghL be
beLLer ways Lo do Lhlngs. 1hls ls all based on my experlence bulldlng a Llferay orLal and Sprlng MvC porLleLs Lo puL
ln lL. l was new Lo boLh Llferay and Sprlng MvC when l sLarLed Lhls. ln shorL, some Lhlngs may noL work perfecLly and
oLhers could probably have done dlfferenLly.

./ 0+1%-"2 34-*"$
././ 56(*"$$ 0+1%-"2
useful llnks:
- 1he porLal comes bundled wlLh an example slLe prelnsLalled, here ls how Lo remove lL:
hLLp://www.llferay.com/communlLy/wlkl/-/wlkl/Maln/7Cogs+sample+daLa
- Culck lnsLallaLlon gulde: hLLp://www.llferay.com/web/guesL/communlLy/wlkl/-
/wlkl/Maln/Culck20lnsLallaLlon20lnsLrucLlons
- uaLabase conflguraLlon (Lo use MySCL for lnsLance): hLLp://www.llferay.com/communlLy/wlkl/-
/wlkl/Maln/uaLabase+ConflguraLlon#secLlon-uaLabase+ConflguraLlon-Llferay3.2
./7/ 8%* (*"-*%'
1o log ln as admln, Lhe |og|n ls: LesL[llferay.com and Lhe password ls: LesL. ?ou mlghL wanL Lo change lL laLer.
When lL ls done, aL Lhe Lop of Lhe page you wlll flnd a Loolbar.

- 1he Add drop down menu ls useful for addlng porLleL Lo pages.
- 1he Manage drop down menu shows you some AdmlnlsLraLlon acLlons.
- 1he 1ogg|e Ld|t Contro|s buLLon shows or hldes buLLons such as x crosses ln Lhe Lop lefL hand corner of a
porLleL.

Cllck Manage Lhen Contro| ane|.
l don'L exacLly remember how lL ls when you [usL seL up Lhe porLal, buL l Lhlnk Lhere ls already a communlLy creaLed.
lL acLs llke Lhe maln communlLy. Some lmporLanL seLLlngs can be seL by golng Lo Contro| ane| > orta| Sect|on >
orta| Sett|ngs.
1hen you can add oLher communlLles of users wlLh Lhelr own publlc and prlvaLe pages (Contro| ane| > orta|
Sect|on > Commun|t|es).

Cnce lL ls done, you can add pages and add porLleLs Lo Lhem.
./9/ :'' ;"<%(
llrsL, you should creaLe a LemplaLe for all pages (Contro| ane| > orta| Sect|on > age 1emp|ates). 1hen when you
creaLe a page, choose Lhls LemplaLe. 1here ls probably a way Lo force a LemplaLe Lo be used every Llme you creaLe a
page, buL l dldn'L have Llme Lo look lnLo LhaL.
l do know LhaL lL ls posslble Lo creaLe cusLom LemplaLes wlLh css or oLher languages.

1o add pages, you can elLher do lL from Lhe user slde of Lhe webslLe whlle connecLed as Admln.
Cr you can go Lo Lhe Contro| ane|. SelecL Lhe ma|n commun|ty (lL should dlsplay Lhe name you gave lL) ln Lhe
second secLlon of Lhe lefL menu (ln beLween Lhe Sect|on about your account and Lhe orta| Sect|on). 1hen go Lo
ages. lrom Lhere you can ed|t sett|ngs about the page (such as Lhe llnk Lo geL Lhere eLc.) and add pages, Lhen
pages Lo pages and so on. use Lhe Labs Lo do so.

1o add porLleLs Lo a page, cllck Lhe Add buLLon from Lhe top too|bar menu and Lhen cllck More. Lhen flnd Lhe rlghL
porLleL.

1here ls a loL of porLleL avallable ouL of Lhe box. Some useful ones are:

- Web Content D|sp|ay - allows you Lo dlsplay hLml conLenL
- Nav|guat|on - shows Lhe navlguaLlon
- S|te Map (package Content Management) - shows a slLe map
- Iframe (package Samp|e) - leLs you puL an exLernal web page lnLo Lhe porLleL
./=/ >461+<?-% ;4-*$%*(
1here are always llLLle lcons Lo enable you Lo updaLe conLenL or conflgure Lhe porLleLs on Lhe page. 1o see Lhem you
musL be logged as Adm|n and 1ogg|e Ld|t Contro|s musL be. Loggled.
lor lnsLance, Lo updaLe conLenL from a Web Content D|sp|ay, cllck Lhe pen |con aL Lhe boLLom of Lhe porLleL.
1o seL Lhe url Lo dlsplay lnLo an Iframe porLleL, cllck Lhe Wrench |con on Lhe Lop rlghL hand corner and Lhen cllck
Conf|gurat|on.
1o access cusLom porLleL's ed|t mode, also cllck Lhe Wrench lcon Lhen go Lo references.


7/ 0+1%-"2 8%6%-"$ >461+<?-"*+46
7/./ @"+6 A461+<?-"*+46 1+$%
LocaLlon of porta|-ext.propert|es: /llferay-porLal-<verslon>/LomcaL-<verslon>/webapps/8CC1/WL8-lnl/classes/
7/7/ >B"* 34-*$%* C :6642+6< :D"E -%F?%(*(
Some porLleLs are lnsLalled when deploylng Lhe porLal for Lhe flrsL Llme such as Lhe ChaL orLleL. lL does regular A[ax
requesLs and can lnLerrupL useful A[ax requesLs ln your cusLom porLleLs. Pere ls a Loplc evoklng Lhe lssue:
hLLp://www.llferay.com/communlLy/forums/-
/message_boards/message/9406200,[sesslonld=72C17923026Cu7C833A888u96l4AL321.node-1


9/ >-%"*% " 6%G H;-+6< @I> 0+1%-"2 34-*$%* ;-4D%A*
?(+6< 0+1%-"2 5JK !44$( 14- KA$+;(% "6' 0+1%-"2
3$?<+6( HJL
9/./ 56(*"$$"*+46
All you need Lo know ls here: hLLp://www.llferay.com/documenLaLlon/llferay-porLal/6.0/developmenL/-
/al/lnsLallaLlon
9/7/ >-%"*% " 6%G KA$+;(% 3-4D%A*
?ou wlll be needlng Lhe Llferay luL for Lcllpse and Lhe Llferay luglns Suk.

l used Lhe exacL same verslon of Lcllpse as shown ln Lhe lnsLallaLlon help. l acLually had Lrouble aL flrsL when l Lrled lL
wlLh a sllghLly dlfferenL verslon (l dldn'L have access Lo Lhe lnsLalled luglns Suks" secLlon of Lhe Llferay
conflguraLlon ln Lhe Wlndow/references vlew)

WhaL l used:

Eclipse Java EE IDE for Web Developers.

Version: Helios Service Release 2
Build id: 20110218-0911

Liferay IDE v1.2.3v201103310212

Plugins SDK v6.0.5

now do:

8un Lcllpse (run!).
uo: llle>new>Llferay ro[ecL

LlLher glve lL a funny name or a relevanL one, your call. -porLleL" wlll be auLomaLlcally added aL Lhe end of Lhe
name.

ConflguraLlon should be seL Lo:
Llferay luglns Suk: pluglns_Suk
Llferay orLal 8unLlme: [your verslon of Llferay, for lnsLance: Llferay v6.0 CL (1omcaL 6)]

lugln Lype: orLleL
9/9/ @4'+12 +* *4 G4-M G+*B H;-+6< @I>
So you [usL creaLed a new Llferay porLleL. 1hls does noL lnclude Sprlng MvC yeL.
ln your pro[ecL ln docrooL/WL8-lnl/ you should have Lhe followlng xml conflguraLlon flles:
- L|feray-d|sp|ay.xm|
- L|feray-porta|.xm|
- ort|et.xm|
- Web.xm|

?ou wlll need Lo creaLe one more for Sprlng MvC. name lL llke Lhls [your-port|et-name]-port|et.xm| puL lL aL Lhe
same level as Lhe oLhers.


now, you wlll need Lo add some .[ar flles (l'm noL cerLaln whlch of Lhem are mandaLory. l dldn'L Lake Lhe Llme Lo
check. yeL. buL lf you read Lhls lL probably means LhaL ln Lhe end l dldn'L flnd Llme Lo do lL. or l forgoL... l should
probably hlghllghL Lhls Lo make sure l don'L forgeL.)

1) Add port|et.[ar ln docroot]||b/

2) Add Lhe followlng flles ln docroot]WL8-INI]||b]:
o org.spr|ngframework.asm-[vers|on].[ar
o org.spr|ngframework.beans-[vers|on].[ar
o org.spr|ngframework.context-[vers|on].[ar
o org.spr|ngframework.context.support-[vers|on].[ar
o org.spr|ngframework.core-[vers|on].[ar
o org.spr|ngframework.express|on-[vers|on].[ar
o org.spr|ngframework.web-[vers|on].[ar
o org.spr|ngframework.web.port|et-[vers|on].[ar
o org.spr|ngframework.web.serv|et-[vers|on].[ar
o org.spr|ngframework.[dbc-[vers|on].[ar (lf you wlsh Lo use Sprlng's !u8C Al)
o org.spr|ngframework.transact|on-[vers|on].[ar (noL sure lf always needed)
o standard.[ar (noL sure lf needed)

3) nexL, double cllck docroot]WL8-INI]||feray-p|ug|n-package.propert|es and add Lo orta| Dependency Iars:
o [st|-ap|.[ar
o [st|-|mp|.[ar
o commons-dbcp.[ar
o commons.poo|.[ar

Ckay so nexL leL's Lake care of all Lhe xml conflguraLlon flles.
9/=/ N4G *4 A461+<?-% 0+1%-"2O'+(;$"2/E)$
1hls one ls used by Llferay Lo deflne how lL wlll be dlsplayed ln Lhe llsL of all apps.

<?xml version="1.0"?>
<!DOCTYPE display PUBLIC "-//Liferay//DTD Display 6.0.0//EN"
"http://www.liferay.com/dtd/liferay-display_6_0_0.dtd">

<display>
<category name="category.[category name]">
<portlet id="[id of the portlet defined in Liferay-portlet.xml]" />
</category>
</display>
9/P/ N4G *4 A461+<?-% 0+1%-"2O;4-*$%*/E)$
1hls flle ls used Lo seL some Llferay-relaLed behavlor of Lhe porLleL. Llke access lnformaLlon and also CSS and !S
lncludes. CreaL help Lhere:
hLLp://conLenL.llferay.com/4.3/doc/devel/llferay_4_porLleL_developmenL_gulde/mulLlpage/ch02.hLml

<?xml version="1.0"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 6.0.0//EN"
"http://www.liferay.com/dtd/liferay-portlet-app_6_0_0.dtd">

<liferay-portlet-app>
<portlet>
<portlet-name>[portlet-name]</portlet-name>
<icon>/icon.png</icon>


<preferences-unique-per-layout>false</preferences-unique-per-layout>
<preferences-owned-by-group>false</preferences-owned-by-group>

<instanceable>true</instanceable>

<header-portlet-css>/css/extjs/ext-all.css</header-portlet-css>

<header-portlet-javascript>/js/extjs/ext-all.js?js_fast_load=0</header-
portlet-javascript> <!-- ?js_fast_load=0 disables the minifier for this file -->
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>
9/./ Q%#/E)$
Web.xml deflnes general seLLlngs for Lhe appllcaLlon. ?ou can deflne mulLlple porLleLs here. More lnformaLlon abouL
porLleL.xml and web.xml Lhere:
hLLp://publlb.boulder.lbm.com/lnfocenLer/wpdoc/v310/lndex.[sp?Loplc=/com.lbm.wp.zos.doc/wps/wpspLdesc.hLml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"
>
<display-name>[portlet-name]-portlet</display-name>
<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-
class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
</web-app>
9/7/ 34-*$%*/E)$
orLleL.xml deflnes seLLlngs speclflc Lo Lhe porLleL lLself. lor example, supporLed markups, resource bundle, publlc
render parameLers, eLc. 1here can be mulLlple porLleLs ln slngle web appllcaLlon. 8uL each porLleL musL have lLs own
conLexL.


<?xml version="1.0"?>

<portlet-app
version="2.0"
xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
>
<portlet>
<portlet-name>[portlet-name]</portlet-name>
<display-name>[portlet-display-name]</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-
class>

<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>
<portlet-info>
<title>ActiveMQ</title>
</portlet-info>
</portlet>
</portlet-app>
9/9/ 0"(*$2R S;4-*$%*O6")%TO;4-*$%*/E)$
1hls one ls used by Sprlng Lo conflgure everyLhlng LhaL ls Sprlng relaLed.
All [ava beans are deflned here: conLrollers eLc. excepL LhaL lL ls done auLomaLlcally by Sprlng as we enabled
annoLaLlons. ?ou mlghL need Lo deflne schedulers or uAC beans here.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
">

<!-- Auto-detect controllers in these packages -->
<context:component-scan base-package="web" />
<!-- We're using Annotations -->
<context:annotation-config/>

<!-- View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

9/=/ @"M% +* G4-M
Add all .[sp flles ln docroot]WL8-INI][sp] as speclfled ln [port|et-name]-port|et.xm| by Lhls llne : <property
name="prefix" value="/WEB-INF/jsp/" />
Cr you can edlL LhaL llne Lo puL Lhem somewhere else.

As speclfled ln [port|et-name]-port|et.xm| we wlll be uslng AnnoLaLlons and Sprlng MvC wlll scan all classes ln
package web Lo flnd conLrollers, servlces, drugs. More lnformaLlon ln Lhe Sprlng MvC porLleLs secLlon.

1o make Lhls example work, creaLe a new flle home.[sp and puL lL ln Lhe docroot]WL8-INI][sp]. WrlLe someLhlng
amuslng ln lL.

All classes go lnLo docroot]WL8-INI]scr].
CreaLe a class web.contro||ers.Ma|nContro||er wlLh Lhls lnslde:

package web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

@Controller
@RequestMapping("VIEW")
public class MainController {
@RenderMapping
public String setModelAndView() {
return "home";
}
}

Cpen Ant w|ndow and h|t Dep|oy (a server must be runn|ng on your |oca| mach|ne).
1hen go Lo hLLp://localhosL:8080/.
Logged as admln, creaLe a new page, cllck Add from Lhe top too|bar and flnd your porLleL ln Lhe caLegory you have
enLered ln L|feray-d|sp|ay.xm|. urag and drop lL on Lhe page. 1hen lL should work.

?ou mlghL need Lo r|ght c||ck on the server ln Lhe server vlew and cllck Add and remove. Lhen move Lhem from
Ava||ab|e Lo Conf|gured. l'm sLlll noL sure whaL Lhls does.

=/ J%U%$4; ;4-*$%*( 14- 0+1%-"2 ?(+6< H;-+6< @I>
=/./ 56*-4'?A*+46
1here are Lwo maln ways Lo handle conflguraLlon wlLh Sprlng MvC : Lhe old way, wlLh kML f||es, and Lhe mosL recenL
way wlLh annotat|ons. l wlll only Lalk abouL Lhe annoLaLlons-drlven way.

MulLlples porLleLs can be deflned ln Lhe same Web appllcaLlon. 8uL lL seems LhaL each porLleL has Lo have lLs own
Sprlng MvC conLexL. lL should posslble Lo share daLa beLween porLleLs. See:
hLLp://publlb.boulder.lbm.com/lnfocenLer/wpdoc/v310/lndex.[sp?Loplc=/com.lbm.wp.zos.doc/wps/wpspLdesc.hLml
hLLp://www.llferay.com/communlLy/forums/-/message_boards/message/3446470
=/7/ V(%1?$ $+6M(
ln some of Lhese llnks Lhey do noL use annoLaLlons-drlven conf buL are sLlll below because Lhey are sLlll helpful for
some oLher reason.

hLLp://sLaLlc.sprlngsource.org/sprlng/docs/currenL/sprlng-framework-reference/hLml/mvc.hLml
hLLp://sLaLlc.sprlngsource.org/sprlng/docs/2.0.0/reference/porLleL.hLml
hLLp://levelup.llshman.com/sprlng/geLLlng-sLarLed/lndex.php (real good LuLorlal)
hLLp://books.dzone.com/arLlcles/hello-world-porLleL-uslng-Sprlng-3-porLleL-MvC (good LuLorlal)
hLLp://books.dzone.com/arLlcles/sprlng-30-porLleL-mvc-parL-2 (good LuLorlal)
hLLp://www.lnfoq.com/arLlcles/sprlng-2.3-ll-sprlng-mvc
hLLp://www.unlcon.neL/node/1367
hLLp://sLaLlc.sprlngsource.org/sprlng/docs/3.0.0.8LLLASL/sprlng-framework-reference/hLml/beans.hLml#beans-
annoLaLlon-conflg
hLLp://weblogs.[ava.neL/blog/seemarlch/archlve/2007/11/annoLaLlon_base.hLml
hLLp://lnLegraLlngsLuff.com/2011/03/29/geLLlng-sLarLed-wlLh-porLals-wrlLlng-a-porLleL-wlLh-sprlng-porLleL-mvc-and-
deploylng-lL-Lo-a-porLal/
hLLp://www.esup-porLall.org/consorLlum/espace/normes_1C/porLleL-sprlng/esup-porLleL-sprlng.hLml
hLLp://www.andypemberLon.com/sprlng/annoLaLlon-based-sprlng-porLleL-mvc-llfecycle/
hLLp://www.slldeshare.neL/[ohnalewls/annoLaLlon-based-sprlng-porLleL-mvc
hLLp://[onaLhanhul.com/sprlng-mvc-conLroller
hLLp://www.google.com/


=/9/ H;-+6< @I> ;-+6A+;$%

Source : hLLp://www.andypemberLon.com/sprlng/annoLaLlon-based-sprlng-porLleL-mvc-llfecycle/

As lL we are uslng Lhe annoLaLlon-drlven conflguraLlon, classes wlll need Lo be annoLaLed ln order for Sprlng Lo use
Lhem. ln [port|et-name]-port|et.xm|, Lhls musL be seL:
<context:annotation-config/>

1he maln Lypes are:
@Controller for web conLrollers. 1hey are used Lo handle user requesLs and redlrecL Lo vlews.
@Service for web servlces.
@Repository for uAC classes eLc.

All Lhese classes need Lo be ln a cerLaln package referred Lo by Lhls llne ln [port|et-name]-port|et.xm|.
<context:component-scan base-package="web" />

orLleLs can be dlsplayed ln Lhree modes. 1he normal mode called v|ew plus Lhe ed|t and he|p modes.
=/=/ J%;%6'%6A2 56D%A*+46
uependency ln[ecLlon ln Sprlng MvC can be used Lo llnk a ConLroller Lo a runnlng Web Servlce. When xml
conflguraLlon would have you wrlLe some really long conflguraLlon nodes Lo llnk !ava8eans, annoLaLlons make lL all a
loL easler.

LeL's say LhaL you have a Server called uselessServlce.
CreaLe a package web.serv|ces and creaLe Lhe class Use|essServ|ce.

uL Lhls lnslde:

package web.services;

import org.springframework.stereotype.Service;


@Service
public class UselessService {
public void uselessMethod() {
System.out.println("Wow! This was useless...");
}
}

lf you need Lo use Lhe meLhod uselessMeLhod() or Lhls servlce ln one of your conLroller, [usL wrlLe Lhls lnslde Lhe
conLroller:

private UselessService uselessService;

@Autowired
public void setUselessService(UselessService uselessService) {
this.uselessService = uselessService;
}

1hen slmply call Lhe meLhod by dolng uselessService.uselessMethod(). Sprlng ln[ecLs all dependencles when
Lhe porLleL ls belng deployed and you wlll geL a nasLy LxcepLlon lf lL can'L do lL because you dld someLhlng wrong
(Lrled Lo auLowlre a class LhaL doesn'L have Lhe @Service annoLaLlon eLc.).

@Autowired does all Lhe work by lLself as long as Lhere ls only 1 lnsLance of Lhls Lype of Servlce/8eposlLory. lf Lhere
ls more Lhan one, you wlll need Lo use Lhe annoLaLlon @Qualifier.
=/P/ @";;+6<
So conLrollers wlll need Lo be annoLaLed accordlng Lo Lhe mode Lhey wlll be used for. lor lnsLance, Lhe followlng
conLroller wlll be used for Lhe edlL mode:

@Controller
@RequestMapping("VIEW")
public class MainController {

V|ew mode ls Lhe maln mode you wlll be uslng. Ld|t mode can be used Lo allow Admlns Lo seL preferences for Lhe
porLleL (Lhere ls a parL abouL Lhls somewhere hldden ln Lhls documenLaLlon).

1o dlrecL a user from one vlew Lo and oLher, you wlll use Lhe annoLaLlon @RenderMapping. 1hls allows you Lo seL
Model parameLers and Lhe vlew Lo show.
1here ls anoLher lmporLanL annoLaLlon: @ActionMapping. lL ls used Lo perform cerLaln acLlons (such as lnserLlng
some daLa lnLo Lhe daLabase).
!"#"$" %&'(&)*+,,-'.
ln a vlew (a .[sp flle, leL's call lL page1.[sp), when you need Lo seL a llnk for Lhe user Lo reach anoLher vlew (page2.[sp),
here ls whaL you wlll enLer ln your .[sp flle:

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

<portlet:renderURL var="showPage2URL">
<portlet:param name="action" value="showPage2" />
</portlet:renderURL>

<a href="${showPage2URL}">Show me page 2</a>

1hen, ln your MalnConLroller.[ava, you wlll need a meLhod LhaL looks llke Lhls:

@RenderMapping(params = "action=showPage2")
public String showPage2() {
return "page2";

}

1he name of Lhe meLhod can be anyLhlng you wanL, be creaLlve. 1he only consLralnL ls LhaL Lhe meLhod reLurns a
SLrlng LhaL represenLs Lhe name of Lhe vlew (page2 -> page2.[sp).

So now you're Lhlnklng: !"#$% '()$*+ ,-* ."/ 0" 1 *)22 34(56' 789 /.$* *.) :$56 4$') 5;< =.) 4$') *.) -;)( ')*; *"
/.)6 .) "4)6; *.) 4"(*2)*<>.
lL's easy. Somewhere ln one of you conLroller, you [usL need a @RenderMapping wlLh no parameLer. Llke Lhls:

@RenderMapping
public String setModelAndView() {
return "page1";
}

So your MalnConLroller.[ava should look llke Lhls:

package web.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

@Controller
@RequestMapping("VIEW")
public class MainController {
@RenderMapping
public String setModelAndView() {
return "page1";
}

@RenderMapping(params = "action=showPage2")
public String showPage2() {
return "page2";
}
}

And lL should work.
!"#"0" 123-4'*+,,-'.
1he maln dlfference ls LhaL AcLlonMapplng meLhods reLurn noLhlng.
ln a .[sp flle Lhlngs are a llLLle blL dlfferenL, you do noL use portlet:renderURL anymore buL
portlet:actionURL lnsLead.

LeL's puL Lhls lnLo page2.[sp:

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

<portlet:actionURL var="doActionURL">
<portlet:param name="action" value="doAction" />
</portlet:actionURL>

<a href="${doActionURL}">do Something</a>

now, sLlll ln Lhe MalnConLroller.[ava, add Lhls meLhod:

@ActionMapping(params = "action=doAction")
public void doAction() {
System.out.println("Congratulations, you just did something.");
}


1esL lL. And you musL reallze LhaL yes, Lhe meLhod was called lndeed, buL you were senL back Lo page1.[sp. l'm preLLy
sure Lhls ls by deslgn and Lhere ls only one way l could flnd Lo prevenL Lhls. Add Lhls llne Lo your doAcLlon meLhod:
response.setRenderParameter("action", "showPage2");
So baslcally you dld Lhe acLlon buL you sLlll have Lo Lell Sprlng whaL 8enderMapplng Lo call. 1he meLhod would look
llke:

@ActionMapping(params = "action=doAction")
public void doAction(ActionResponse response) {
System.out.println("Congratulations, you just did something.");
response.setRenderParameter("action", "showPage2");
}
=/W/ 3"(( @4'%$ J"*" *4 " U+%G
1here are Lwo ways Lo proceed.
!"5"$" 67-'. 38& +''43+3-4' 9*4(&:133)-;<3&
MalnConLroller.[ava:

@RenderMapping(params = "action=showPage2")
public String showPage2() {
return "page2";
}
@ModelAttribute("something")
public String getSomething() {
return "bleh bleh bleh";
}
page2.[sp:

<%@ page isELIgnored="false" %>

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

<c:out value="${something}"/> <!-- this uses JSP Expression Language -->

1he meLhod getSomething()wlll be called every Llme Lhe conLroller ls used.

1he annoLaLed @ModelAttribute meLhods are called before Lhe @ActionMapping or @RenderMapping
meLhods.
!"5"0" 67-'. 38& *4(&:*+, ;&+'
MalnConLroller.[ava:

@RenderMapping(params = "action=showPage2")
public String showPage2(ModelMap model) {
model.addAttribute("something", "blah blah blah");
return "page2";
}

page2.[sp:

<%@ page isELIgnored="false" %>

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

<c:out value="${something}"/> <!-- this uses JSP Expression Language -->


ln Lhls case, you can generaLe Model daLa dependlng on whlch kenderMapp|ng meLhod ls called.
=/X/ Y%F?%(*3"-")
now leL us see how Lo pass parameLers Lo a kenderMapp|ng or Act|onMapp|ng meLhod from Lhe !S.

page1.[sp:

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

<portlet:renderURL var="showPage2URL">
<portlet:param name="action" value="showPage2" />
<portlet:param name="param1" value="foo" />
<portlet:param name="param2" value="bar" />
</portlet:renderURL>

<a href="${showPage2URL}">Show me page 2</a>

MalnConLroller.[ava:

@RenderMapping(params = "action=showPage2")
public String showPage2(@RequestParam String param1, @RequestParam String param2) {
System.out.println("Param1: " + param1);
System.out.println("Param2: " + param2);
return "page2";
}

Ckay now leL's Leach you how Lo do forms wlLh Sprlng MvC.
=/Z/ J+(;$"2 " $+(* 41 4#D%A*( G+*B "A*+46(
llrsL, add Lhls Lo your pro[ecL:

package domain.things;

public class SomeObject {
private int id;
private String name;
private String somethingElse;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSomethingElse() {
return somethingElse;
}
public void setSomethingElse(String somethingElse) {
this.somethingElse = somethingElse;
}

public SomeObject() {}


public SomeObject(int id, String name, String somethingElse) {
setId(id);
setName(name);
setSomethingElse(somethingElse);
}

public String toString() {
return getId() + "\n" + getName() + "\n" + getSomethingElse();
}
}

MalnConLroller.[ava:

package web.controllers;

import java.util.LinkedList;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import domain.things.*;

@Controller
@RequestMapping("VIEW")
public class MainController {
@RenderMapping
public String setModelAndView(ModelMap model) {
List<SomeObject> listOfSomeObject = new LinkedList<SomeObject>();
listOfSomeObject.add(new SomeObject(13, "Object 13", "aze"));
listOfSomeObject.add(new SomeObject(56, "Object 56", "aze"));
listOfSomeObject.add(new SomeObject(41, "Object 41", "aze"));
listOfSomeObject.add(new SomeObject(11, "Object 11", "aze"));

model.addAttribute("listOfSomeObject", listOfSomeObject);

return "page1";
}

@ActionMapping(params = "action=doSomethingWithSomeObject")
public void doSomethingWithSomeObject(@RequestParam int someObjectId) {
System.out.println("SomeObjectId: " + someObjectId);
}
}

page1.[sp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>

<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Action</td>
</tr>
<c:forEach var="someObject" items="${listOfSomeObject}">
<tr>
<td><c:out value="${someObject.id}" /></td>
<!-- Automatically calls someObject.getId() -->

<td><c:out value="${someObject.name}" /></td>
<!-- Automatically calls someObject.getName() -->
<td><a
href="
<portlet:actionURL>
<portlet:param name="action"
value="doSomethingWithSomeObject" />
<portlet:param name="someObjectId"
value="${someObject.id}" />
</portlet:actionURL>
"><strong>Do something</strong></a>
</tr>
</c:forEach>
</table>

now you have a real ugly llsL LhaL can perform acLlons eLc.
=/./ [4-)(
!"$"$" =&)> ;+7-2 ?4)@
page1.[sp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!-- Link -->
<portlet:actionURL var="doSimpleFormURL">
<portlet:param name="action" value="doSimpleForm" />
</portlet:actionURL>

<!-- Form -->
<form method="POST" action="${doSimpleFormURL}">
<input name="input1" id="input1" value="<c:out value="${stuff}"/>" />
<input type="submit" value="Submit" />
</form>

MalnConLroller.[ava

package web.controllers;

import javax.portlet.ActionRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

@Controller
@RequestMapping("VIEW")
public class MainController {
@RenderMapping
public String setModelAndView() {
return "page1";
}

@ModelAttribute("stuff")
public String getStuff() {
return "some stuff";
}


@ActionMapping(params = "action=doSimpleForm")
public void changePreferences(@RequestParam String input1, ActionRequest
actionRequest) {
System.out.println(input1);
}
}
!"$"0" *4)& +(A+'2&( ?4)@
Ckay now someLlmes you may wanL Lo bulld more complex forms. Maybe you have an ob[ecL, and you wanL Lo
populaLe lL wlLh a form. So here's how l'd do lL:
We'll be uslng agaln doma|n.th|ngs.SomeCb[ect.[ava descrlbed a few pages up.

MalnConLroller.[ava:

package web.controllers;

import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import domain.things.*;

@Controller
@RequestMapping("VIEW")
public class MainController {
@RenderMapping
public String showForm() {
return "page1";
}

@InitBinder("someObject")
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"somethingElse"}); // to make
sure that we won't try to set this value in the form
dataBinder.setRequiredFields(new String[] {"id", "name"});
dataBinder.registerCustomEditor(String.class, new
StringTrimmerEditor(false)); // removes leading and trailing whitespace from a String.
}

@ModelAttribute("someObject")
public SomeObject getSomeObject() {
return new SomeObject(666, "I'm the devil", "What?");
}

@ActionMapping(params = "action=doFormAction")
public void doFormAction(@ModelAttribute SomeObject someObject) {
System.out.println(someObject.toString());
}
}

page1.[sp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>


<!-- Link -->
<portlet:actionURL var="doFormActionURL">
<portlet:param name="action" value="doFormAction" />
</portlet:actionURL>

<!-- Form -->
<form:form name="form" modelAttribute="someObject" method="post"
action="${doFormActionURL}">
<table>
<tr>
<td><form:input path="id" /></td>
</tr>
<tr>
<td><form:input path="name" /></td>
</tr>
</table>
<input type="submit" value="Just do it" />
</form:form>
!"$"B" C<734@=+:-(+34)7
WhaL we dld before ls greaL buL when Lhe lnpuLs are lefL empLy, lL ralses an error. Same Lhlng when you enLer
someLhlng oLher Lhan a number lnLo Lhe ld lnpuL. 1haL's were custom va||dators come ln.
?ou wlll need a .properLles flle wlLh some error codes. 1hey're descrlbed laLer ln Lhls doc, baslcally, you need Lo add
Lhls Lo Lhe [port|et-name]-port|et.xm| flle:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="/WEB-INF/messages" />

1hen creaLe Lhe flle messages.propert|es ln docroot]WL8-INI] and puL Lhls lnslde:

validation.error.isEmpty=The field is empty, how do you expect the form to validate?
validation.error.formatIsntRight=Wrong format! Think before you submit!

?our error messages have Lo be really mean and condescendlng.

CreaLe Lhe class SomeCb[ecLvalldaLor.[ava:

package web.controllers;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import domain.things.*;

@Component
public class SomeObjectValidator implements Validator {
public boolean supports(Class<?> klass) {
return SomeObject.class.isAssignableFrom(klass);
}

public void validate(Object target, Errors errors) {
SomeObject someObject = (SomeObject)target;
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "id",
"validation.error.isEmpty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name",
"validation.error.isEmpty");

int id = someObject.getId();
int idLength = Integer.toString(id).length();

if (idLength > 50 || idLength < 1) {
errors.rejectValue("id", "validation.error.formatIsntRight");
}

String name = someObject.getName();
if (name.length() > 100 || name.length() < 2) {
errors.rejectValue("name", "validation.error.formatIsntRight");
}
}
}

MalnConLroller.[ava:

package web.controllers;

import javax.portlet.ActionResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import domain.things.*;

@Controller
@RequestMapping("VIEW")
@SessionAttributes("machinesGroup") // keeps the object in session memory between
postbacks
public class MainController {
@RenderMapping
public String showForm() {
return "page1";
}

@Autowired
private SomeObjectValidator someObjectValidator;

@InitBinder("someObject")
public void initBinder(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields(new String[] {"somethingElse"}); // to make
sure that we won't try to set this value in the form
dataBinder.setRequiredFields(new String[] {"id", "name"});
dataBinder.registerCustomEditor(String.class, new
StringTrimmerEditor(false)); // removes leading and trailing whitespace from a String.
}

@ModelAttribute("someObject")
public SomeObject getSomeObject() {
return new SomeObject(666, "I'm the devil", "What?");
}

@ActionMapping(params = "action=doFormAction")
public void doFormAction(@ModelAttribute SomeObject someObject, BindingResult
bindingResult, ActionResponse response, SessionStatus sessionStatus) {
someObjectValidator.validate(someObject, bindingResult);
if (!bindingResult.hasErrors()) {
System.out.println(someObject.toString());

// sets the session status as complete to cleanup the model
attributes stored using @SessionAttributes
sessionStatus.setComplete(); // With Spring 3.1.0, it doesnt seem to
work with ActionMapping (it does with RenderMapping). Is it a bug?
}
}
}

page1.[sp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!-- Link -->
<portlet:actionURL var="doFormActionURL">
<portlet:param name="action" value="doFormAction" />
</portlet:actionURL>

<!-- Form -->
<form:form name="form" modelAttribute="someObject" method="post"
action="${doFormActionURL}">
<table>
<tr>
<td><form:input path="id" /></td>
<td><form:errors path="id" /></td>
</tr>
<tr>
<td><form:input path="name" /></td>
<td><form:errors path="name" /></td>
</tr>
</table>
<input type="submit" value="Just do it" />
</form:form>
=/7/ J4 :D"E
lL ls posslble Lo do a[ax calls, Lhls works preLLy much llke Lhe oLher Mapplng acLlons. 1hls Llme lL uses Lhe annoLaLlon
@ResourceMapping. 1he maln dlfference beLween @ResourceMapping and @RenderMapping ls LhaL a
8enderMapplng P11 requesLs reLurns Lhe whole P1ML for Lhe porLal page, wlLh all oLher porLleLs, when Lhe
8esourceMapplng only reLurns whaL you glve lL.

page1.[sp:

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

<portlet:resourceURL var="doAjaxURL" id="doAjax" escapeXml="false" />
<script type="text/javascript">
<%@ include file="/js/getCharts.js" %>
</script>

essal.[s (uslng LxL !S, requlres Lo lnclude some .[s flles ln Lhe page, buL lL's [usL a sample):

Ext.Ajax.request({
url: "${doAjaxURL}",
params: {
param1: "Hi"
},
success: function(response) {
alert(response.responseText);
}
});


MalnConLroller.[ava:

@ResourceMapping(value="doAjax")
public void doAjax(ResourceRequest request, ResourceResponse response, @RequestParam
String param1){
response.setContentType("application/x-json");
response.getWriter().print("Hi back!");
}

Cne Lhlng you should know ls LhaL when you have an appllcaLlon LhaL does A[ax requesLs whlle you are hoL
redeploylng Lhe porLleL LhaL conLalns Lhe appllcaLlon, you may geL errors ln Lhe LomcaL console. l haven'L found a
way Lo prevenL Lhls yeL. ?ou can elLher resLarL Lhe server or close all web browsers LhaL do Lhe A[ax calls.


P/ @"M% 34-*$%*(R *+;(
P/./ 56+* "6' '%(*-42 )%*B4'( 41 " A4);46%6*
lnlL MeLhod: CreaLe a meLhod ln Lhe Servlce and annoLaLe lL wlLh: @PostConstruct
uesLroy MeLhod: CreaLe a meLhod ln Lhe Servlce and annoLaLe lL wlLh: @PreDestroy
P/7/ 8%* >46*%E* 3"*B O A$+%6* (+'% *%AB64$4<2/
WrlLe Lhe conLexL-paLh ln a !S flle:
<c:out value="${pageContext.request.contextPath}" />

P/9/ "AA%(( -%6'%-Y%F?%(*R -%6'%-Y%(;46(% +6 D(;
Add Lhls: <portlet:defineObjects/>

Lxample: 1esL porLleL mode :
<c:choose>
<c:when
test="<%=renderRequest.getPortletMode().equals(javax.portlet.PortletMode.VIEW)%>">
aze
</c:when>
</c:choose>
P/=/ 8%* 34-*$%* >46*%E* "6' >46*%E* Y%"$ 3"*B C (%-U%- (+'%
ln your conLroller class, [usL add Lhls:

private PortletContext portletContext;

@Autowired
public void setPortletContext(PortletContext portletContext) {
this.portletContext = portletContext;
}

1hen use: porLleLConLexL.geL8ealaLh("/")
P/P/ :AA%(( (*"*+A -%(4?-A%(
docrooL
- lmages
o lmage.[pg
- resources
- .
- WL8-lnl
o [sp
o .

lf you need Lo access sLaLlc conLenL ln Lhe resources" folder (for lnsLance lmage.[pg), puL ln [port|et-name]-
port|et.xm| : <mvc:resources mapping="/images/**" location="/images/" />


1hen access lL uslng: <img src="<c:url value="/images/image.jpg" />" />
P/W/ )%(("<%(/;-4;%-*+%( 1+$%(
?ou can creaLe .properLles flles Lo handle mulLlple languages eLc.
1hey look llke Lhls:

global.actions.Edit=Edit
global.actions.Delete=Delete
global.actions.Remove=Remove
global.actions.Action=Action
global.actions.Add=Add
global.actions.Ok=Ok

1o be able Lo use Lhem, you need Lo add Lhls somewhere ln Lhe [port|et-name]-port|et.xm| flle:

<!-- Access resource bundles with the specified basename -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
p:basename="/WEB-INF/[File name without the .properties extension]" />

1hen ln a !S flle, do Lhls:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:message code="global.action.Edit"/>
P/X/ K'+* @4'% "6' 3-%1%-%6A%(
#"D"$" E8+3 ,)&?&)&'2&7 +)&
references are daLa LhaL can be sLored for a porLleL wlLhouL needlng a daLabase. ?ou can use lL Lo make a porLleL
conflgurable.
#"D"0" F&:, G-38 ,)&?&)&'2&7
hLLp://blog.asoluLlons.com/2010/03/user-preferences-ln-llferay/
#"D"B" H'+;:& &(-3 @4(& +'( 24'?-.<)& ,)&?&)&'2&7
Add <portlet-mode>edit</portlet-mode> Lo porLleL.xml ln <supports>
ln Llferay-porLleL.xml seL <instanceable>true</instanceable> (can have more Lhan one lnsLance of Lhls
porLleL ln one page).
Advlsed conflguraLlon: owned-by-group=Lrue, unlque-per-layouL=Lrue: preferences of a porLleL wlll be shared by all
users buL dlfferenL for each lnsLance of Lhe porLleL.
#"D"!" 122&77 &(-3 @4(& -' I-?&)+>
?ou musL be logged as admln. Cllck Lhe Wrench |con and go Lo references.
#"D"#" J4)3:&3 J)&?&)&'2&7
hLLp://blog.asoluLlons.com/2010/03/user-preferences-ln-llferay/

P/Z/ @+6+1+%- C $4*( 41 /D( 1+$%( +6 *4)A"*\*%);\)+6+1+%-\]
Llferay mlnlfles css and [avascrlpL flles and sLores Lhem ln llferay-porLal/LomcaL/Lemp
1o dlsable lL, add Lhls ln Lhe porLal-exL.properLles:
com.llferay.porLal.servleL.fllLers.mlnlfler.MlnlflerlllLer=false
com.llferay.porLal.servleL.fllLers.sLrlp.SLrlplllLer=false
[avascrlpL.fasL.load=false

1he above may cause lnconvenlences such as Lhe [avascrlpL of Lhe Llferay componenLs noL worklng anymore. ln Lhls
case, Lhe besL you can do ls Lo add ?js_fast_load=0 Lo Lhe llnk Lo Lhe .[s flles ln ||feray-port|et.xm|:
<header-portlet-javascript>/js/extjs/ext-all.js?js_fast_load=0</header-portlet-
javascript>


W/ NH^0 J"*"#"(%
W/./ KE;$4-+6< 0+1%-"2_( NH^0 J"*"#"(%
1he server should be off.
use Lhe defaulL PSCLu8 Lxplorer:
Co Lo dlrecLory where hsql.[ar ls Llferay-porLal-<verslon>\LomcaL-<verslon>\llb\exL\
[ava -cp hsql.[ar org.hsqldb.uLll.uaLabaseManager

urlver: <paLh Lo Llferay-porLal>\LomcaL-<verslon>\llb\exL\hsql.[ar
1ype: ln memory
u8L: [dbc:hsqldb:flle:<paLh Lo Llferay-porLal>\LomcaL-<verslon>\daLa\hsql\lporLal (ex:
[dbc:hsqldb:flle:\rogra~1\Llferay\llferay-porLal-6.0.6\daLa\hsql\sbc\sbcdb)
username: sa
assword: <blank>

|porta| represenLs Lhe name of Lhe daLabase. ?ou wlll know lf lL ls Lhe rlghL folder lf Lhere ls lporLal.scrlpL and
lporLal.properLles ln Lhe folder (daLa\hsql\). lL may move afLer Llferay upgrades.

1ables are under u8LlC/1A8LL

?ou may have Lo seL: hsqldb.lock_file=false ln flle: <paLh Lo Llferay-porLal>\daLa\hsql\lporLal\lporLal.properLles
?ou may have Lo deleLe Lhe .lck flle ln <paLh Lo Llferay-porLal>\daLa\hsql\lporLal\

W/7/ >-%"*% " 6%G NH^0 J"*"#"(%
uo preLLy much exacLly llke Lhe prevlous sLep buL lnsLead of |porta| puL Lhe name of your daLabase

usual PSCL Lables creaLlon commands:

9?@A=@ =ABC@ '("-4; D
50 1E=@F@? 1G@E=1=H+
6$:) 8A?9IA?DJKLM EN= EOCC+
M

9?@A=@ =ABC@ :$P.56); D
50 1G@E=1=H+
54 8A?9IA?DQRM EN= EOCC+
6$:) 8A?9IA?DJKLM EN= EOCC
M

9?@A=@ =ABC@ '("-4S:$P.56) D
'("-4S50 1E=@F@? EN= EOCC+
:$P.56)S50 1E=@F@? EN= EOCC+
TN?@1FE U@H D'("-4S50M ?@T@?@E9@3 '("-4;D50M NE G@C@=@ 9A39AG@ NE OVGA=@ 9A39AG@+
TN?@1FE U@H D:$P.56)S50M ?@T@?@E9@3 :$P.56);D50M NE G@C@=@ 9A39AG@ NE OVGA=@ 9A39AG@
M

luLn1l1? Lakes care of Lhe auLo lncremenL.

W/9/ 0"?6AB " NH^0 H%-U%-
8equlres boLh server.baL and server.properLles flles

server.baL: [ava -cp <paLh Lo hsql.[ar> org.hsqldb.Server -daLabase.0 flle:<name of flle .scrlpL wlLhouL Lhe .scrlpL> -
dbname.0 <allas of Lhe daLabase>

server.properLles: server.porL=<server porL>

LxecuLe server.baL Lo launch Lhe server
W/=/ >466%A* *4 " NH^0 '"*"#"(% 1-4) " H;-+6< @I> ";;$+A"*+46
8equlres Lo add hsqldb.jar lnLo /WL8-lnl/llb/

ln Sprlng MvC ConLexL conflguraLlon xML flle:

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>org.hsqldb.jdbcDriver</value></property>
<property name="url">
<value>jdbc:hsqldb:hsql://localhost:<server port>/<alias of the
database></value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>


X/ V(% KE* `H +6 " ;4-*$%*
X/./ 56*-4'?A*+46
LxL !s ls a [avascrlpL framework. See hLLp://sencha.com for more lnformaLlon.
X/7/ 56(*"$$"*+46
uownload Lhe laLesL verslon from sencha.com. 1he .zlp flle downloaded wlll conLaln loLs of flles lncludlng examples
eLc.
1he only flles you wlll really need are:
exL-all.[s or exL-all-debug.[s
exL-all.css

1hen you mlghL need some lmages conLalned ln Lhe resources" dlrecLory dependlng on Lhe componenLs you'll be
uslng.
CeLLlng sLarLed gulde: hLLp://docs.sencha.com/exL-[s/4-0/#/gulde/geLLlng_sLarLed
X/9/ V(% KE* `H +6*4 " ;4-*$%*
Pere's a good way Lo do Lhlngs. Add Lhese flles lnLo your pro[ecL:
docrooL/[s/exL[s/exL-all.[s
docrooL/css/exL[s/exL-all.css
docrooL/lmages/. for some lmages, lcons for grlds eLc.
docrooL/resources/Lhemes/lmages/defaulL/. for Lheme lmages

1hen, ln ||feray-port|et.xm|, you should have Lhls:
<portlet>
<header-portlet-css>/css/extjs/ext-all.css</header-portlet-css>
<header-portlet-javascript>/js/extjs/ext-all.js?js_fast_load=0</header-portlet-
javascript>
</portlet>

1hls adds Lhe .css and .[s flles ln Lhe header of Lhe page so you can use Lhem ln your porLleL vlews. 1hls can creaLe css
confllcs beLween exL-all.css and Llferay's css. l dldn'L flnd a way Lo resolve Lhese yeL.

1hls ls how you add LxL !S Lo a porLleL. 1he addlLlon of ?js_fast_load=0 ls Lo prevenL Llferay from mlnlflng Lhe .[s
flle. 1here ls a parL abouL Lhls ln Lhe L|feray Genera| Conf|gurat|on parL.
X/=/ :-AB+*%A*?-% 41 "6 KE* `( ";;$+A"*+46
Pere's whaL a .[s flle LhaL uses LxL !S should look llke:
/* Ext.require allows to asynchronously load graphical components (such as panel,
charts etc...). When you need such components (which may not always be the case) it is
required to list them in here */
Ext.require([
'Ext.panel.*',
'Ext.chart.*',

'Ext.data.*',
'Ext.grid.*',
'Ext.util.*',

... /* add other components you might need */
]);
Ext.onReady(function () {
/* this is where all your code should be (of course functions can be outside, but
call them from here */
});
X/P/ >B"6<% *B% A46*%6* 41 " '4) %$%)%6*
<div id="aze"></div>

<script type="text/javascript">
Ext.onReady(function () {
Ext.get("aze").dom.innerHTML = "blah";
});
</script>
X/W/ 5);4-*"6* *B+6< *4 M64G "#4?* D"U"(A-+;* 4#D%A*(
When uslng LxL !S you wlll flnd loLs of conflguraLlon array llke Lhls:

(uo noL puL a comma where lL says: NO COMMA)

Ext.create('Ext.grid.Panel', {
store: store,
cls: "css_centered",
columns: [
{
text : 'ID',
flex : 1,
resizable: false,
sortable : false,
draggable: false,
hideable : false,
dataIndex: 'ID' NO COMMA
},
{
text : 'VALUE',
width : 100,
resizable: false,
sortable : false,
draggable: false,
hideable : false,
dataIndex: 'VALUE' NO COMMA
} NO COMMA
],
autoHeight: true,
width: 400,
title: 'Top 10 Apps',
renderTo: 'div_top10Grid',
viewConfig: {
stripeRows: true NO COMMA
} NO COMMA
});

AlLhough llrefox and Chrome don'L mlnd Lhe exLra comma, lL Lends Lo noL llke lL and noL dlsplay a slngle [avascrlpL
elemenL ln Lhe page lf Lhere ls a slngle exLra comma. 1hen lL's really compllcaLed Lo debug.
8e careful, lL's really easy Lo remove a conflguraLlon llne and forgeL Lo remove Lhe prevlous comma lf LhaL llne was aL
an end.

X/X/ QB"*_( " KE*/'"*"/(*4-%a
1hls ls whaL LxL !S uses Lo sLore daLa LhaL can be used Lo dlsplay ln charLs or grlds. 1hese componenLs are bound Lo
a sLore, updaLlng a componenL's sLore wlll auLomaLlcally updaLe lLs conLenL.
?ou can load daLa dlrecLly from lnslde Lhe deflnlLlon of your sLore, or pass daLa Lo lL.

Pere, we deflne a model for Lhe daLa conLalned lnLo Lhe sLore, and Lhen we declare Lhe sLore wlLh a proxy ob[ecL Lo
geL A[ax daLa:

Ext.define('User', {
extend: 'Ext.data.Model',
fields: [
{name: 'firstName', type: 'string'},
{name: 'lastName', type: 'string'},
{name: 'age', type: 'int'},
{name: 'eyeColor', type: 'string'}
]
});

var myStore = new Ext.data.Store({
model: 'User',
proxy: {
type: 'ajax',
url : '/users.json',
reader: {
type: 'json',
root: 'users'
}
},
autoLoad: true
});

Pere ls a dlfferenL way Lo do Lhlngs:

var data = [{data:'aze'}, {data:'azeazeaze'}];
var store = Ext.create('Ext.data.JsonStore', {
fields: ['data'],
data: storeData
});

1hen you can updaLe Lhe daLa array and apply Lhe modlflcaLlons Lo Lhe sLore llke Lhls:

store.loadData(data, false);

SeL Lhe second parameLer Lo Lrue Lo append daLa Lo Lhe sLore.

I prefer the second way as |t a||ows you more eas||y to |oad A[ax data |nto an array and then |nsert the new data
|nto the store.
X/Z/ :D"E Y%F?%(*(
Pere's how you do a baslc A[ax requesL:

Ext.Ajax.request({
url: "url",
timeout: 1000, // timeout of Ajax Request
autoAbort: false, // new request aborts previous requests or not
success: function(response) { // called on success

var text = response.responseText;
var object = Ext.decode(text);
},
failure: function(result, request) { // called on failure
alert("Ajax request Failed<br />status code: " + result.status + "<br
/>status text: " + result.statusText + "<br />response text: " + result.responseText)
}
});

lL's very lmporLanL Lo lmplemenL Lhls somewhere Lo caLch a[ax excepLlons:

Ext.Ajax.on('requestexception', function(){
});
X/b/ `Hcd
!SCn sLands for !avaScrlpL Cb[ecL noLaLlon. lL's an efflclenL way Lo pass daLa durlng A[ax requesLs.

Pere's a baslc !SCn ob[ecL:
W!50>XJK+ !6$:)>X>Y)$6 9.$(2);>Z

Pere's an array of 3 !SCn ob[ecLs:
[W!50>XJK+ !6$:)>X>\)$6 9.$(2);>Z+ W!50>XJJ+ !6$:)>X>\)$6 V5)(()>Z+ W!50>XJ]+ !6$:)>X>\)$6 8$2Y)$6>Z^

LxL [s can Lurn !SCn sLrlng lnLo acLual !SCn uslng Lhe funcLlon LxL.decode:

var array = new Array();
array = Ext.decode('[{"id":12, "name":"Jean Charles"}, {"id":11, "name":"Jean Pierre"},
{"id":10, "name":"Jean Valjean"}]');
var object0 = array[0];
object0.id;
object0.name;

Das könnte Ihnen auch gefallen