(一)SpringMVC

第一章 问候SpringMVC

第一节 SpringMVC简介

  SpringMVC是一套功能强大,性能强悍,使用方便的优秀的MVC框架

  下载和安装Spring框架:

    登录http://repo.springsource.org/libs-release-local/

    选择org->springframework->spring,然后选择一个版本下载,可以是4.2.0版本

  除此之外,Spring的核心容器还必须依赖commons-logging的jar包

    登录http://commons.apache.org/

    选择Releases->Logging,下载commons-logging工具,下载好了解压将commons-logging-1.2.jar添加到项目的类加载路径中

第二节 SpringMVC版HelloWorld实现

导入对应的jar包:

web.xml: 1 <?xml version="1.0" encoding="UTF-8"?>

 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     id="WebApp_ID" version="2.5">
 6     <display-name>SpringMvc01</display-name>
 7     <welcome-file-list>
 8         <welcome-file>index.html</welcome-file>
 9     </welcome-file-list>
10 
11     <servlet>
        <!-- Servlet的名称 --> 12 <servlet-name>springmvc</servlet-name>
        <!-- Serlvet对应的java类 --> 13 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 当前Servlet的参数信息 --> 14 <init-param>
          <!-- contextConfigLocation是参数名称,该参数的值包含SpringMVC的配置文件路径 --> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:spring-mvc.xml</param-value> 17 </init-param> 18 </servlet>
      <!-- Servlet映射声明 --> 19 <servlet-mapping>
        <!-- 请求对应的Servlet的名称 --> 20 <servlet-name>springmvc</servlet-name>
        <!-- 监听当前域的所有请求 --> 21 <url-pattern>*.do</url-pattern> 22 </servlet-mapping> 23 </web-app>

index.html:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 <a href="helloWorld.do">问候SpringMvc</a>
 9 </body>
10 </html>

spring-mvc.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd">
11 
12     <!-- 使用注解的包,包括子集 -->
     <!-- Spring可以自动去扫描base-pack下面的包或者子包下面的java文件,如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean --> 13 <context:component-scan base-package="com.wishwzp"/> 14 15 <!-- 视图解析器 --> 16 <bean id="viewResolver" 17 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 18 <property name="prefix" value="/WEB-INF/jsp/" /> 19 <property name="suffix" value=".jsp"></property> 20 </bean> 21 22 </beans>

HelloWorldController.java:

 1 package com.wishwzp.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.ui.Model;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 @Controller
 8 public class HelloWorldController {
 9 
10     @RequestMapping("/helloWorld")
11     public String helloWorld(Model model){
12         model.addAttribute("message", "StringMvc你好!");
13         return "helloWorld";
14     }
15 }

helloWorld.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 ${message }
11 </body>
12 </html>
原文地址:https://www.cnblogs.com/wishwzp/p/5654159.html