Spring MVC + blzeds + flex framework practice: helloworld

Source: Internet
Author: User

I. Environment Configuration:

Myeclipse7.0

Felx builder3.0

Jdk1.6

Flex sdk3.2

Tomcat6.0

2. Create a Java Web project

Create a WEB Project: testproject in myeclipse
Copy the relevant jar package to the WEB-INF/lib and introduce it to the build path of the project.

3. Create a flex Project

Create a flex project: testproject In Flex Builder

 

4. Configure web. xml

Configure webroot/WEB-INF/Web. XML in a Java Web project
<! -- Add mode file --> <br/> <web-app version = "2.4" <br/> xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee <br/> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <br/> <! -- Specify the welcome page --> <br/> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> <! -- Specify the initialization file --> <br/> <context-param> <br/> <param-Name> contextconfiglocation </param-Name> <br/> <param-value> <br/> classpath: applicationcontext. XML, classpath: Application-Flex.xml <br/> </param-value> <br/> </context-param> <br/> <! -- Add a listener --> <br/> <listener-class> <br/> Org. springframework. web. context. contextloaderlistener <br/> </listener-class> <br/> </listener> <br/> <listener-class> <br/> org. springframework. security. UI. session. httpsessioneventpublisher <br/> </listener-class> <br/> </listener> <br/> <! -- Add servlet configuration --> <br/> <servlet-Name> flex </servlet-Name> <br/> <servlet-class> Org. springframework. web. servlet. dispatcherservlet <br/> </servlet-class> <br/> <load-on-startup> 1 </load-on-startup> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> flex </servlet-Name> <br/> <URL-pattern>/messagebroker/* </URL -Pattern> <br/> </servlet-mapping> <br/> <! -- Add session configuration --> <br/> <session-config> <br/> <session-Timeout> 30 </session-Timeout> <br/> </session-config> <br/> <! -- Add mode file --> <br/> <web-app version = "2.4" <br/> xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee <br/> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <br/> <! -- Specify the welcome page --> <br/> <welcome-file-List> <br/> <welcome-File> index. JSP </welcome-File> <br/> </welcome-file-List> <br/> <! -- Specify the initialization file --> <br/> <context-param> <br/> <param-Name> contextconfiglocation </param-Name> <br/> <param-value> <br/> classpath: applicationcontext. XML, classpath: Application-Flex.xml <br/> </param-value> <br/> </context-param> <br/> <! -- Add a listener --> <br/> <listener-class> <br/> Org. springframework. web. context. contextloaderlistener <br/> </listener-class> <br/> </listener> <br/> <listener-class> <br/> org. springframework. security. UI. session. httpsessioneventpublisher <br/> </listener-class> <br/> </listener> <br/> <! -- Add servlet configuration --> <br/> <servlet-Name> flex </servlet-Name> <br/> <servlet-class> Org. springframework. web. servlet. dispatcherservlet <br/> </servlet-class> <br/> <load-on-startup> 1 </load-on-startup> <br/> </servlet> <br/> <servlet-mapping> <br/> <servlet-Name> flex </servlet-Name> <br/> <URL-pattern>/messagebroker/* </URL -Pattern> <br/> </servlet-mapping> <br/> <! -- Add session configuration --> <br/> <session-config> <br/> <session-Timeout> 30 </session-Timeout> <br/> </session-config>

 

5. Create applicationcontext. XML, Application-Flex.xml

In
Web. xml
The initialization configuration file is configured in
(Applicationcontext. xml
,
Application-Flex.xml)
Therefore, you must
SRC
Create the corresponding
XML
File
(
The file name must be
Web. xml
The configuration file name set in is the same; otherwise, an error will be reported during loading.
)
.

 

6. Configure applicationcontext. xml
<Beans <br/> xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: Flex = "http://www.springframework.org/schema/flex" <br/> xmlns: AOP = "http://www.springframework.org/schema/aop" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans </P> <p> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd </P> <p> http://www.springframework.org/schema/aop </P> <p> http://www.springframework.org/schema/aop/spring-aop-2.0.xsd </P> <p> http://www.springframework.org/schema/flex </P> <p> http://www.springframework.org/schema/flex/spring-flex-1.0.xsd "> <br/> <AOP: aspectj-autoproxy/> <br/> <bean id = "timingaspect" class = "org. perf4j. log4j. AOP. timingaspect "/> <br/> </beans> <br/>

 

VII. Spring MVC Configuration
Using spring MVC Framework, two configuration files: applicationcontext. XML,
[Servlet-name]-servlet. xml
WEB-INF/[servlet-name]-servlet. XML, which is used to configure the necessary

Bean. [Servlet-name] refers to the name of the spring dispatcher servlet defined in the web. xml deployment descriptor.
According to the name defined in Web. XML, our configuration file should be flex-servlet.xml. In
Create a WEB-INF under the flex-servlet.xml.

Configure flex-servlet.xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: Flex = "http://www.springframework.org/schema/flex" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "</P> <p> http://www.springframework.org/schema/beans </P> <p> http://www.springframework.org/schema/beans/spring-beans-2.5.xsd </P> <p> http://www.springframework.org/schema/flex </P> <p> http://www.springframework.org/schema/flex/spring-flex-1.0.xsd"> <br/> </beans> <br/>

 

8. Configure spring + blazeds
Blazeds is a web communication technology used by the server to communicate with Java. It allows developers to easily connect to the backend distributed data and push data to flex or AIR applications. It is a rich Internet application (RIA) provide a better experience.
Spring's integration with blazeds exposes spring bean to the blazeds service. In spring blazeds
Before integration, you need to write your own code to access spring bean from blazeds. Use spring blazeds
Integration. When flash is used to access the server, spring will forward the request to the flex message.
Broker. At the same time, we expose some beans in spring to flex remoting.
Then flash can call the bean exposed as flex remoting service in spring.
The main blazeds component is messagebroker.
The HTTP information transmitted by the flex end will be triggered by spring-managed through spring's dispatcherservlet.
Messagebroker. That is to say, spring uses dispatcherservlet to manage the messagebroker of blazeds.
Configure Application-Flex.xml
<Beans <br/> xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xmlns: Flex = "http://www.springframework.org/schema/flex" <br/> xmlns: AOP = "http://www.springframework.org/schema/aop" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans </P> <p> http://www.springframework.org/schema/beans/spring-beans-2.0.xsd </P> <p> http://www.sprin Gframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd </P> <p> http://www.springframework.org/schema/flex </P> <p> http://www.springframework.org/schema/flex/spring-flex-1.0.xsd "> <br/> <! -- Use Spring Message Broker instead of blazeds --> <br/> <flex: Message-broker>

 

IX. configuration of blazeds
1) download blazeds
: Http://opensource.adobe.com/wiki/display/blazeds/Release+Builds

2. Decompress blazeds.zip, decompress the blazeds. War file, and copy the four XML files (messaging-
Config. XML, proxy-config.xml, remoting-config.xml, services-config.xml) copy

  • Java engineering webroot/WEB-INF/flex (Flex directory needs to be manually created)
  • Flex project src/config (the config directory needs to be manually created)

(3) modify the flex project src/config/services-config.xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <services-config> <br/> <services> <br/> <service-include file-Path = "remoting-config.xml"/> <br/> <service- include File-Path = "proxy-config.xml"/> <br/> <service-include file-Path = "messaging-config.xml"/> <br/> </services> <br/> <security> <br/> <login-command class = "flex. messaging. security. tomcatlogincommand "<br/> Server =" Tomcat "/> <br/> </Security> <br/> <channels> <br/> <channel-definition <br/> id = "My-AMF" <br/> class = "MX. messaging. channels. amfchannel "> <br/> <endpoint <br/> url ="/testweb/messagebroker/AMF "<br/> class =" flex. messaging. endpoints. amfendpoint "/> <br/> </channel-definition> <br/> <channel-definition <br/> id =" My-secure-AMF "<br/> class = "MX. messaging. channels. secureamfchannel "> <br/> <endpoint url ="/testweb/messagebroker/amfsecure "<br/> class =" flex. messaging. endpoints. secureamfendpoint "/> <br/> <Properties> <br/> <add-no-Cache-headers> false </Add-no-Cache-headers> <br/> </Properties> <br/> </channel-definition> <br/> <channel-definition <br/> id = "My-polling-AMF" <br/> class = "flex. MX. messaging. channels. amfchannel "> <br/> <endpoint <br/> url ="/testweb/messagebroker/amfpolling "<br/> class =" flex. messaging. endpoints. amfendpoint "/> <br/> <Properties> <br/> <polling-enabled> true </polling-enabled> <br/> <polling-interval-seconds> 4 </polling-interval-seconds> <br/> </Properties> <br/> </channel-definition> <br/> </channels> <br/> <logging> <br/> <target class = "flex. messaging. log. consoletarget "level =" error "> <br/> <Properties> <br/> <prefix> [blazeds] </prefix> <br/> <shortdedate> false </shortdedate> <br/> <shortdetime> false </shortdetime> <br/> <shortdelevel> false </shortdelevel> <br/> <shortdecategory> false </shortdecategory> <br/> </Properties> <br/> <Filters> <br/> <pattern> endpoint. * </pattern> <br/> <pattern> service. * </pattern> <br/> <pattern> Configuration </pattern> <br/> </Filters> <br/> </Target> <br/> </Logging> <br/> <system> <br/> <redeploy> <enabled> false </enabled> </redeploy> <br/> </system> <br/> </services-config> <br/>

 

10. Framework Application
1) Create userservice. Java
After setting up the framework, you need to create a Java class in the Java project and release it to the flex application through the configuration file to verify whether the communication between Java and flex is successful.
Create package: COM. admin. Service and userservice. Java under the SRC project of Java.

Package com. admin. Service;
Public class userservice {
Public String getmessage (){
System. Out. println ("excute userservice:" + "Hello world! ");
Return "Hello World ";
}
}

2) Publish userservice
All Java classes, if you want to publish to a flex application, you need to add the <flex: remoting-destination> node in application-flex and add the following configuration in the Application-Flex.xml:
<Bean id = "userservice" class = "com. Hand. admin. Service. userservice">
<Flex: remoting-Destination/>
</Bean>

3) Flex references userservice

For remote services released for all Java projects, if you need to call, you must configure them in the configuration file remoting-config.xml of the Flex project, in
Add the following configuration in the remoting-config.xml (the source here must be the bean provided when the remote service is released with the Java Project
The ID is the same, otherwise the call will fail ):

<Destination ID = "userservice">
<Properties>
<Source> userservice </source>
</Properties>
</Destination>

4) create a flex Application
Create the modules/common directory under the SRC of the Flex project, create index. mxml, and call userservice
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" <br/> layout = "vertical" <br/> horizontalalign = "center" <br/> creationcomplete = "initapp () "<br/> fontsize =" 15 "> <br/> <mx: SCRIPT> <br/> <! -- [CDATA [<br/> Import MX. RPC. events. resultevent; <br/> Import MX. controls. alert; <br/> private function initapp (): void {<br/> userservice. getmessage (); <br/> userservice. getmessage. addeventlistener (resultevent. result, getmessagehandler); <br/>}< br/> private function getmessagehandler (EVT: resultevent): void {<br/> userservice. getmessage. removeeventlistener (resultevent. result, getmessagehandler); <br/> inittext. TEXT = EVT. result as string; <br/>}< br/>] --> <br/> </MX: SCRIPT> <br/> <mx: remoteobject id = "userservice" Destination = "userservice"/> <br/> <mx: label text = "welcome" id = "inittext" fontweight = "bold"/> <br/> </MX: Application> <br/>

5) modify the flex Main Application

Note that a main application testproject. mxml is provided by default when the flex project is created. When we want the main application to be determined by ourselves (in this
In this example, the main application is moduls/common/index. mxml. You need to modify the. actionscriptproperties file under the flex project directory.
. Modify the following configurations:
<Applications>
<Application Path = "testflex. mxml"/>
<Application Path = "modules/common/index. mxml"/>
</Applications>

6) modify the Java Homepage
Modify index. jsp in the Java Project
<! -- Set the displayed flex homepage. The URL must match the specified Homepage Address in the flex project. -->
<% Response. sendredirect (path + "/modules/common/index.html"); %>

 

11. Compile and run
1) Compile the flex Project
Select project> clean> testproject
2) copy the compilation directory bin-Debug/modules to the webroot of the Java project.
3) Start the Tomcat server in myeclipse
4) run the program
After the server is started normally, enter the following address in the browser (9090 is the server port number ):
HTTP: /localhost: 9090/testproject/modules/common/index.html

The running result is as follows:

 

This example ends with practice and seems to be a little long. The next time you add the database configuration ......

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.