springmvc20170322

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <display-name></display-name>    
  
  
   <!--  配置 springmvc的核心控制器    
     这里的 <servlet-name>springmvc</servlet-name>
     springmvc就是规定了 我们springmvc的配置文件的名称
     <servlet-name>-servlet.xml
      -->
     <servlet> 
     <servlet-name>springmvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>
     
     <servlet-mapping> 
     <servlet-name>springmvc</servlet-name>
     <url-pattern>/</url-pattern> 
     <!--  为什么不能配置/*
      我们打开浏览器 第一次访问  项目名/hello ,会通过我们的handlerMaping找 有没有bean name="/hello"!
      有就进入对应的controller进行操作,之后根据controller中返回的ModelAndView ===》hello01!
     让试图解析器进行解析 :解析之后 变成了/SpringMVC001/WEB-INF/jsp/hello01.jsp
     
     如果我们配置的是 /*  那么解析之后的路径也会被当成一个Bean  name来处理,很显然我们没有这个bean,所以会报错!
     
     /: 只会匹配  /login    /index   不能匹配  /*.jsp 这样的后缀url
     /*:匹配所有的请求 所以我们hello01.jsp 也会被拦截
       -->
     </servlet-mapping>
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation=" http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--  采用默认的方式  BeanNameUrlHandlerMapping -->
 <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
 
 <!-- name="/hello" 用户的请求   class就是我们的Controller处理器-->
 <bean name="/hello01" class="cn.rick.controller.HelloController"/>

<!-- 视图解析器         项目名/web-inf/jsp/springmvc.jsp  需要设置前缀 和后缀 -->   
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>

</bean>

</beans>
springmvc-servlet.xml
package cn.rick.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class HelloController extends AbstractController { // 第一个springmvc小例子
    /**
    * handleRequestInternal():对应struts2的 execute()
    * ModelAndView:返回值   需要核心配置文件中的试图解析器 解析
    */
    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        System.out.println("第一个springmvc小例子....");
        // hello01就是我们返回的视图名称 但是 现在只是一个字符串
        return new ModelAndView("hello01");
    }

}
HelloController.java
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    成功
</body>
</html>
hello01.jsp
原文地址:https://www.cnblogs.com/xtdxs/p/6597850.html