Reputation: 134
I am trying to configure Spring MVC in xml because I do not want (yet) use config classes.
I think there is something missing beacuse I does not work if I remove the following config class:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
public class Config {
}
Here is my 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>
Component scan is in applicationContext.xml file:
Versions: Spring: 3.2.0.RELEASE Spring MVC: 3.2.0.RELEASE Thymeleaf: 2.1.4
Does anyone knows what xml entry am I missing?
Upvotes: 1
Views: 3236
Reputation: 121
If your intention is to use a XML configuration for your Spring MVC project, then you can use a SimpleUrlHandlerMapping Spring class to map your URL with the controller. Below is a snippet:
<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" />
Here i have mapped 3 URL's to a single Controller, and i don't need to use a config class or annotations to make this work.
I have a working project (basically the whole project of the snippet i have attached above). {https://youtu.be/Vm4XFrWjDhE} The source code is in the video's description.
Cheers ..
Upvotes: 2
Reputation: 1632
I'm not an expert but you can try to tell Spring MVC servlet where is the configuration file. In your servlet configuration in you web.xml add a init param:
<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>
Upvotes: 2
Reputation: 84
the tag < mvc:annotation-driven> was introduced in spring 3 so that the default handlers and converters are automatically available to your mvc application, [ check section 17.16.1 Enabling the MVC Java Config or the MVC XML Namespace in the link] .So if you really want to do a web app in spring without annotation here are the choices .
or
2.Its not that is taking away great power from you ,all its doing is taking further the DI/IoC style spring popularized . Since the magic will be done by respective classes in MVC framework (say a json converter),it doesn't really affect your learning ,whether you declare their init and injection manually or otherwise , unless ofcourse someone is coming from pure j2ee to spring :) Njy.
Upvotes: 1