spring mvc(一)HelloWorld

Record the usage of SpringMvc4.
Environment: Myeclipse10.6+jdk7+tomcat7
Library: spring4.1.6

1. Create a project
Configure Maven-related properties in Myeclipse, and create a Maven Web project testSpringWeb.
Deploy the project to tomcat and start it.
Visit http://localhost:8080/testSpringWeb to verify the base environment.

2. Add dependency library
Add springmvc related dependencies in pom.xml.
<properties>
	<jackson.version>1.9.13</jackson.version>
	<spring.version>4.1.6.RELEASE</spring.version>
</properties>
...
<!-- javaee -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
	<version>1.2</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>javax.servlet.jsp</groupId>
	<artifactId>jsp-api</artifactId>
	<version>2.1</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>org.glassfish</groupId>
	<artifactId>javax.servlet</artifactId>
	<version>3.0.1</version>
</dependency>
<!-- spring -->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</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>
<!-- jackson -->
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-core-asl</artifactId>
	<version>${jackson.version}</version>
</dependency>
<dependency>
	<groupId>org.codehaus.jackson</groupId>
	<artifactId>jackson-mapper-asl</artifactId>
	<version>${jackson.version}</version>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.1.4</version>
</dependency>

3. Configure web.xml
Add the following configuration in web.xml, load spring and springmvc servlet at startup
<!-- spring -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
          classpath:root-context.xml
	</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- encoding filter -->
<filter>
	<filter-name>CharacterEncodingFilter</filter-name>
	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
	<init-param>
		<param-name>forceEncoding</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring mvc -->
<servlet>
	<servlet-name>spring</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
				classpath:servlet-context.xml
			</param-value>
    </init-param>
	<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
	<servlet-name>spring</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>

When spring starts, root-context.xml is loaded, and springmvc servlet loads servlet-context.xml, and parses all paths under /.

4. Spring configuration
Create a configuration file root-context.xml under src/main/resources with the following contents:
<?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:p="http://www.springframework.org/schema/p" 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
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
	default-autowire="byName" default-lazy-init="true">

</beans>

Don't use bean configuration for now.

5.springmvc configuration
Create a configuration file servlet-context.xml under src/main/resources, the content is as follows:
<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-autowire="byName" default-lazy-init="true">

	<!-- If the current request is "/", forward to "/helloWorld" -->
	<mvc:view-controller path="/" view-name="forward:/helloWorld"/>
	<!-- Static resource mapping -->
	<mvc:resources location="/resources/" mapping="/resources/**" />

	<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views
		directory -->
	<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
		<property name="order" value="1" />
	</bean>
	<!-- @ResponseBody -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
			</list>
		</property>
	</bean>
	<!-- controller -->
	<import resource="resolvers-context.xml" />
</beans>

Create a configuration file resolvers-context.xml under src/main/resources with the following contents:
<?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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
	default-autowire="byName" default-lazy-init="true">

	<!-- controller -->
	<bean name="/helloWorld" class="com.sunbin.test.testSpring.web.controller.HelloWorldController"/>	
</beans>

Configure a controller with an address of /helloWorld, the viewer page directory is /WEB-INF/jsp/, and define the home page to jump to the controller (must not have index.jsp/index.html)

6. Hello World
creates a Controller.java class
package com.sunbin.test.testSpring.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {

	public ModelAndView handleRequest(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("message", "hello world!");
		modelAndView.setViewName("helloWorld");
		return modelAndView;
	}

}

Create helloWorld.jsp in the src/main/webapp/WEB-INF/jsp directory
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>${message }
</body>
</html>

The Controller simply outputs the hello world! string on the page.

Delete the index.jsp of the home page so that the <mvc:view-controller path="/" view-name="forward:/helloWorld"/> configuration takes effect.

7. After testing the Hello World deployment project and starting tomcat, you can see the hello world! information by visiting http://localhost:8080/testSpringWeb/helloWorld http://localhost:8080/testSpringWeb/
through a browser



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326762956&siteId=291194637