美文网首页程序员
Simple demo for Spring MVC with

Simple demo for Spring MVC with

作者: AliceGreek | 来源:发表于2016-07-20 23:12 被阅读155次
想介绍一下我写这个demo用的环境:
Eclipse IDE || JDK 1.8.0_91 || SERVER:Tomcat v8.0

1、新建一个 Dynamic web project,右键该项目-> configure -> convert to maven project

pom.xml 配置.png
2、Add dependency in pom.xml
添加dependency.png
3、创建 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、创建 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、创建Controller class

  • 右键 Java Resourses -> src
  • new -> class
  • Package:com.name.controller
  • Filename:HelloWorld.java
Create class.png HelloWorld.java.png
6、创建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、创建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 springmvc-clean-install-maven.png

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.

tomcat-start.png

到这一步,一个简单的基于 Maven 的Spring MVC 就写好了,但是可能由于配置的细节可能还是会遇到问题,如果遇到问题,可以在下面留言,我会帮助大家解决的。


  1. The name of servlet,In addtion,name-servlet is the format of creating servlet xml file.

相关文章

网友评论

    本文标题:Simple demo for Spring MVC with

    本文链接:https://www.haomeiwen.com/subject/vitbjttx.html