Spring MVC的配置与使用

首先将Spring包导入WebContent/WEB-INF/lib 文件夹中

Spring MVC的配置与使用_第1张图片

 

Spring Web 配置文件 web.xml 的内容

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" id="WebApp_ID" version="3.0">
  <display-name>Spring MVC Applicationdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  
  <servlet>
      <servlet-name>HelloWebservlet-name>
          <servlet-class>
              org.springframework.web.servlet.DispatcherServlet
          servlet-class>
          <load-on-startup>1load-on-startup>
  servlet>
  
  <servlet-mapping>
      <servlet-name>HelloWebservlet-name>
      <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

另一个 Spring Web 配置文件 HelloWeb-servlet.xml 的内容

<beans xmlns="http://www.springframework.org/schema/beans"
   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-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="com.tutorialspoint" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/" />
      <property name="suffix" value=".jsp" />
   bean>

beans>

HelloController.java 文件的内容:

package com.tutorialspoint;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping(method=RequestMethod.GET)
    public String printHello(ModelMap model) {
        model.addAttribute("message","Hello Sping MVC Framework");
        return "hello";
    }
}

视图文件 hello.jsp 的内容

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<title>Hello Worldtitle>
head>
<body>
   <h2>${message}h2>
body>
html>

 

 然后访问web页面

Spring MVC的配置与使用_第2张图片

 

你可能感兴趣的:(Spring MVC的配置与使用)