好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring mvc高级技术实例详解

spring mvc高级技术包括但不限于web.xml配置、异常处理、跨重定向请求传递数据

1、web.xml文件的配置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

<!doctype web-app public

  "-//sun microsystems, inc.//dtd web application 2.3//en"

  "http://java.sun.com/dtd/web-app_2_3.dtd" >

 

<web-app>

 

  <context-param>

  <param-name>contextconfiglocation</param-name>

  <param-value>classpath:spring-*.xml</param-value>

  </context-param>

  <listener>

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

  </listener>

 

  <servlet>

  <servlet-name>appservlet</servlet-name>

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

  <init-param>

   <param-name>contextconfiglocation</param-name>

   <param-value>classpath:springmvc.xml</param-value>

  </init-param>

  </servlet>

  <servlet-mapping>

  <servlet-name>appservlet</servlet-name>

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

  </servlet-mapping>

 

</web-app>

  contextloaderlistener是根容器,dispatcherservlet是子容器。父容器中管理的bean可以被子容器引用,反之,不行。它们都从各自的xml文件初始化自己的上下文

  contextloaderlistener如果未指定contextconfiglocation参数,则默认加载的配置文件为/web-inf/applicationcontext.xml

  dispatcherservlet如果未指定contextconfiglocation参数,则根据<servlet-name>元素指定的名称,在/web-inf/文件夹下寻找配置文件appservlet-servlet.xml

2、处理异常

spring提供了多种方式将异常转换为响应

特定的 spring异常会自动映射为指定的http状态码 异常上可以添加@responsestatus注解,将异常映射为某一个http状态码 在方法上可以添加@exceptionhandler注解,使其用来处理异常

 1)特定的 spring异常会自动映射为指定的http状态码

  如果在控制器中抛出了异常,该异常不在列表中,又没有指定http状态码,则默认为状态码为500

2)异常上可以添加@responsestatus注解,将异常映射为某一个http状态码

?

1

2

3

4

5

6

7

8

9

10

@controller

public class hellocontroller {

 

  @requestmapping ( "/home" )

  public string home(){

  system.out.println( "执行home" );

  throw new myexception();

// return "home"; //返回一个字符串,即逻辑视图名

  }

}

?

1

2

3

4

5

6

7

package com.cn.exception;

 

import org.springframework.http.httpstatus;

import org.springframework.web.bind.annotation.responsestatus;

public class myexception extends runtimeexception{

 

}

浏览器访问 http://localhost:8080/home

修改自定义异常

?

1

2

3

4

5

6

7

8

9

package com.cn.exception;

 

import org.springframework.http.httpstatus;

import org.springframework.web.bind.annotation.responsestatus;

 

@responsestatus (value = httpstatus.not_found, reason = "抛出异常的原因" )

public class myexception extends runtimeexception{

 

}

浏览器访问 http://localhost:8080/home

3)在方法上可以添加@exceptionhandler注解,使其用来处理异常

该方式是按照处理请求的方式处理异常,@exceptionhandler注解的方法返回值为字符串表示逻辑视图名

?

1

2

3

4

5

package com.cn.exception;

 

public class myexception2 extends runtimeexception{

 

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

@controller

public class hellocontroller {

 

  @requestmapping ( "/home" )

  public string home(){

  system.out.println( "执行home" );

  throw new myexception2();

// return "home"; //返回一个字符串,即逻辑视图名

  }

 

  @exceptionhandler (myexception2. class )

  public string handlemexception(){

  system.out.println( "处理异常逻辑" );

  return "fail" ;

  }

}

访问http://localhost:8080/home,后台输出

浏览器页面

@exceptionhandler注解的方法可以处理同一个控制器中所有处理器方法所抛出的异常(注解指定的异常),注解定义如下

?

1

2

3

4

5

6

@target ({elementtype.method})

@retention (retentionpolicy.runtime)

@documented

public @interface exceptionhandler {

  class <? extends throwable>[] value() default {};

}

spring也支持为控制器添加通知,那么该通知对于所有的控制器中所有的处理器方法抛出的异常都起作用,实现如下

  @controlleradvice 所标注的类会被组件扫描实例化,交给容器管理。最为实用的一个场景是将所有@exceptionhandler标注的方法收集到一个类中,这样所有的控制器异常就能在一个地方进行一致处理。以下handleexception类定义表明在控制器中的处理器抛出myexception2类的异常,就会被handlemexception方法处理,最终跳转fail.jsp页面

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

package com.cn.advice;

 

import com.cn.exception.myexception2;

import org.springframework.web.bind.annotation.controlleradvice;

import org.springframework.web.bind.annotation.exceptionhandler;

 

@controlleradvice

public class handleexception {

 

  @exceptionhandler (myexception2. class )

  public string handlemexception(){

   system.out.println( "处理异常逻辑" );

   return "fail" ;

  }

}

 3、跨重定向请求传递数据

  如果是forward转发请求,那么处理器方法业务处理完成后,该方法所指定的模型数据会复制到请求中,作为请求的一部分,转发到下一个处理器,下一个处理器可以从请求中获取上一个处理的模型数据;

  如果是redirect重新向,原始请求就结束了,并且会重新发起一个get请求。因此原始请求中的模型数据也就消失了,在请求到达下一个处理器,没有任何的模型数据,需要模型数据必须自己处理。

针对重定向传递数据,有两种方案:

使用url模板以路径变量和/或查询参数的形式传递数据 使用flash属性传递数据

1)使用url模板以路径变量和/或查询参数的形式传递数据

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@controller

public class hellocontroller {

 

  @requestmapping ( "/home2" )

  public string home2(model model){

   model.addattribute( "id" , "12324131343256" );

   model.addattribute( "name" , "pick" );

   return "redirect:home3/{id}" ; //含有redirect的字符串,表示重定向到另一个处理器;

          //如果含有forward的字符串,表示转向到另一个处理器

  }

 

  @requestmapping ( "/home3/{id}" )

  public string home3( @pathvariable string id, model model){

   system.out.println( "传递的id=" +id);

   model.addattribute( new user());

   return "home" ;

  }

}

  浏览器访问localhost:8080/home2,抓包得访问的url。从以下可以看出,处理器完成后重定向时,模型中的数据填充到了路径变量中,路径变量中没有的key,则以查询参数的形式赋在url之后,组成了新的url访问。

 2)使用flash属性传递数据

  重定向时候,采用1)中的方式传递数据,仅仅能够传递简单的数据,不能传递对象。如何将对象也能传递到下一个处理器呢?可以将要传递到下一个处理器的数据放入用户会话中,然后在下一个处理器从会话的取出来,并删除会话中的该数据。然而,spring提供了将数据发送为flash属性的功能,不需要我们管理这些数据,flash属性会一致携带这些数据直到下一次请求才消失。

  spring提供redirectattributes是model接口的子接口,除了提供model的所有功能,还提供了几个方法用来设置flash属性。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@controller

public class hellocontroller {

 

  @requestmapping ( "/home2" )

  public string home2(redirectattributes model){

   model.addattribute( "id" , "12324131343256" );

   model.addflashattribute( "user" , new user( "liming" , "123456" )); //设置flash属性,也可以 model.addflashattribute(new user("liming","123456"))

                                       //则通过值得类型自行推断出key为user

   return "redirect:home3/{id}" ;

  }

 

  @requestmapping ( "/home3/{id}" )

  public string home3( @pathvariable string id, redirectattributes model){

   system.out.println( "传递的id=" +id);

   if (model.containsattribute( "user" )) {

    system.out.println( "传递的对象:" + model.getflashattributes().get( "user" ));

   }

   return "home" ;

  }

}

异常测试

在controller、service、dao中任意一处需要手动抛出异常。

如果是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示[未知错误]。

在商品修改的controller方法中抛出异常 .

在service接口中抛出异常:

如果与业务功能相关的异常,建议在service中抛出异常。

与业务功能没有关系的异常,建议在controller中抛出。

上边的功能,建议在service中抛出异常。

总结:

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://www.cnblogs.com/shixiemayi/p/9569338.html

查看更多关于spring mvc高级技术实例详解的详细内容...

  阅读:43次