Spring MVC系列[1]—— HelloWorld

1.导入jar包

  ioc

  mvc

  复制spring-mvc.xml到src目录下。

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring-mvc.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

3.spring-mvc.xml

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xsi:schemaLocation="         
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd         
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd        
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd         
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd         
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd         
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd        
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd         
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd        
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 
	
	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.java"/>
	
	<!-- 开启mvc注解扫描 -->
	<mvc:annotation-driven/>
	
	<!-- 定义视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/"/>
		<property name="suffix" value=".jsp"/>
	</bean>

</beans>

4.建立Controller类

 1 package com.java.springmvc;
 2 
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.servlet.ModelAndView;
 9 
10 @Controller
11 public class HelloWorld {
12     
13     @RequestMapping("/hello.do")
14     public ModelAndView hello(
15             HttpServletRequest request,
16             HttpServletResponse response)
17     throws Exception{
18         System.out.println("hello world");
19         return new ModelAndView("jsp/hello");
20     }
21     
22     @RequestMapping("say.do")
23     public String say(){
24         
25         System.out.println("hello contorller");
26         return "jsp/hello";
27     }
28     
29 }
Controller类

5.在WEB-INF目录下建立jsp目录,建立新的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 'hello.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>
    Hello World! <br>
  </body>
</html>
View Code

 

6.总结

①在xml中指定src目录下的文件用classpath。

WEB-INF是安全目录。所谓「安全目录」,就是客户端不能访问,但是服务器可以访问的目录。

③创建bean时,容易出现的错误:class=“.class”.应该是class=“”(不带.class)。

④Controller返回应该使用相对路径而不是绝对路径。

⑤MVC基本概念

  M model
    业务逻辑和数据:实体,dao,service
   V view
    jsp,asp
  C controller
    是沟通Controller和view的桥梁

  ⑥MVC流程:
    request --> HandlerMapping --> controller --> ModelAndView --> VewResolver --> View --> response

  ⑦DispatcherServlet四大组件:
    (1).HandlerMapping
    (2).controller
    (3).ModelAndView
    (4).ViewResolver

 

posted @ 2016-06-21 23:40  叶莜落  阅读(229)  评论(0编辑  收藏  举报