Improvement @JVerstry Answer .. I am adding Another solution not explained here in steps.
Create a new Maven project at Netbeans. Then copy your source code to the maven folder, this can be done in Netbeans IDE Projects View Itself. Then do the following.
- Add SPRING MVC Dependencies.
- Add the SPRING version of MVC to your (depending on point 1)
- Set up dispatcher-servlet.xml and web.xml in the SPRING folder.
- Rest is to do some coordination and tuning of the main controller and the JSP / HTML page.,
- check your homepage by running.
Point 1 :
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency>
Point 2 : add spring.version to your properties section
<properties> <spring.version>4.0.2.RELEASE</spring.version> </properties>
Point 3 . In the WEB-INF folder, create a file called dispatcher-servlet.xml . Open the file and copy the following code.
<?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" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.youbequityweb.controllers" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
announces mvc support with annotation support using @Controller, @Service, @Component.
means scanning classes from this base package to determine all bean classes.
The view absorber locates our views (jsp) and extensions. In web.xml add the SPRING configurations inside the web application .
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet- class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Point 4 . It's complicated, now bind the existing main controller to the base package defined in dispatcher-servlet.xml . for example: HomeController.java .
package com.youbequityweb.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HomeController { @RequestMapping(value="/home", method = RequestMethod.GET) public String viewHome(){ return "home"; } }
Point 5 . In NetBeans, right-click, select โCleanโ and โCreateโ, exit if there are any warnings and conflicts, and then run the SPRING mvc application to get the results.