Traditional Culture Encyclopedia - Traditional stories - How to build springMVC development environment

How to build springMVC development environment

Spring MVC as SpringFrameWork products, since the day of birth, by a wide range of developers, now Spring MVC in Java can be said to be flourishing, first tell you how to build Spring MVC development environment.

(a) Preparation of the working environment:

JDK 1.7

Eclipse?Kepler

Apache Tomcat 8.0

(b) In the Eclipse new Maven project In Archetype, select "maven-archetype-webapp".

(3) Configure pom.xml.

<project?xmlns=".favccxx.favsoft</groupId>?

<artifactId>favspringmvcrestful</artifactId>?

<packaging>war</packaging>?

<version>0.0.1-SNAPSHOT</version>?

<name>favspringmvcrestful?Maven?Webapp</name>?

<url>/xml/ns/j2ee"?

xmlns:xsi="/xml/ns/j2ee

/xml/ns/j2ee/web-app_2_4.xsd">?

<display-name>favspringmvcrestful</display-name>?

<filter>?

<filter-name>encodingFilter</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>encodingFilter</filter-name>?

<url-pattern>/*</url-pattern>?

</filter-mapping>?

<listener>?

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>?

</listener>?

<servlet>?

<servlet-name>springMVC</servlet-name>?

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>?

<init-param>?

<param-name>contextConfigLocation</param-name>?

<param-value>classpath*:spring-context.xml</param-value>?

</init-param>?

<load-on-startup>1</load-on-startup>?

</servlet>?

</servlet-mapping>?

<servlet-name>springMVC</servlet-name>?

<url-pattern>/</url-pattern>?

</servlet-mapping>?

</web-app>

(v) In the resources directory, create spring-context.xml to support annotations, page path resolution, etc.

<?xml?version="1.0"?encoding="UTF-8"? >?

<beans?xmlns="component-scan?base-package="com.favccxx.favsoft.favjson.controller"></context:component-scan>?

<mvc:annotation-driven></mvc:annotation-driven>?

<bean?id="viewResolver"?class="org.springframework.web.servlet.view.UrlBasedViewResolver">?

<property?name="viewClass"?

value="org.springframework.web.servlet.view.JstlView"? />?

<property?name="prefix"?value="/WEB-INF/views"? />?

<property?name="suffix"?value=".jsp"? />?

</bean>?

</beans>

(vi) Create a new HelloController class and use annotations to complete the Spring MVC class calls.

package?com.favccxx.favsoft.favjson.controller;?

import?java.util.HashMap;?

import?java.util.Map;?

import?org.springframework.stereotype.Controller;?

import?org.springframework.web.bind.annotation.RequestMapping;?

import?org.springframework.web.bind.annotation.RequestParam;?

import?org.springframework.web.bind.annotation.RequestMapping;?

@Controller?

public?class?HelloController?{?

@RequestMapping("/greeting")?

public?ModelAndView?greeting(@RequestParam(value="name",?defaultValue="World")?String?name)? {?

System.out.println("Hello?"? +?name);?

System.out.println("Hello?"?

Map<String,?Object>?map?=?new?HashMap<String,?Object>();?

map.put("userName",?name);?

return?new?ModelAndView("/hello",map);?

}?

}

(vii) Create /WEB-INF/views/hello.jsp to present the data.

<%@?page?language="java"?contentType="text/html;?charset=UTF-8"?

pageEncoding="UTF-8"%>?

<%@?taglib?uri="/jsp/jstl/core"?prefix="c"%>?

<!DOCTYPE?html>?

<html>?

<head>?

<meta?http-equiv="Content-Type"?content="text/html;?charset=UTF-8">?

<title>Hello</title>?

</head>?

<body>?

Hello, ${userName?}?

</body>?

</html>

(viii) In the browser, enter the URL of the visit: http://localhost:8080/favspringmvcrestful/greeting?name=%E7%BE%8E%E5%A5%B3 to display Hello SpringMVC, it means that the Spring MVC development framework is built successfully.