想介紹一下我寫這個demo用的環(huán)境:
Eclipse IDE || JDK 1.8.0_91 || SERVER:Tomcat v8.0
1尼酿、新建一個 Dynamic web project,右鍵該項目-> configure -> convert to maven project
2搀继、Add dependency in pom.xml
3徙融、創(chuàng)建 web.xml
<pre><code>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>AliceSpringMVCTutorial</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>alice</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>alice</servlet-name>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app></code></pre>
The above code in web.xml will map DispatcherServlet with url pattern /welcome.jsp. Also note that we have define index.jsp
as welcome file.
4积担、創(chuàng)建 name[1]-servlet.xml
<pre><code><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<context:component-scan base-package="com.name.controller" />
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans></code></pre>
In the above name-servlet.xml configuration file, we have defined a tag <context:component-scan>. This will allow Spring to load all the components from package com.name.controller and all its child packages.
5承二、創(chuàng)建Controller class
- 右鍵 Java Resourses -> src
- new -> class
- Package:com.name.controller
- Filename:HelloWorld.java
6凹蜂、創(chuàng)建index.jsp Path: WebContent/index.jsp
<pre><code>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Index Page</title>
</head>
<body>
<div style="text-align: center">
<h2>Hi,this is ur first Spring MVC Tutorial.</h2>
<h3><a href="welcome.html">Check here to see Welcome message ...</a> to check Spring MVC controller @RequestMapping("/welcome")</h3>
</div>
</body>
</html></code></pre>
7踊跟、創(chuàng)建welcome.jsp Path:WebContent/WEB-INF/jsp/welcome.jsp
<pre><code>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
${message}
</body>
</html></code></pre>
8、右鍵Project -> Run as -> Maven Build
9涉茧、
*If you don't see Tomcat Server in Servers tab then follow steps to add Apache Tomcat to Eclipse.
*Deploy project to Apache Tomcat and start tomcat.
到這一步赴恨,一個簡單的基于 Maven 的Spring MVC 就寫好了,但是可能由于配置的細(xì)節(jié)可能還是會遇到問題伴栓,如果遇到問題伦连,可以在下面留言,我會幫助大家解決的钳垮。
-
The name of servlet,In addtion,name-servlet is the format of creating servlet xml file. ?