当前位置: 代码迷 >> Web前端 >> SpringDM札记24-Using action-based web frameworks with Spring DM:SpringMVC
  详细解决方案

SpringDM札记24-Using action-based web frameworks with Spring DM:SpringMVC

热度:952   发布时间:2012-10-27 10:42:26.0
SpringDM笔记24-Using action-based web frameworks with Spring DM:SpringMVC

■ Action-based web frameworks
■ Component-driven frameworks
■ AJAX-based frameworks
■ Web services

Action-based web frameworks are responsible for selecting the appropriate actions to handle requests.Actions are responsible for extracting parameters from requests, executing the

requests, and building responses.

1. Using Spring DM with action-based frameworks

??? The central entity of action-based web frameworks is the action entity. This kind of entity is

??? responsible? for handling requests and executing functionality based on the requests, typically

??? by using external components.When configuring Spring support for this kind of framework,

??? there are two main steps:
??? (1)Specify the OSGi web application context from Spring DM to use the osgi namespace. This

??? allows you to define and reference OSGi services from within Spring application contexts.
??? (2)Inject OSGi services configured through Spring DM info the actions of the application. This

??? can be done using classic or annotation-based dependency injection.

2. Using Spring MVC with Spring DM

??? Bundles related to Spring MVC, available from the SpringSource EBR:

??? org.springframework.web.servlet :2.5.6

??? com.springsource.javax.servlet :2.5.0

??? com.springsource.javax.servlet.jsp :2.1.0

??? com.springsource.javax.servlet.jsp.jstl: 1.1.2

??? com.springsource.org.apache.taglibs.standard :1.1.2

??? spring-osgi-web :1.2.0

??? (1)CONFIGURING SPRING MVC FOR SPRING DM

??? Spring MVC uses two Spring application contexts for its configuration:
??? The first one corresponds to Spring MVC’s association with the web application; it’s used to

??? configure entities independent of web-related concerns, such as logic or business entities.

??? If you’re using Spring DM, this context is also where you’d put OSGi service references to

??? business services. To use Spring DM’s service support, we need to specify

??? OsgiBundleXmlWebApplicationContext, which is the OSGi-aware application context provided

??? by Spring DM.

?

??? The other Spring application context corresponds to the Spring frontend controller, the

??? DispatcherServlet. This context is a child context of the web application context, and it has

??? access to all artifacts configured in its parent. We can also use Spring DM at this level to

??? reference services by specifying an OSGi-aware application context class.

??? Configuring the Spring web application context:

??? <?xml version="1.0" encoding="ISO-8859-1"?>
??? <web-app 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"
?? ????? version="2.4">

?? ????? <context-param>
?? ?????????? <param-name>contextConfigLocation</param-name>
?? ?????????? <param-value>/WEB-INF/osgi-context.xml</param-value>
?? ????? </context-param>

?? ????? <context-param>
?? ?????????? <param-name>contextClass</param-name>
?? ?????????? <param-value>
?? ???? ??????????? org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext
?? ?????????? </param-value>
?? ????? </context-param>


?? ????? <listener>
?? ??????????? <listener-class>
?? ???? ???????????? org.springframework.web.context.ContextLoaderListener
?? ??????????? </listener-class>
?? ? ??? </listener>

?? ????? <servlet>
?? ?????????? <servlet-name>springmvc</servlet-name>
?? ?????????? <servlet-class>
?? ???? ?????????? org.springframework.web.servlet.DispatcherServlet
?? ?????????? </servlet-class>
?? ?????????? <load-on-startup>2</load-on-startup>
?? ?????????? <init-param>
?? ???? ???????????? <paramname>contextClass</param-name>???????????????????????????????????????????????????????????????????????

???????????????????? <param-value>

?????????????????????????? org.springframework.osgi.web.context.support

?????????????????????????? .OsgiBundleXmlWebApplicationContext</param-value>
?? ?????????? </init-param>

?? ????? </servlet>

? ?? ??? <servlet-mapping>
?? ???????????? <servlet-name>springmvc</servlet-name>
?? ???????????? <url-pattern>*.do</url-pattern>
? ?????? </servlet-mapping>
??? </web-app>

??? springmvc-servlet.xml:

??? <?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:context="http://www.springframework.org/schema/context"
???????????? xsi:schemaLocation="http://www.springframework.org/schema/beans
???????????? http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
??????? ???? http://www.springframework.org/schema/context
???????????? http://www.springframework.org/schema/context/spring-context-2.5.xsd">

???????????? <context:component-scan base-package="com.manning.sdmia.web.springmvc"/>

???????????? <bean id="viewResolver"????

????????????????? class="org.springframework.web.servlet.view.InternalResourceViewResolver">
?????? ? ? ? ? ?? <property name="prefix" value="/WEB-INF/jsp/"/>
????????????????? <property name="suffix" value=".jsp"/>
???????????? </bean>
???????????? <bean class="org.springframework.osgi.extensions.annotation

????????????????? .ServiceReferenceInjectionBeanPostProcessor"/>
???? </beans>

??? (2)USING ANNOTATION-BASED SUPPORT

??? Configuring Spring MVC entities:

??? <context:component-scan base-package="com.manning.sdmia.web.springmvc"/>

??? <!--Activates annotationbased injection for services-->
??? <bean class="org.springframework.osgi.extensions.annotation

???????????? .ServiceReferenceInjectionBeanPostProcessor"/>
??? <bean id="viewResolver"????

???????????? class="org.springframework.web.servlet.view.InternalResourceViewResolver">
??????????? <property name="prefix" value="/WEB-INF/jsp/"/>
??????????? <property name="suffix" value=".jsp"/>
??? </bean>

??? Implementing the Spring MVC controller:

??? @Controller
??? public class ContactsController {
?????????? private ContactService contactService;
?????????? @ServiceReference
?????????? public void setContactService(ContactService contactService) {
???????????????? this.contactService = contactService;
?????????? }

?????????? @RequestMapping("/contacts.do")
?????????? public ModelMap getContacts() {
???????????????? List<Contact> contacts = contactService.getContacts();
???????????????? return new ModelMap("contacts", contacts);
?????????? }
?? }

?? 需Import的Package:

?? Import-Package: (...)
?? org.springframework.stereotype;version="2.5.6",
?? org.springframework.ui;version="2.5.6",
?? org.springframework.web.bind.annotation;version="2.5.6",
?? org.springframework.web.servlet.view;version="2.5.6"

?? (3)CONFIGURING AND USING JSTL

?? Packages to import for Spring MVC:

?? Import-Package: (...)
?? javax.servlet.http;version="2.4.0",
?? javax.servlet.resources;version="2.0.0",
?? javax.servlet.jsp;version="2.0.0",
?? javax.servlet.jsp.jstl.core;version="1.1.2",
?? javax.servlet.jsp.jstl.fmt;version="1.1.2",
?? javax.servlet.jsp.jstl.tlv;version="1.1.2",
?? org.apache.taglibs.standard.resources;version="1.1.2",
?? org.apache.taglibs.standard.tag.common.core;version="1.1.2",
?? org.apache.taglibs.standard.tag.rt.core; version="1.1.2",
?? org.apache.taglibs.standard.tei;version="1.1.2",
?? org.apache.taglibs.standard.tlv;version="1.1.2"

?? Implementing a JSTL view in a JSP file:

?? <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
?? <html>
????? <body>
????????? (...)
????????? <c:forEach items="${contacts}" var="contact">
?????????????? <c:out value="${contact.firstName}"/>
?????????????? <c:out value="${contact.lastName}"/>
????????? </c:forEach>

????????? (...)
?????? </body>
?? </html>

3. MVC 2 pattern

??? The MVC pattern aims to keep responsibilities separate when implementing UIs. The model???

??? corresponds to the data to be displayed, and the view to the UI. The controller intercepts

??? user requests, executes business services, and then forwards the results to the view.

???
??? Although this pattern is convenient, it can be cumbersome to implement. That’s why a new

??? version has been developed based on a controller called the front controller, which is

??? responsible for forwarding requests to the right handling entity. The rest of the pattern

??? remains as it was before. All current Java web frameworks base their implementations
??? on the second version of this pattern.

1 楼 yuanyuan110_l 2012-05-03  
请问能给个这个工程的目录结构吗?如果能给个例子更好,我按照这个上面说的,没有弄好。谢谢了。
2 楼 yuanyuan110_l 2012-05-04  
忘记给邮箱了,yuanyuan110.l@gmail.com
3 楼 SpringsFeng 2012-05-04  
yuanyuan110_l 写道
忘记给邮箱了,yuanyuan110.l@gmail.com

请直接参考Eclipse Virgo项目的Samples Demo:
http://www.eclipse.org/virgo/samples/
这个例子比较完善。
  相关解决方案