spring mvc 学习

1 环境myeclipse7.5(不要用eclipse),jdk1.6,tomcat6.0.13,spring2.5.6

2 所用到jar包:spring中的jar包都在spring目录\dist\modules下:spring-aop.jar,spring-beans.jar,spring-context.jar,spring-core.jar,spring-web.jar,spring-webmvc.jar。用到的jstl包:jstl.jar, standard.jar.其他jar包:

commons-logging.jar,log4j-1.2.15.jar.

3 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">
   
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
   
    <!-- spring MVC -->
    <servlet>
        <servlet-name>springMVCTemplet</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVCTemplet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
   
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
4 springMVCTemplet-servlet.xml注意xxx-servlet.xml中的xxx应是web.xml中的servlet-name不是所谓的项目名称。

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,这里以jsp为后最 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/" p:suffix=".jsp"
        p:viewClass="org.springframework.web.servlet.view.JstlView" />

    <!--告诉spring在cn.lym.control包下应用了spring注解  -->
    <context:component-scan base-package="com.lym.control" />

</beans>
5 applicationContext.xml 注意 applicationContext.xml文件位置应该与web.xml中的配置一致即在WEB-INF下

<?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:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   

</beans>

6 src 目录下创建包: com.lym.control

创建类 IndexController ,代码如下:

package com.lym.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

@Controller
public class IndexController {
   
    @RequestMapping("/index.htm")
    public ModelMap index(HttpServletRequest request,
            HttpServletResponse response)throws Exception{       
        ModelMap model = new ModelMap();
        model.addAttribute("hello", "hello Word!");
        return model;
    }
}
7 index.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>
      spring MVC<br>
    访问index.htm <br>
    从控制器获得的值:${hello}
  </body>
</html>

访问 http://地址:端口号/项目名称/index.htm 例如http://localhost:8080/springmvc/index.htm

出现

spring MVC
访问index.htm   
从控制器获得的值:hello Word!

配置成功!(以上所有配置文件都在WEB-INF下)

你可能感兴趣的:(spring,AOP,Web,mvc,servlet)