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

java框架--springmvc --intercepter-restfull + maven

时间:2018-01-08 12:15:00      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:tag   -name   pack   hello   scan   hand   ogg   4.0   handle   

技术分享图片

技术分享图片

技术分享图片
package com.sxt.interceptor;

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

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


public class MyInterceptor implements HandlerInterceptor {

    public void afterCompletion(HttpServletRequest request,    HttpServletResponse response, Object handler, Exception ex)    throws Exception {
        System.out.println("最后执行!!!一般用于释放资源!!");
        
    }

    public void postHandle(HttpServletRequest request,HttpServletResponse response, Object handler,    ModelAndView modelAndView) throws Exception {
        System.out.println("Action执行之后,生成视图之前执行!!");
    }

    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        System.out.println("action之前执行!!!");
        return true;  //继续执行action
    }

}
MyInterceptor
技术分享图片
package com.sxt.interceptor;

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

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


public class MyInterceptor2 extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor2.preHandle()");
        return true;  //继续执行action
    }

}
MyInterceptor2
技术分享图片
package com.zhouwuji.controller;

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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class LoginController {
      
    /*@RequestMapping("/login.do")
    public ModelAndView  login(String name,HttpServletRequest request,HttpServletResponse response){
        ModelAndView  mV=new ModelAndView();
         mV.addObject("ad", name);
         mV.setViewName("index");
         return mV;
    }*/
    
     @RequestMapping("/login.do")
    public  String login(String name,ModelMap modelMap){
         System.out.println(name);
         modelMap.addAttribute("name",name);
         return  "index";
     }
     
     //delete?id=123  restful 风格请求  http://localhost:8080/project/abc/1/delete.do
     //  @PathVariable int  String  可以一个
     @RequestMapping("/delete/{id}")
     public String hello(@PathVariable String id,ModelMap modelMap){
        System.out.println(id);
         modelMap.addAttribute("name",id);
         return "index";
     }
     
     //  @PathVariable int  String  多个
     @RequestMapping("/{id}/{uid}/delete")
     public String hello1(@PathVariable String id,@PathVariable int uid,ModelMap modelMap){
         System.out.println(id);
         System.out.println(uid);
         modelMap.addAttribute("name",id);
         return "index";
     }
/*action之前执行!!!
MyInterceptor2.preHandle()
sdf
Action执行之后,生成视图之前执行!!
最后执行!!!一般用于释放资源!*/   
     
     
}
LoginController
技术分享图片
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@  taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
       值:${name}  
</body>
</html>
index.jsp
技术分享图片
<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <bean  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <context:component-scan base-package="com.zhouwuji.controller"></context:component-scan>
   
       <mvc:interceptors>
        <bean class="com.sxt.interceptor.MyInterceptor"></bean> <!-- 拦截所有springmvc的url! -->
        <mvc:interceptor>
            <mvc:mapping path="/login.do" />
            <!--<mvc:mapping path="/test/*" />-->
            <bean class="com.sxt.interceptor.MyInterceptor2"></bean>
        </mvc:interceptor>
    </mvc:interceptors>

   
</beans>
springmvc-servlet.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <filter>
        <filter-name>CharacterEncodingFilter</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>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
   
   <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
   
   <welcome-file-list>
           <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
</web-app>
web.xml
技术分享图片
<%@ 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> 
   <fieldset>
       <legend>form</legend>
        <form action="login.do"  method="post">
                    姓名  :<input  type="text"  name="name">
                    <input type="submit" value="提交"> 
        </form>
   </fieldset>
    <a  href="delete/abc.do"> restful风格请求 一个的参数</a>
    <a  href="abc/1/delete.do"> restful风格请求  多个参数</a>
</body>
</html>
index.jsp
技术分享图片
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.zhouwuji.wuji</groupId>
    <artifactId>project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/jstl/jstl -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.2</version>
        </dependency>
    </dependencies>


</project>
pom.xml

 

java框架--springmvc --intercepter-restfull + maven

标签:tag   -name   pack   hello   scan   hand   ogg   4.0   handle   

原文地址:https://www.cnblogs.com/ou-pc/p/8241840.html

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