Spring Mvc+blzeds+flex Framework Practice: HelloWorld Chapter

Source: Internet
Author: User
Tags aop create index getmessage java web server port tomcat server

First, the environment configuration:
MyEclipse7.0
Felx Builder3.0
JDK1.6
Flex sdk3.2
Tomcat6.0

Ii. creating Java Web projects
Create a Web Project:testproject in MyEclipse
Copy the associated jar package to web-inf/lib and introduce it into the project's build path

Third, the creation of Flex engineering

Create Flex Project:testproject in Flex Builder

Iv. Configuration of Web.xml
Configuring Webroot/web-inf/web.xml in Java WEB Engineering
< add schema file--> <web-app version= "2.4" xmlns= "HTTP://JAVA.SUN.COM/XML/NS/J2EE" xmlns:xsi= "http://www.w3.org" /2001/xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ Web-app_2_4.xsd "> <!--Specify Welcome page--> <welcome-file-list> <welcome-file>index.jsp</welcome-file > </welcome-file-list> <!--specifying initialization files--> <context-param> <param-name>contextconfiglocation </param-name> <param-value> classpath:ApplicationContext.xml, Classpath:application-flex.xml </ Param-value> </context-param> <!--add listener--> <listener> <listener-class> Org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> Org.springframework.security.ui.session.HttpSessionEventPublisher </listener-class> </listener> <!--add servlet configuration--> <servlet> <servlet-name>flex</servlet-name> < Servlet-class>org.springframework.web.servlet.dispatcherservlet </servlet-class> <load-on-startup>1</ load-on-startup> </servlet> <servlet-mapping> <servlet-name>flex</servlet-name> < Url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <!--add Session configuration--> < Session-config> <session-timeout>30</session-timeout> </session-config> <!--add schema file--> <web-app version= "2.4" xmlns= http://java.sun.com/xml/ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_ 2_4.xsd "> <!--Specify Welcome page--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ welcome-file-list> <!--Specify initialization file--> <context-param> <param-name>contextconfiglocation</ Param-name> <param-value> classpath:ApplicationContext.xml, Classpath:application-flex.xml </ Param-value> &Lt;/context-param> <!--add listener--> <listener> <listener-class> Org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> Org.springframework.security.ui.session.HttpSessionEventPublisher </listener-class> </listener> <!--add servlet configuration--> <servlet> <servlet-name>flex</servlet-name> < Servlet-class>org.springframework.web.servlet.dispatcherservlet </servlet-class> <load-on-startup >1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>flex</servlet-name > <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <!--add Session configuration--> <session-config> <session-timeout>30</session-timeout> </session-config>

V. Create Applicationcontext.xml, Application-flex.xml
Normal 0 7.8 lb 0 2 false false MicrosoftInternetExplorer4 configured the initialization profile in Web.xml (Applicationcontext.xml, Applicatio N-flex.xml), you need to create the corresponding XML file in the SRC directory (the file name needs to be the same as the profile name set in Web.xml, otherwise it will be an error when loading).

Vi. Configuration of Applicationcontext.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:flex=" Http://www.springframework.org/schema/flex "xmlns:aop=" http:// Www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:// Www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/flex http:// Www.springframework.org/schema/flex/spring-flex-1.0.xsd "> <aop:aspectj-autoproxy/> <bean id=" Timingaspect "class=" Org.perf4j.log4j.aop.TimingAspect "/> </beans>

VII. Spring MVC Configuration
Using the Spring MVC Framework, you will use the two profiles applicationcontext.xml, [servlet-name]-servlet.xml
Web-inf/[servlet-name]-servlet.xml, which is used to configure the Web tier, the view parser, the controller, the validator, and all other MVC frameworks that are required

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 Flex-servlet.xml under Web-inf.

Configure Flex-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: flex= "Http://www.springframework.org/schema/flex" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/flex http://www.springframework.org/schema/flex/ Spring-flex-1.0.xsd "> </beans>

Eight, spring+blazeds configuration
BlazeDS is a server-side technology that uses Java for Web communications, enabling developers to easily connect to back-end distributed data, push data to flex or air applications, and provide a better experience for RIA (Rich Internet application).
The spring integration blazeds exposes the spring bean to the blazeds service. Before spring blazeds integration, blazeds needed to write code to access the bean of spring. With spring blazeds integration, when Flash accesses the server, spring sends the request to flex message broker. At the same time, we expose some of the beans in spring to flex Remoting service, so flash can invoke the bean that is exposed to flex remoting service in spring.
The main blazeds component is messagebroker. The incoming HTTP information from the flex end will trigger the spring-managed Messagebroker through spring dispatcherservlet. That means spring uses Dispatcherservlet to manage BlazeDS's messagebroker.
Configure Application-flex.xml
<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xmlns:flex=" Http://www.springframework.org/schema/flex "xmlns:aop=" http:// Www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:// Www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http:// Www.springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/flex http:// Www.springframework.org/schema/flex/spring-flex-1.0.xsd "> <!--use spring message broker instead of Blazeds--> < Flex:message-broker>

Nine, blazeds configuration
1) Download BlazeDS
Download Address: Http://opensource.adobe.com/wiki/display/blazeds/Release+Builds

2 extract Blazeds.zip, extract the Blazeds.war in which the Flex directory of four XML files (messaging-config.xml, Proxy-config.xml, Remoting-config.xml, services-config.xml) Copy to Java Engineering Webroot/web-inf/flex (the Flex directory needs to be created manually) Flex engineering src/config (config directory needs to be created manually)

3) Modify Flex Engineering Src/config/services-config.xml
<?xml version= "1.0" encoding= "UTF-8"?> <services-config> <services> <service-include file-path= "Remoting-config.xml"/> <service-include file-path= "Proxy-config.xml"/> <service-include file-path= " Messaging-config.xml "/> </services> <security> <login-command class=" Flex.messaging.security.TomcatLoginCommand "server=" Tomcat "/> </security> <channels> < Channel-definition id= "MY-AMF" class= "Mx.messaging.channels.AMFChannel" > <endpoint url= "/testweb/" MESSAGEBROKER/AMF "class=" Flex.messaging.endpoints.AMFEndpoint "/> </channel-definition> < Channel-definition id= "MY-SECURE-AMF" class= "Mx.messaging.channels.SecureAMFChannel" > <endpoint url= " Testweb/messagebroker/amfsecure "class=" Flex.messaging.endpoints.SecureAMFEndpoint "/> <properties> < Add-no-cache-headers>false</add-no-cache-headers> </properties> </channel-definition> < Channel-definition id= "MY-POLLING-AMF" Class= "Flex.mx.messaging.channels.AMFChannel" > <endpoint url= "/testweb/messagebroker/amfpolling" Flex.messaging.endpoints.AMFEndpoint "/> <properties> <polling-enabled>true</polling-enabled > <polling-interval-seconds>4</polling-interval-seconds> </properties> </ channel-definition> </channels> <logging> <target class= "Flex.messaging.log.ConsoleTarget" level= "Error" > <properties> <prefix>[blazeds] </prefix> <includedate>false</includedate > <includeTime>false</includeTime> <includeLevel>false</includeLevel> < includecategory>false</includecategory> </properties> <filters> <pattern>endpoint.* </pattern> <pattern>Service.*</pattern> <pattern>Configuration</pattern> </ filters> </target> </logging> <system> <redeploy><enabled>false</enabled> </redeploy> </system&Gt </services-config>

X. Application of the framework
1) Create Userservice.java
After framing, you need to create Java classes in Java Engineering and publish them to flex applications through a configuration file to verify that Java and Flex communication is successful
Create Package:com.admin.service under Java Engineering src, create Userservice.java

Package com.admin.service;
public class UserService {
Public String GetMessage () {
System.out.println ("Excute userservice:" + "Hello world!");
Return to "Hello World";
}
}

2) Release UserService
All Java classes, if they want to be published to Flex applications, need to add <flex:remoting-destination> nodes to the Application-flex. Add the following configuration to the Application-flex.xml:
<bean id= "UserService" class= "Com.hand.admin.service.UserService" >
<flex:remoting-destination/>
</bean>

3 Flex Reference UserService
For all remote services published by the Java project, if called, you must configure in the Flex Engineering configuration file Remoting-config.xml to add the following configuration in Remoting-config.xml ( The source must be the same as the Bean ID provided when the remote service is published in the Java project, otherwise the call will fail:

<destination id= "UserService" >
<properties>
<source>UserService</source>
</properties>
</destination>
4) Creating Flex applications
Create the Modules/common directory under Flex engineering SRC, create index.mxml in which to invoke UserService
<?xml version= "1.0" encoding= "Utf-8"?> <mx:application xmlns:mx= "Http://www.adobe.com/2006/mxml" Vertical "horizontalalign=" center "creationcomplete=" Initapp () "fontsize=" > <mx:Script> <!--[cdata[ Import mx.rpc.events.ResultEvent; Import Mx.controls.Alert; Private Function Initapp (): void{userservice.getmessage (); UserService.getMessage.addEventListener (Resultevent.result, Getmessagehandler); Private Function Getmessagehandler (evt:resultevent): void{UserService.getMessage.removeEventListener ( Resultevent.result,getmessagehandler); Inittext.text = Evt.result as String; }]]--> </mx:Script> <mx:remoteobject id= "UserService" destination= "UserService"/> <mx:label "text=" Welcome "id=" Inittext "fontweight=" bold "/> </mx:Application>

5) Modify the Flex main application
In particular, it is necessary to mention here that a main application testproject.mxml is provided by default when the Flex project is created, and when we want the main application to be determined by ourselves (in this case the main application is Moduls/common/index.mxml), You need to modify the. actionscriptproperties file in the Flex Engineering directory. Modify the following configuration
<applications>
<application path= "Testflex.mxml"/>
<application path= "Modules/common/index.mxml"/>
</applications>

6) Modify the Java home page
Modifying the index.jsp in Java Engineering
<!--Set the Flex home page to display, URL needs to match the specified home address under Flex Project-->
<%response.sendredirect (path+ "/modules/common/index.html");%>

Xi. Compiling and running
1) Compiling Flex Engineering
Select Project->clean->testproject
2 Copy compilation directory Bin-debug/modules to Java engineering Webroot
3 start Tomcat server in MyEclipse
4) Running the program
After the server starts normally, enter the following address in the browser (where 9090 is the server port number):
Http://localhost:9090/TestProject/modules/common/Index.html
The results of the operation are as follows:

This example to this end of practice, seems a bit long, the next time on this basis to 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.