SpringMvc入门教程

1.新建demo4  web项目, 导入spring包(使用的是spring4.2)

2.修改WEB-INF下的WEB.XML内容为



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
   
      
     dispatch  
     class>org.springframework.web.servlet.DispatcherServletclass>  
       
      contextConfigLocation  
      /WEB-INF/spring-servlet.xml  
       
     1  
      
      
     dispatch  
     *.do  
      
    
    index.jsp  
    
View Code

3.在WEB-INF下的新建spring-servlet.xml,内容为



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"
xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">
  
           package="com">   
              
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
          
          
          
                value="org.springframework.web.servlet.view.JstlView" />  
          
        
View Code

4.在SRC下新建 包  com.game.controller

5.在该包下新建类

package com.game.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Helloworld {

    @RequestMapping("/helloworlda")  //此处控制浏览器里访问路径 具体为:/SpringDemo/helloworld
    public void helloWorld(HttpServletRequest request, HttpServletResponse response) throws IOException {
        
        //输出字符串
        response.getWriter().append("hello world--a");

    }
    
  
    @RequestMapping("home")  
    public ModelAndView home(){  
        ModelAndView mv=new ModelAndView();
        mv.addObject("name","xiaoxiao");
         mv.getModel().put("age", "111110");
        return mv;
    }  
    
 
    
}
View Code

6.在web-inf下新建名为jsp的文件夹,并新建home.jsP文件,内容为

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'home.jsp' starting pagetitle>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    

  head>
  
  <body>
    This is my JSP page. ${age} ==${name} }<br>
  body>
html>
View Code

7.打开地址 http://localhost:8080/demo4/home.do

 

转载于:https://www.cnblogs.com/tiancai/p/6444308.html

你可能感兴趣的:(SpringMvc入门教程)