注解配置springMVC

在随笔“springMVC项目配置文件”的基础上,进行优化,使用注解配置,控制器类得以简化:

一、注解配置springMVC

1、在HelloController类中,去除实现的Controller接口,并给方法HandlerRequest添加注解@RequestMapping:

  @Controller

  public Class HelloController{

    @RequestMapping("/hello")

    public ModelAndView HandlerRequest(HttpServletRequest x1,HttpServletResponse x2)    {       }

  }

  //其中,@Controller注解是用来声明控制器的

  //@RequestMapping注解表示/hello路径会映射到该方法上

  //另,若@RequestMapping注解作用在类上,则相当于给该类所有配置的映射地址前加上了一个地址

2、在dispatcher-servlet.xml 文件中,注释掉原来的映射设置:包括路径映射与控制器类的bean

 重新增加一个组件扫描:  <context : component-scan base-package = "controller" />     //扫描controller包下的控制器类

二、配置视图解析器

  在WEB-INF 下建page文件夹,WEB-INF是javaweb默认的安全目录,不允许用户直接访问

1、在dispatcher-servlet.xml中,添加一个bean,告知视图解析器:

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

    <property name = "prefix" value = "/WEB-INF/page" />

    <property name = "subfix" value = ".jsp" />

  </bean>

  //配置springMVC内置的视图解析器InternalResourceViewResolver,在视图名上添加前缀后缀进而确定一个web应用中视图资源的物理路径

2、在HelloController中把方法HandlerRequest的方法体内容改为ModelAndView model = new ModelAndView(viewName:"index");

3、把原index.jsp文件剪切到WEB-INF 下page文件夹中

4、访问路径localhost/hello   则可以显示内容页了,实际路径是/WEB-INF/page/index.jsp

三、控制器接收请求数据

<form action = "/param" role = "form">

  用户名:<input type = "text"  name = "username"><br/>

  <input type = "submit" value = "提交">

</form>

servlet 原生ApI实现接收请求数据:

1、@RequestMapping("/param")

  public ModelAndView getparam(HttpServletRequest req, HttpServletResponse  resp){

    String username = req.getParameter("username");

    sysout(username);

    return null;

  }   //获取表单提交的用户名

2、同名匹配规则,把方法入参名设成与前台传参名一致,也可获取到表单提交的数据:

@RequestMapping("/param")

public ModelAndView getparam(String username){

    sysout(username);

    return null;

  } 

//问题是,与前台强耦合

解决方法是用@RequestParam(“前台参数名”)来注入:

3、@RequestMapping("/param")

public ModelAndView getparam(@RequestParam("username")  String u){

    sysout(u);

    return null;

  } //同样获取到表单提交的数据

另,使用模型传参:前台参数名必须与模型字段名一致:

public Class  User{   String username;   //getter、setter方法}

public ModelAndView getparam(User user){

  sysout(user.getUsername);

  return null;

}

解决post方式的中文乱码问题: 通过在web.xml中添加springmvc字符编码过滤器:

<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>

</filter>

<filter-mapping>

  <filter-name>CharacterEncodingFilter</filter-name>

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

</filter-mapping>

//<filter><filter-mapping>要成对配置

四、控制器回显数据

/WEB-INF/page下创建test.jsp页面

<!Doctype html>

<%@page language = "java" contentType = "text/html;charset = UTF-8" pageEncoding = "UTF-8" import="java.util.*" isElIgnore = "false" %>  //不忽略el表达式

<html>

  <head>  <title>springmvc数据回显</title> </head>

  <body>

    <h1>  回显数据: ${ message }</h1>

  </body>

</html>

1、Servlet原生ApI回显数据:

@RequestMapping("/value")

public ModelAndView handlerRequest( HttpServletRequest req,  HttpServletResponse resp){

  req.setAttribute("message","成功");

  return new ModelAndView("test");

}  //localhost/value  返回: 回显数据:成功

2、使用springmvc的ModelAndView对象回显数据:

@RequestMapping("/value")

public ModelAndView handlerRequest( HttpServletRequest req,  HttpServletResponse resp){

  ModelAndView model  = new ModelAndView(viewName:"test");

  model.addObject(attributeName:"message", attributeValue:"成功");

  return model ;

}  // 访问 localhost/value  返回: 回显数据:成功

3、使用Model对象回显数据

@RequestMapping("/value")

public String handlerRequest(Model  m){

  m.addAttribute(s:"message",o:"成功");

  return  "test";

}

//@ModelAttribute注解

public void model(Model model){

  model.addAttribute("message","成功");

}

//message会放进页面参数中,在视图中直接调用。

客户端跳转

/hello 或者/test都是服务端的跳转,即:request.getRequestDispatcher("地址").forwards(req,resp);

改写HelloController控制器类:

@RequestMapping("/hello")

public ModelAndView handlerRequest(HttpServletRequest req, HttpServletResponse resp){

  ModelAndView model = new ModelAndView("index");

  model.addObject("message","hello springmvc");

  return model;

}

@RequestMapping("/jump")

public ModelAndView jump(){

  ModelAndView m = new ModelAndView("redirect:/hello");

  return m;

}

//访问localhost/jump 会自动跳转到  localhost/hello

结果和前面一样

也可以这样写:

@RequestMapping("/jump")

public String jump(){

  return "redirect: ./hello";

}

文件上传

先导入jar包: commons-io-1.3.2.jar      \     commons-fileupload-1.2.1.jar

1、在dispatcher-servlet.xml中配置上传解析器:

<bean id="multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" />

2、 upload.jsp建在page文件夹下:

<%@ page contentType = "Text/html; charset = UTF-8" language = "java" %>

<html>

  <head><title>文件上传</title><head>

  <body>

    <form action = "/upload" method = "post" enctype = "multipart/form-data">

      <input type = "file" name = "picture">

      <input type = "submit" value = "上传"> 

    </form>

  </body>

</html>

3、编写控制器类UploadController在包controller下:

@Controller

public class UploadController{

  @RequestMapping("/upload")

  public void upload(@Requestparam("picture") MultipartFile file) Throws Exception{

    sysout(file.getOriginalFileName());

  }

  @RequestMapping("/test2")

  public ModelAndView upload(){

    return new ModelAndView("upload")

  }

}

//访问 localhost/test2 

猜你喜欢

转载自www.cnblogs.com/blackdd/p/12313044.html