1. 程式人生 > >SpringMvc學習第一天

SpringMvc學習第一天

h+ 轉發 play tomcat ctype 第一天 3-9 位置 contex

1.環境搭配:MyEclipse10.7,tomcat9.0 jdk1.8

2.目錄結構:

技術分享圖片

3.運行效果:

技術分享圖片 技術分享圖片

頁面代碼:

web.xml

<?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> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class
> <!-- 配置 DispatcherServlet的一個初始化參數:配置SpringMvc 配置文件的位置和名稱--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 也可以不通過contextConfigLocation 來配置SpringMvc的配置文件,而使用默認的 默認的配置文件為:
/WEB-INF/<servlet-name>-servlet.xml /WEB-INF/springDispatcherServlet-servlet.xml --> <!-- 設置啟動 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <!-- 請求處理 --> <servlet-name>springDispatcherServlet</servlet-name> <!-- /:應答所有請求 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 
         xmlns:aop="http://www.springframework.org/schema/aop"
 
       xmlns:mvc="http://www.springframework.org/schema/mvc"
 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
       xmlns:context="http://www.springframework.org/schema/context" 
 
       xmlns:p="http://www.springframework.org/schema/p"
 
  xsi:schemaLocation="
 
              http://www.springframework.org/schema/beans    
 
                 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     
              http://www.springframework.org/schema/mvc      
 
              http://www.springframework.org/schema/tx/spring-mvc-4.0.xsd
 
               http://www.springframework.org/schema/context
 
               http://www.springframework.org/schema/context/spring-context-4.0.xsd
 
               http://www.springframework.org/schema/aop
 
             http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
             
             <!-- 配置自定義掃描包 -->
             <context:component-scan base-package="como.springmvc.handlers"></context:component-scan>
           <!-- 配置視圖解析器:如何把handler方法返回值解析為實際的物理視圖 -->
           
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                       <!-- 前綴 -->
                       <property name="prefix" value="/WEB-INF/views/"></property>
                       <!-- 後綴 -->
                       <property name="suffix" value=".jsp"></property>
           </bean>
           </beans>
 

HelloWorld.java

package como.springmvc.handlers;

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


@Controller
public class HelloWorld {
    /*1.使用@RequestMapping 註解來映射請求的URL,與index.jsp中的請求一致
    2.返回值會通過視圖解析器解析為實際的物理視圖,對於InternalResourceViewResolver視圖解析器
        會做如下解析:
        通過prefix  +  returnVal +後綴  這樣的方式得到實際的物理視圖,然後做轉發操作
        /WEB-INF/views/+success+.jsp
    
    */
    @RequestMapping("/helloworld")
            public String hello(){
                System.out.println("hello world");
                return "success";
            }
}

success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 success.jsp starting page</title>
    
  </head>
  
  <body>
   success page. <br>
  </body>
</html>

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 index.jsp starting page</title>
    
  </head>
  
  <body>
     <!--@RequestMapping("/helloworld")  -->
    <a href="helloworld">hello world</a>
  </body>
</html>

前幾天看了一點Spring,現在開始接觸SpringMvc,學的有點急,過程還不是很懂,慢慢來吧。。。。。。

SpringMvc學習第一天