[英]Spring MVC xml configuration
我正在嘗試在xml中配置Spring MVC,因為我現在還不想使用配置類。
我認為有一些缺少的東西,因為如果刪除以下配置類,則無法使用:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
public class Config {
}
這是我的webmv-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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="es.webtools.eencuesta.api.config.HibernateAwareObjectMapper" />
</property>
</bean>
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000" />
</bean>
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/thymeleaf/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="cacheable" value="false" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="viewNames" value="**" />
</bean>
組件掃描位於applicationContext.xml文件中:
版本:Spring:3.2.0.RELEASE Spring MVC:3.2.0.RELEASE Thymeleaf:2.1.4
有誰知道我缺少什么xml條目?
我不是專家,但是您可以嘗試告訴Spring MVC servlet配置文件在哪里。 在web.xml中的servlet配置中,添加一個初始化參數:
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/webmv-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
如果您打算為Spring MVC項目使用XML配置,則可以使用SimpleUrlHandlerMapping Spring類將URL與控制器映射。 以下是代碼段:
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<!-- http://localhost:8080/(Project_Name)/(URL_Mapping_as_below eg: index , wel and hello in the below example) is the URL context -->
<props>
<prop key="/index.html">WelcomeController</prop>
<prop key="/wel.html">WelcomeController</prop>
<prop key="/hello.html">WelcomeController</prop>
</props>
</property>
</bean>
<bean id="WelcomeController" class="com.kl.controllers.WelcomeController" />
在這里,我已將3個URL映射到單個Controller,並且不需要使用config類或批注來完成此工作。
我有一個正在運作的項目(基本上是我上面所附的代碼段的整個項目)。 { https://youtu.be/Vm4XFrWjDhE }源代碼在視頻的描述中。
干杯..
標記<mvc:annotation-driven>是在春季3中引入的,因此默認的處理程序和轉換器可自動用於您的mvc應用程序,[請參見第17.16.1節在鏈接中啟用MVC Java Config或MVC XML命名空間]。因此,如果您真的想在春季制作不帶注釋的Web應用程序,則可以選擇。
要么
2.這不是要奪走您的強大力量,而是要進一步普及DI / IoC風格的彈簧。 由於魔術是由MVC框架中的各個類(例如json轉換器)完成的,因此,無論您是手動聲明其init和注入還是以其他方式聲明它,都不會真正影響您的學習,除非有人從純j2ee到spring: )Njy。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.