閱讀957 返回首頁    go 微軟 go Office


Spring MVC學習筆記之Hello World

            

            

            

            

            

3、配置web.xml文件,這裏直接貼完整的web.xml文件,裏麵會有一些注釋:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns="https://java.sun.com/xml/ns/javaee" xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  version="3.0">
  <display-name>Spring MVC</display-name>
  
  <servlet>
  	<!-- 引入Spring MVC核心過濾器 -->
  	<servlet-name>springDispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 初始化參數 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	<!-- 啟動時加載 -->
  	<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="https://www.springframework.org/schema/beans" xmlns:aop="https://www.springframework.org/schema/aop"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="https://www.springframework.org/schema/mvc" xmlns:context="https://www.springframework.org/schema/context"
	xsi:schemaLocation=" 
	https://www.springframework.org/schema/aop 
    https://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	https://www.springframework.org/schema/beans
	https://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	https://www.springframework.org/schema/context 
	https://www.springframework.org/schema/context/spring-context-4.0.xsd
	https://www.springframework.org/schema/mvc  
    https://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> 
    <!-- 配置自動掃描注解的包 -->
    <context:component-scan base-package="com.watimer.springmvc"></context:component-scan>
    
    <!-- 配置視圖解析器 -->
    <!-- 
    	視圖解析器的作用為:當請求成功時,跳轉到“視圖解析器前綴+返回值+視圖解析器後綴界麵”
    				以此項目為例,則跳轉到WEB-INF/views/success.jsp界麵
    -->
    <bean >
    	<property name="prefix" value="/WEB-INF/views/"></property>
    	<property name="suffix" value=".jsp"></property>
    </bean>
</beans>



5、在SRC下建包,注意與springmvc.xml中配置的要相同,這裏為com.watimer.springmvc,同時建立類Hello,貼代碼:

  Hello.java

package com.watimer.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 * @author 易訊.嶽耀櫟
 */
//掃描Controller
@Controller
public class Hello {
	//@RequestMapping對應的是請求路徑,會在下文中指出
	@RequestMapping("/hello")
	public String hello(){
		System.out.println("hello world");
		//請求成功後,根據上文配置的視圖解析器,會跳轉到success.jsp界麵
		return "success";
	}
}



   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>
	<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">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<!-- 這裏的請求,對應的是@RequestMapping("/hello"),如果不一致,則請求不到-->
    <a href="hello">helloworld</a>
  </body>
</html>



7、請求界麵

617f72c1eab49b412e834b52718e199823187efe


8、點擊後的界麵

2825b9c0d29a6050c4d08a494ebccbd9a6408c64


8、控製台的輸出

c83485dee92ab6b540eb9677d49877ad82343d6a



到此,第一個項目就完成了,項目源碼可供下載

最後更新:2017-08-13 22:25:42

  上一篇:go  了解人工智能之基礎概念-基本概念問答
  下一篇:go  【雲周刊】第134期:阿裏雲發布ECS企業級產品家族 19款實例族涵蓋173個應用場景