{"id":959,"date":"2014-07-19T10:04:44","date_gmt":"2014-07-19T02:04:44","guid":{"rendered":"http:\/\/coderbee.net\/?p=959"},"modified":"2014-08-13T23:11:19","modified_gmt":"2014-08-13T15:11:19","slug":"spring-mvc-%e4%b8%8e-web%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/java\/20140719\/959","title":{"rendered":"Spring MVC \u4e0e web\u5f00\u53d1"},"content":{"rendered":"<p>\u9879\u76ee\u7ec4\u7528\u4e86 Spring MVC \u8fdb\u884c\u5f00\u53d1\uff0c\u89c9\u5f97\u5bf9\u91cc\u9762\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0d\u662f\u5f88\u6ee1\u610f\uff0c\u5c31\u60f3\uff0c\u5982\u679c\u662f\u6211\u6765\u642d\u5efa\u5f00\u53d1\u73af\u5883\uff0c\u6211\u4f1a\u600e\u4e48\u505a\uff1f\u4e0b\u9762\u5c31\u662f\u6211\u7684\u60f3\u6cd5\uff0c\u53ea\u5173\u6ce8\u4e8e MVC \u7684 View \u5c42\u3002<\/p>\n<h3>\u4e00\u3001\u7edf\u4e00\u7684\u54cd\u5e94\u683c\u5f0f<\/h3>\n<p>\u73b0\u5728\u57fa\u672c\u4e0a\u90fd\u662f\u7528 ajax \u6765\u8c03\u7528\u540e\u53f0\u63a5\u53e3\uff0c\u62ff\u5230 json\u683c\u5f0f\u7684\u6570\u636e\u518d\u5c55\u793a\uff0c\u6709\u7684\u4eba\u76f4\u63a5\u8fd4\u56de\u6570\u636e\uff0c\u5374\u6ca1\u6709\u8003\u8651\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u6211\u89c9\u5f97\u8fd4\u56de\u7684\u62a5\u6587\u91cc\u5fc5\u987b\u5305\u542b\u8868\u793a\u53ef\u80fd\u7684\u5f02\u5e38\u4fe1\u606f\u7684\u6570\u636e\u548c\u4e1a\u52a1\u54cd\u5e94\u6570\u636e\u3002\u6211\u5b9a\u4e49\u4e86\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u6765\u8868\u793a\u62a5\u6587\u683c\u5f0f\uff1a<\/p>\n<pre><code class=\"java\">\/**\n * \u7edf\u4e00\u7684 HTTP \u54cd\u5e94\u683c\u5f0f\u3002&lt;br\/&gt;\n * code \u4e3a \"ok\" \u8868\u793a\u4e1a\u52a1\u8c03\u7528\u6210\u529f\uff0c\u5426\u5219\u662f\u5931\u8d25\u7684\u9519\u8bef\u7801\uff0c\u5982\u679c\u6709\u591a\u4e2a\u5219\u4ee5\u9017\u53f7\u5206\u9694\u3002&lt;br\/&gt;\n * data \u662f\u4e1a\u52a1\u6570\u636e\uff0c\u5982\u679c\u5931\u8d25\u4e86\u5219\u662f null\u3002\n * \n * @author http:\/\/coderbee.net\n *\n *\/\npublic class RespBody {\n    public static final String OK_CODE = \"ok\";\n    private final String code;\n    private final Object data;\n\n    private static final RespBody OK = new RespBody(OK_CODE, null);\n\n    private RespBody(String code, Object data) {\n        this.code = code;\n        this.data = data;\n    }\n\n    public static RespBody ok() {\n        return OK;\n    }\n\n    public static RespBody ok(Object data) {\n        return new RespBody(\"ok\", data);\n    }\n\n    public static RespBody error(String code) {\n        return new RespBody(code, null);\n    }\n\n    public static RespBody error(String code, Object msg) {\n        return new RespBody(code, msg);\n    }\n\n    public String getCode() {\n        return code;\n    }\n\n    public Object getData() {\n        return data;\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7c7b\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9759\u6001\u65b9\u6cd5\u6765\u5feb\u901f\u6784\u5efa\u54cd\u5e94\u62a5\u6587\uff0c\u8fd9\u4e5f\u662f\u5f88\u91cd\u8981\u7684\u4e00\u4e2a\u8bbe\u8ba1\uff1a\u7528\u9759\u6001\u5de5\u5382\u65b9\u6cd5\u800c\u4e0d\u662f\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p>\u8fd9\u91cc\u7684 <code>code<\/code> \u4e0d\u5e94\u8be5\u662f\u76f4\u63a5\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f\uff0c\u5e94\u8be5\u53ea\u662f\u7b80\u5355\u7684\u9519\u8bef\u7f16\u7801\uff0c\u8fd9\u6837\u4e0d\u540c\u7684\u5ba2\u6237\u7aef\u90fd\u53ef\u4ee5\u8c03\u7528\u8fd9\u4e2a API\uff0c\u7136\u540e\u518d\u6839\u636e\u9519\u8bef\u7f16\u7801\u3001\u5ba2\u6237\u7aef\u8bed\u8a00\u548c\u81ea\u5df1\u7684\u5ba2\u6237\u7aef\u7279\u6027\u9009\u62e9\u5408\u9002\u7684\u9519\u8bef\u63d0\u793a\u4fe1\u606f\u548c\u63d0\u793a\u65b9\u5f0f\u3002<\/p>\n<p><!--more--><\/p>\n<h3>\u4e8c\u3001\u7edf\u4e00\u7684\u5f02\u5e38\u5904\u7406<\/h3>\n<p>\u5f88\u591a\u4eba\u90fd\u4e0d\u8003\u8651\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u5bfc\u81f4\u5f02\u5e38\u6808\u76f4\u63a5\u629b\u5230\u54cd\u5e94\u91cc\uff0c\u8fd9\u662f\u4e0d\u53cb\u597d\u4e5f\u4e0d\u5b89\u5168\u7684\u3002\u7edf\u4e00\u7684\u5f02\u5e38\u5904\u7406\u662f\u5fc5\u987b\u7684\u3002<\/p>\n<p>\u6211\u5b9a\u4e49\u4e86\u4e00\u4e2a <code>BaseController<\/code>\uff1a<\/p>\n<pre><code class=\"java\">@Controller\npublic class BaseController {\n    protected final static Logger logger = LoggerFactory\n            .getLogger(\"controller\");\n\n    @ResponseBody\n    @ExceptionHandler(Exception.class)\n    public RespBody exceptionHandler(Exception ex) {\n        return RespBody.error(\"exception\", ex.getMessage());\n    }\n}\n<\/code><\/pre>\n<p>\u5b83\u7684\u4f5c\u7528\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7edf\u4e00\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\u3002Spring MVC\u5bf9\u5f02\u5e38\u5904\u7406\u7684\u903b\u8f91\u5f88\u597d\uff0c\u5982\u679c\u67d0\u4e2a<code>Controller<\/code>\u7c7b\u6ca1\u6709\u63d0\u4f9b\u5e26 <code>@ExceptionHandler<\/code> \u6ce8\u89e3\u7684\u65b9\u6cd5\uff0c\u5219\u4f1a\u67e5\u627e\u7236\u7c7b\u662f\u5426\u6709\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6240\u4ee5\u7ee7\u627f\u81ea\u8fd9\u4e2a <code>BaseController<\/code> \u7684 <code>Controller<\/code> \u90fd\u81ea\u52a8\u83b7\u5f97\u5f02\u5e38\u5904\u7406\u80fd\u529b\u3002<\/p>\n<h3>\u4e09\u3001\u7edf\u4e00\u7684\u53c2\u6570\u6821\u9a8c<\/h3>\n<p><strong>\u53c2\u6570\u6821\u9a8c\u662f\u5fc5\u987b\u7684\uff0c\u800c\u4e14\u5fc5\u987b\u653e\u5728\u670d\u52a1\u5668\u7aef\u6765\u505a\uff0c\u5ba2\u6237\u7aef\u7684\u6821\u9a8c\u90fd\u662f\u53ef\u4ee5\u7ed5\u8fc7\u7684<\/strong>\u3002<\/p>\n<p>Spring MVC \u5f53\u7136\u4e5f\u652f\u6301\u53c2\u6570\u6821\u9a8c\uff0c\u5728 Spring MVC \u7684\u914d\u7f6e\u6587\u4ef6\u91cc\u52a0\u5165 <code>&lt;mvc:annotation-driven \/&gt;<\/code> \u5373\u53ef\u4ee5\u5f00\u542f\u6ce8\u89e3\u6821\u9a8c\u3002<\/p>\n<p>\u4f46 Spring MVC \u7684\u53c2\u6570\u6821\u9a8c\u6709\u4e9b\u5c40\u9650\uff1a<\/p>\n<ul>\n<li>\u4e0d\u652f\u6301\u5bf9\u57fa\u672c\u7c7b\u578b\u548c <code>String<\/code> \u7c7b\u578b\u7684\u53c2\u6570\u8fdb\u884c\u6821\u9a8c\uff0c\u4e5f\u5c31\u662f\u53ea\u652f\u6301\u5bf9 POJO \u6821\u9a8c\uff0c\u8fd9\u4e2a\u975e\u5e38\u4e0d\u53cb\u597d\uff0c\u5982\u679c\u4e00\u4e2a\u63a5\u53e3\u53ea\u6709\u5f88\u5c11\u7684\u51e0\u4e2a\u53c2\u6570\u90fd\u5fc5\u987b\u5b9a\u4e49\u4e00\u4e2a POJO \u662f\u5f88\u607c\u4eba\u7684\uff0c\u8981\u4e48\u5c31\u5f97\u624b\u52a8\u6821\u9a8c\uff0c\u5199\u4e00\u4e9b if \u5206\u652f\uff1b<\/li>\n<li>\u6bcf\u4e2aPOJO\u540e\u9762\u90fd\u5f97\u6709\u4e00\u4e2a <code>BindingResult<\/code> \u7684\u53c2\u6570\uff0c\u4f5c\u4e3a\u5bf9 POJO \u7684\u6821\u9a8c\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p>\u800c\u4e14\u5728\u6bcf\u4e2a\u65b9\u6cd5\u91cc\u90fd\u5fc5\u987b\u5bf9  <code>BindingResult<\/code> \u8fdb\u884c\u68c0\u6d4b\uff0c\u6765\u5224\u65ad\u53c2\u6570\u662f\u5426\u5408\u6cd5\u3002<\/p>\n<h4>\u5728 AOP \u91cc\u8fdb\u884c\u53c2\u6570\u6821\u9a8c<\/h4>\n<p>\u501f\u52a9 Spring \u5bf9 AOP \u7684\u652f\u6301\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 AOP \u91cc\u5bf9\u8bf7\u6c42\u7684 <code>Controller<\/code> \u7684\u65b9\u6cd5\u8fdb\u884c\u62e6\u622a\uff0c\u505a\u53c2\u6570\u6821\u9a8c\uff0c\u5982\u679c\u6821\u9a8c\u4e0d\u5408\u683c\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\uff08\u56e0\u4e3a\u6211\u4eec\u5df2\u7ecf\u6709\u4e86\u7edf\u4e00\u7684\u54cd\u5e94\u683c\u5f0f\uff09\u3002<\/p>\n<p>\u5728 AOP \u91cc\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Hibernate-Validator \u8fdb\u884c\u624b\u52a8\u6821\u9a8c\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 Spring-MVC \u8fdb\u884c\u6821\u9a8c\uff0c\u8fd9\u6837\u6211\u4eec\u5c31\u4e0d\u9700\u8981\u5728\u6bcf\u4e2a POJO \u540e\u9762\u653e\u4e00\u4e2a <code>BindingResult<\/code> \u53c2\u6570\uff0c\u4e14 Hibernate-Validator \u652f\u6301\u5bf9\u57fa\u672c\u7c7b\u578b\u548c <code>String<\/code> \u7c7b\u578b\u7684\u53c2\u6570\u8fdb\u884c\u6821\u9a8c\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u5728 Hibernate-Validator-4.2.0-Final\u3001validation-api-1.0.0-GA \u4e0b\u6d4b\u8bd5\u7684\uff1a<\/p>\n<p>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a <code>BindingResultHandler<\/code> \u7c7b\uff0c\u5b83\u7684\u65b9\u6cd5 <code>validate<\/code> \u6821\u9a8c\u8bf7\u6c42\u7684 <code>Controller<\/code> \u7684\u65b9\u6cd5\u7684\u53c2\u6570\u662f\u5426\u5408\u683c\uff0c\u5982\u679c\u5408\u683c\u5219\u7ee7\u7eed\u8c03\u7528\u4e1a\u52a1\u903b\u8f91\uff0c\u5426\u5219\u8fd4\u56de\u9519\u8bef\u63d0\u793a\u3002<\/p>\n<pre><code class=\"java\">public class BindingResultHandler {\n\n    public Object validate(ProceedingJoinPoint pjp) throws Throwable {\n        Object target = pjp.getTarget();\n\n        MethodSignature joinPointObject = (MethodSignature) pjp.getSignature();\n        Method method = joinPointObject.getMethod();\n\n        MethodValidator validator = Validation\n                .byProvider(HibernateValidator.class).configure()\n                .buildValidatorFactory().getValidator()\n                .unwrap(MethodValidator.class);\n\n        Set&lt;MethodConstraintViolation&lt;Object&gt;&gt; violations = validator\n                .validateAllParameters(target, method, pjp.getArgs(),\n                        new Class[] {});\n\n        if (!violations.isEmpty()) {\n            StringBuilder sb = new StringBuilder(128);\n            for (ConstraintViolation&lt;Object&gt; violation : violations) {\n                sb.append(',').append(violation.getMessage());\n            }\n            return RespBody.error(sb.substring(1), \"param validation failed .\");\n        }\n\n        return pjp.proceed();\n    }\n}\n<\/code><\/pre>\n<p>AOP \u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean id=\"bindingResultHandler\" class=\"net.coderbee.demo.controller.validation.BindingResultHandler\" \/&gt;\n&lt;aop:config&gt;\n    &lt;aop:aspect id=\"aspectBindingResult\" ref=\"bindingResultHandler\"&gt;\n        &lt;aop:pointcut id=\"bindingResultHandlerPointcut\"\n            expression=\"execution(public * net.coderbee.demo.controller..*Controller.*(..))\" \/&gt;\n\n        &lt;aop:around method=\"validate\" pointcut-ref=\"bindingResultHandlerPointcut\" \/&gt;\n    &lt;\/aop:aspect&gt;\n&lt;\/aop:config&gt;\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5728 Controller \u91cc\u5c31\u53ea\u9700\u505a\u6ce8\u89e3\u4e0d\u80fd\u5b8c\u6210\u7684\u6821\u9a8c\u4e86\u3002<\/p>\n<pre><code class=\"java\">@Controller\npublic class UserController extends BaseController {\n    @ResponseBody\n    @RequestMapping(value = \"\/test\/valids\")\n    public RespBody valids(@Valid User user, @Valid Address address) {\n\n        return RespBody.ok(user);\n    }\n}\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u4ee3\u7801\u4f1a\u7b80\u6d01\u5f88\u591a\u3002<\/p>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9879\u76ee\u7ec4\u7528\u4e86 Spring MVC \u8fdb\u884c\u5f00\u53d1\uff0c\u89c9\u5f97\u5bf9\u91cc\u9762\u7684\u4f7f\u7528\u65b9\u5f0f\u4e0d\u662f\u5f88\u6ee1\u610f\uff0c\u5c31\u60f3 &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/java\/20140719\/959\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[173,172],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/959"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=959"}],"version-history":[{"count":4,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/959\/revisions"}],"predecessor-version":[{"id":963,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/959\/revisions\/963"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=959"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=959"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=959"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}