spring-mvc 框架的简单搭建

1、下载 spring 包,http://www.springsource.org/download

     我下的是spring-framework-4.0.6.RELEASE

2、新建 web项目,在/WebRoot/lib 导入.jar包(/spring-framework-4.0.6.RELEASE/libs目录下)

spring框架配置

1、web.xml 配置

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

<web-app 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee" 

    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"

    id="WebApp_ID" 

    version="3.0">

    

    <!-- 应用上下文配置文件 -->

    <context-param>

        <param-name>contextConfigLocation</param-name>

        <param-value>/WEB-INF/spring-servlet.xml</param-value>

    </context-param>

    

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

    

    <!-- 配置servlet 核心servlet -->

    <servlet>

        <servlet-name>spring</servlet-name>

        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <load-on-startup>1</load-on-startup>

    </servlet>

    <!-- url-pattern配置为/,不带文件后缀,会造成其它静态文件(js、css等)不能访问。

         如配置为*.do,则不影响静态文件的访问 -->

    <servlet-mapping>

        <servlet-name>spring</servlet-name>

        <url-pattern>/</url-pattern>

    </servlet-mapping>

    

</web-app>

 

2、应用上下文配置文件,spring-servlet.xml(web中定义存放路径为:/WEB-INF/spring-servlet.xml)

<beans xmlns="http://www.springframework.org/schema/beans"

 xmlns:context="http://www.springframework.org/schema/context"

 xmlns:p="http://www.springframework.org/schema/p"

 xmlns:mvc="http://www.springframework.org/schema/mvc"

 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

 xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

      http://www.springframework.org/schema/context

      http://www.springframework.org/schema/context/spring-context.xsd

      http://www.springframework.org/schema/mvc

      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

      

      <!-- 启动注解驱动的Spring MVC功能,注册请求url和注解 POJO 类方法的映射 -->

      <mvc:annotation-driven/>

      

      <!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等

          注解的类成为spring的bean -->

      <context:component-scan base-package="sendi.znwg.rest"></context:component-scan>

      

      <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"

                                                          

          p:prefix="/WEB-INF/view" p:suffix=".jsp"></bean>

 </beans>

3、Demo例子

根据spring-servlet配置的包路径(sendi.znwg.rest),新建RestController.java

@Controller

public class RestController {

    public RestController(){

        

    }

    

    @RequestMapping(value = "/login/{user}",method=RequestMethod.GET)

    public ModelAndView rest(HttpServletRequest request,

            HttpServletResponse response,@PathVariable("user")String user,ModelMap modelMap){

        modelMap.put("loginUser", user);

        return new ModelAndView("/login/hello",modelMap);

    }

    

    @RequestMapping(value="/welcome",method = RequestMethod.GET)

    public String registPost(){

        return "/welcome";

    }

    

}

可以自定义jsp(视图路径 /WEB-INF/view 下)

hello.jsp

<%@page import="java.util.Date"%>

<%@ 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>hello 页面</title>

</head>

<body>

<h2>

    你好:<%=request.getAttribute("loginUser") %>,现在的时间是<%=new Date() %>

</h2>

</body>



</html>

tomcat部署后访问: 

http://localhost:8080/spring-mvcDay2/login/gzsendi

 

你可能感兴趣的:(spring)