SpringMVC框架执行方法前预处理

在控制类中添加预处理方法,并在方法前添加注解@ModelAttribute("名字")则在执行目标方法前会先执行该方法,再执行目标方法
如关键字查询时,后台获取的是完整字符,需要在其前后加%XXX%,可以通过此方法预处理

HTML

 <form action="queryByKey.do" method="post">
      关键字:<input type="text" name="key">
      <input type="submit" value="搜索">
   </form>

后台

@Controller
public class Query {
	@ModelAttribute("pre")
  public String pre(String key){
	  StringBuffer k = new StringBuffer();
	  k.append("%");
	  k.append(key);
	  k.append("%");
	  return k.toString();
  }
	@RequestMapping("/queryByKey")
	public String query(ModelMap map){
		String s = (String) map.get("pre");
		System.out.println(s);
		return "success";
	}
}

在前端虽然请求的是queryByKey,但是会先经过pre()方法,然后通过query方法的形参来获取预处理参数的值

猜你喜欢

转载自blog.csdn.net/qq_36677358/article/details/84566126