The Spring MVC web application context is configured in springmvcsrcmainwebappWEB-INF jboss-as-spring-mvc-context.xml
. In addition to adding the org.jboss.springmvc.controller
package for auto-detection, support for processing requests using the controller methods annotated with @RequestMapping
is configured with the <mvc:annotation-driven />
declaration. Add support for the annotation-based transaction manager; the CatalogDao
interface's persist
method is annotated with @Transactional
. Add a view resolver bean for the org.springframework.web.servlet.view.InternalResourceViewResolver
class. It is a best practice to add the view templates, such as catalog.jsp
, within the WEB-INF
directory so that only the controller has access to them. Add a bean property prefix with the /WEB-INF/views
value to resolve the view templates. The jboss-as-spring-mvc-context.xml
file is listed in the following code:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" 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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="org.jboss.springmvc.controller" /> <mvc:annotation-driven /> <!-- Add JPA support --> <bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> </property> </bean> <!-- Add Transaction support --> <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="emf" /> </bean> <!-- Use @Transaction annotations for managing transactions --> <tx:annotation-driven transaction-manager="txManager" /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:resources mapping="/static/**" location="/" /> <mvc:default-servlet-handler /> </beans>
The Spring MVC provides two levels of application contexts, the root application context for application-level services and a servlet application context for the servlet-level beans and services. The /META-INF/spring/applicationContext.xml
file is configured in web.xml
is the root-level application context, and /WEB-INF/jboss-as-spring-mvc-context.xml
is the context for DispatcherServlet
. The servlet context configuration overrides the root context for beans with the same name.
18.227.24.60