SpringMVC第二篇【过滤编码器、注解开发、requestMapping、业务方法与传统参数】

2023-05-24,,

SpringMVC过滤编码器

在SpringMVC的控制器中,如果没有对编码进行任何的操作,那么获取到的中文数据是乱码!

即使我们在handle()方法中,使用request对象设置编码也不行!原因也非常简单,我们SpringMVC接收参数是通过控制器中的无参构造方法,再经过handle()方法的object对象来得到具体的参数类型的

Struts2是使用拦截器来自动帮我们完成中文乱码的问题的。那么SpringMVC作为一个更加强大的框架,肯定也有对应的方法来帮我们完成中文乱码问题!

值得注意的是:该过滤编码器只能解决POST的乱码问题

我们只需要在web.xml配置文件中设置过滤编码器就行了


<!-- 编码过滤器 -->
<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>


注解开发SpringMVC

我们在快速入门的例子中使用的是XML配置的方式来使用SpringMVC的,SpringMVC也能够支持注解。【个人非常喜欢注解的方式】

我们在使用Action的时候,要么继承着AbstractCommandController类,要么实现了Controller接口。当我们使用了注解以后就不用显示地继承或实现任何类了

开发流程

使用@Controller这个注解,就表明这是一个SpringMVC的控制器!


@Controller
public class HelloAction { }

当然了,现在Spring是不知道有这么一个注解的,因此我们需要在配置文件中配置扫描注解

值得注意的是:在配置扫描路径的时候,后面不要加.。。。不然扫描不了,我不知道学Struts2还是其他的地方时候,习惯加了.,于是就搞了很久!

   <!--扫描注解,后面不要加.*-->

    <context:component-scan base-package="zhongfucheng"/>

在控制器中写业务方法

@Controller
public class HelloAction { /**
*
* @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
* @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
* @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
* @throws Exception
*/
@RequestMapping(value="/hello.action") public String hello(Model model) throws Exception{
System.out.println("HelloAction::hello()");
model.addAttribute("message","你好");
return "/index.jsp";
} }

跳转到index页面,首页得到对应的值。


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
这是我的首页 <br>
${message}
</body>
</html>


当然了,基于注解和基于XML来开发SpringMVC,都是通过映射器、适配器和视图解析器的。 只是映射器、适配器略有不同。但是都是可以省略的!


<!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <!-- 视图解析器(可选) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>

更新:上边的适配器和映射器只是Spring3.1版本之前使用的、3.1版本之后现在一般用以下的两个


映射器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping 适配器:
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter

当然了,这上面两个配置也可以使用<mvc:annotation-driven>>替代注解处理器和适配器的配置。


RequestMapping

@RequestMapping能够控制请求路径和请求方式!

一个控制器写多个业务方法

到目前为止,我们都是一个控制器写一个业务方法,这肯定是不合理的。我们在Struts2中一个Action就对应多个业务方法了。那么我们在SpringMVC中又怎么写呢???

其实我们可以推理出来,@RequestMapping就是用于配置哪个请求对应哪个业务方法的!


public @interface RequestMapping {
String[] value() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {};
}

当我们请求hello.action的时候,处理的业务方法是hello()…..当我们请求bye.action的时候,处理的业务方法是bye()


@Controller
public class HelloAction { /**
*
* @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
* @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
* @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
* @throws Exception
*/
@RequestMapping(value="/hello.action")
public String hello(Model model) throws Exception{
System.out.println("HelloAction::hello()");
model.addAttribute("message","你好");
return "/index.jsp";
}
@RequestMapping(value = "/bye.action")
public String bye(Model model) throws Exception {
model.addAttribute("message","再见");
return "/index.jsp";
}
}

分模块开发

当然了,我们在Struts2常常使用namespace来进行分模块开发,在SpringMVC中我们也可以这样干,并且我们又是使用的是@RequestMapping这个注解!

只要把@RequestMapping这个注解写到类上面去,就代表了分模块。



@Controller
//我们知道,如果是value属性上的注解,我们可以把value省略掉的
@RequestMapping("/zhongfucheng")
public class HelloAction { /**
* @param model 它和ModelAndView类似,它这个Model就是把数据封装到request对象中,我们就可以获取出来
* @return 返回跳转的页面【真实路径,就不用配置视图解析器了】
* @throws Exception
* @RequestMapping 表示只要是/hello.action的请求,就交由该方法处理。当然了.action可以去掉
*/
@RequestMapping(value = "/hello.action")
public String hello(Model model) throws Exception {
System.out.println("HelloAction::hello()");
model.addAttribute("message", "你好");
return "/index.jsp";
} @RequestMapping(value = "/bye.action")
public String bye(Model model) throws Exception {
model.addAttribute("message", "再见");
return "/index.jsp";
}
}

那么我们想要HelloAction该控制器处理我们的请求,访问的地址要么是:http://localhost:8080/zhongfucheng/hello.action,或者要么是http://localhost:8080/zhongfucheng/bye.action


限定某个业务控制方法,只允许GET或POST请求方式访问

我们如果想要限定某个业务控制方法,只允许GET或POST请求方式访问。还是通过@RequestMapping来实现。只要设定它的method属性就行了


@RequestMapping(value = "/bye.action",method = RequestMethod.POST)
public String bye(Model model) throws Exception {
model.addAttribute("message", "再见");
return "/index.jsp";
}

当我把业务方法的请求设置为POST以后,我想要通过GET方式来访问该业务方法。就行不通了!


业务方法写入传统web参数

我们的业务方法除了可以写Model这个参数以外,如果有需要我们还可以写request,response等传统Servlet的参数。这是一样可以使用的….

但是呢,我们并不建议使用传统的web参数,因为会耦合


@RequestMapping(method=RequestMethod.POST,value="/register")
public String registerMethod(HttpServletRequest request,HttpServletResponse response) throws Exception{ //获取用户名和薪水
String username = request.getParameter("username");
String salary = request.getParameter("salary");
System.out.println("用户注册-->" + username + ":" + salary); //绑定到session域对象中
request.getSession().setAttribute("username",username);
request.getSession().setAttribute("salary",salary); //重定向/jsp/success.jsp页面
//response.sendRedirect(request.getContextPath()+"/jsp/success.jsp"); //转发/jsp/ok.jsp页面
request.getRequestDispatcher("/jsp/ok.jsp").forward(request,response); //转发(提倡)
return "/jsp/success.jsp";
}

小细节:如果我们的返回值是返回一个真实路径,而我们在程序中又使用了转发或重定向。。。那么具体跳转的位置就是按我们程序中跳转的路径为准


SpringMVC第二篇【过滤编码器、注解开发、requestMapping、业务方法与传统参数】的相关教程结束。

《SpringMVC第二篇【过滤编码器、注解开发、requestMapping、业务方法与传统参数】.doc》

下载本文的Word格式文档,以方便收藏与打印。