Figure 7.1
Now enable Maven by right-clicking the project in the Project Explorer and selecting Configure – “Convert to Maven Project” (This is for the new m2e plugin. For the old m2eclipse plugin this is Maven – “Enable Dependency Management”) as shown in Figure 7.2
Figure 7.2
Enter all details as required and remember to make sure the packaging type to war as shown in Figure 7.3
Figure 7.3
Now write the following code in pom.xml to add Spring Dependency in project.
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring 3 dependencies -->
<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>
</dependencies>
1. Create Controller
Now create HelloWorldController in com.kruders.controller package and write the following code.
package com.kruders.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/helloworld.html")
public class HelloWorldController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Hello World!!!");
return "helloworld";
}
}
2. Create View
Create jsp folder in WEB-INF and create helloworld.jsp file in jsp folder
Now write the following code in helloworld.jsp file
${message}
3. Configuration
Create Spring Bean Configuration in WEB-INF folder and name it dispatcher-servlet.xml and add the following code.
<context:component-scan base-package="com.kruders.controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean>
4. Integrate Spring in Web App
To integrate Spring in Web Application, write the following code in web.xml
<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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
5. Add Maven Dependency
Right-Click on project and Select Properties. Now select Deployment Assembly as shown in Figure 7.4
Figure 7.4
Now Click Add and Select Java Build Path Entries and Click Next as shown in Figure 7.5
Figure 7.5
Select Maven Dependencies and Click Finish as showing in Figure 7.6
Figure 7.6
6. Run
Now run the project as shown in Figure 7.7
Figure 7.7
The folder structure of the example is shown below in Figure 7.8
Figure 7.8

No comments yet.