码迷,mamicode.com
首页 > 编程语言 > 详细

初识spring mvc 框架的搭建

时间:2014-08-30 15:04:09      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   java   strong   ar   

spring mvc:spring提供的一套简洁mvc web框架  

参考

http://blog.csdn.net/wangpeng047/article/details/6983027

http://blog.csdn.net/javabenface/article/details/7405677(spring3.0)

http://www.cnblogs.com/ooi-/p/3535512.htmleclipse搭建web框架

环境:eclipse(Eclipse Java EE IDE for Web Developers)、tomcat7

jar包列表

  spring:不知道那些是必须的,所有都加入

  其他:commons-logging-1.1.3.jar、log4j-1.2.15.jar

1,新建controller

package net.ooi.action;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户controller
 * @author ooi
 *
 */
@Controller
public class UserController {
    
    @RequestMapping("user/list")
    public ModelAndView userList(){
        System.out.println("呵呵");
        ModelAndView modelAndView = new ModelAndView() ; 
        modelAndView.setViewName("user/userList") ; 
        modelAndView.addObject("title", "123") ; 
        
        return modelAndView ; 
    }

}

2,新建拦截器AccessInterceptor.java、UserInterceptor.java
AccessInterceptor.java示例:

package net.ooi.interceptor;

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

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/** 
 * 自定义拦截器 
 */  
public class AccessInterceptor extends HandlerInterceptorAdapter {  
  
    /** 
     * 最后执行,可用于释放资源 
     */  
    @Override  
    public void afterCompletion(HttpServletRequest request,  
            HttpServletResponse response, Object handler, Exception ex)  
            throws Exception {  
       System.out.println(getClass()+"afterCompletion");
        super.afterCompletion(request, response, handler, ex);  
    }  
  
    /** 
     * 显示视图前执行 
     */  
    @Override  
    public void postHandle(HttpServletRequest request,  
            HttpServletResponse response, Object handler,  
            ModelAndView modelAndView) throws Exception {  
        System.out.println(getClass()+"postHandle");
        super.postHandle(request, response, handler, modelAndView);  
    }  
  
    /** 
     * Controller之前执行 
     */  
    @Override  
    public boolean preHandle(HttpServletRequest request,  
            HttpServletResponse response, Object handler) throws Exception {  
        System.out.println(getClass()+"preHandle");
        return super.preHandle(request, response, handler);  
    }  
}  

3,新建页面

位置:bubuko.com,布布扣

内容:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resource/common.js"></script>
</head>
<body>
user list page /${title}
</body>
</html>

4,新建springmvc-servlet.xml配置文件

新建source folder:src-resources。在src-resources下新建目录META-INF,

  然后在META-INF中新建applicationContext.xml、log4j.properties、springmvc-servlet.xml

 springmvc-servlet.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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    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-3.0.xsd    
           http://www.springframework.org/schema/mvc     
           http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
      
    <!-- 扫描 -->  
    <context:component-scan base-package="net.ooi"/>  
   
    <!-- 拦截器-->  
    <mvc:interceptors>  
        <bean class="net.ooi.interceptor.AccessInterceptor"></bean><!-- 拦截所有的配置在这里 -->
        <mvc:interceptor>  
            <mvc:mapping path="/user/*"/>
            <bean class="net.ooi.interceptor.UserInterceptor"></bean>  
        </mvc:interceptor>
    </mvc:interceptors>  
      
    <!-- InternalResourceViewResolver为spring mvc默认的ViewResolver-->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <property name="prefix" value="/WEB-INF/view/"></property>  
        <property name="suffix" value=".jsp"></property>  
    </bean>  
      
</beans>

5,配置web.xml

<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_2_5.xsd"
        version="2.5">

<!-- spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:/META-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
<!--   spring mvc -->
  <servlet>  
    <servlet-name>spring-mvc</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/META-INF/springmvc-servlet.xml</param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
    
  <servlet-mapping>  
    <servlet-name>spring-mvc</servlet-name>  
    <url-pattern>*.action</url-pattern>  
  </servlet-mapping>  
  
<!-- log -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>springmvc01.root</param-value>
    </context-param>

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:/META-INF/log4j.properties</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>

<!-- 编码 -->
    <filter>
        <filter-name>CharsetFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharsetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

6,访问地址http://localhost:8888/springmvc01/user/list.action
ok,结束

 

总算有了一点感觉

 

初识spring mvc 框架的搭建

标签:style   blog   http   color   os   io   java   strong   ar   

原文地址:http://www.cnblogs.com/ooi-/p/3946401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!