柠檬学院
pkuclay的gravatar头像
pkuclay2016-11-18 11:23:37

Spring MVC最简单全面的入门学习示例

百度经验上找到的,我稍微修改了一下,附件的代码可以用eclipse或myeclipse导入,估计是最简单全面的spring mvc入门demo了。

第一步:准备包:

日志相关包

jcl-over-slf4j-1.6.1.jar
logback-classic-0.9.29.jar
logback-core-0.9.29.jar
slf4j-api-1.6.1.jar

jstl包

jstl-1.2.jar

spring 相关包
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar

以前的版本好像叫springmvc包,现在改为servlet包
org.springframework.web.servlet-3.1.1.RELEASE.jar

第二步:

在eclipse工程中建Dynamic Web project,向导式的开发,一路next,最后得到一个web工程。

在WebContent目录下生成一个index.jsp文件

 

<%@ 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>
 <title>Spring 3.0 MVC demo</title>
</head>
<body>
 <a href="hello.html">Say Hello</a>
</body>
</html>

 

在WebContent\WEB-INF目录下生成一个jsp文件夹和两个配置文件:spring-servlet.xml、web.xml

web.xml内容为

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Spring3MVC</display-name>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
     </servlet-mapping>

     <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

 

spring-servlet.xml内容为

 

<?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:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="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">
  
 <context:component-scan base-package="net.spring.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>

 

然后生成java文件HelloWorldController.java包路径为net.spring.controller

 

package net.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloWorldController {

 @RequestMapping("/hello")
 public ModelAndView helloWorld() {

  String message = "Hello World, Spring 3.0!";
  System.out.println(message);
  return new ModelAndView("hello", "message", message);
 }

}

目录结构如下图

Spring MVC最简单全面的入门学习示例

到此就完成了所有的代码和配置,所有的文档和配置加在一起只有60行左右,比较简单把。

完成后就可以放在tomcat等容器中运行一下,然后在web中打开地址。

在index.jsp上点击Say Hello,就会转向http://localhost:8080/springmvcdemo/hello.html页面显示Hello World, Spring 3.0!,表示工作正常。

示例图Spring MVC最简单全面的入门学习示例


打赏

文件名:springmvc.rar,文件大小:2531.699K下载
  • /
      • /springmvc
        • /springmvc/.classpath
        • /springmvc/.project
          • /springmvc/.settings
            • /springmvc/.settings/.jsdtscope
            • /springmvc/.settings/org.eclipse.jdt.core.prefs
            • /springmvc/.settings/org.eclipse.wst.common.component
            • /springmvc/.settings/org.eclipse.wst.common.project.facet.core.xml
            • /springmvc/.settings/org.eclipse.wst.jsdt.ui.superType.container
            • /springmvc/.settings/org.eclipse.wst.jsdt.ui.superType.name
          • /springmvc/build
              • /springmvc/build/classes
                  • /springmvc/build/classes/com
                      • /springmvc/build/classes/com/liu
                          • /springmvc/build/classes/com/liu/controller
                            • /springmvc/build/classes/com/liu/controller/HelloController.class
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友