Spring MVC开发初体验

1.目标实现Spring MVC :
  Hello World!


2.工程创建步骤

new : Dynamic Web Project
  

 


lib引入Spring框架libs/*.jar

touch web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    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">
    <servlet>
        <servlet-name>springMVC</servlet-name>
            <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/config/springMVC-servlet.xml</param-value>
            </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

touch config/springMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
        
    <context:component-scan base-package="www.xi.com" />
    
    <mvc:annotation-driven />
    
    <beans:bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/jsp/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
 </beans:beans>

 

touch jsp/hello.jsp
  文件名称很重要!

<%@ 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>
"hello world"
</body>
</html>

touch www.xi.com.mvcController.java

package www.xi.com;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class mvcController {

    private static final Log logger = LogFactory.getLog(mvcController.class);

    @RequestMapping("/hello")
    public String hello() {
        logger.info("hello called");

        return "hello";
    }

}

 


执行访问:
http://localhost:8080/SpringMVC/hello

显示:

“hello world”
3.问题
1.Spring MVC常用注解有哪些?

2.Spring MVC的基本流程是什么?

3.接口解释
DispatcherServlet
WebApplicationContext
HandlerMapping
HandlerAdapter
Controllers
ViewResolver
LocaleResolver & LocaleContextResolver
HandlerExceptionResolver
ThemeResolver
MultipartResolver
FlashMapManager

4.Servlet,JSP基础概念

5.xml中如何知道有哪些标签,以及标签的意义等?

posted @ 2017-09-15 17:17  小西346  阅读(205)  评论(0编辑  收藏  举报