日期:2014-05-16  浏览次数:20498 次

Spring Json View之快速开始(二)

快速开始-用SimpleForm-Controller提交GET/POST请求
?
?Command-Controller提供一个完整的CommandBean,Spring对它提供校验和绑定支持。这个示例在Command-Controller中返回一个Model-Map的Json字符串,json视图返回信息中包含字段错误、全局错误和绑定。支持CommandBean属性类型的转换。

  • 通过formBackingObject方法触发GET请求。
  • 通过onSubmitAction方法出发POST请求。

?

详细信息参见文档


?
Spring ApplicationContext
?

<beans>
    <bean name="simpleJsonPostFormController" 
         class="org.thing.spring.json.controller.SimpleJsonPostFormController">
            <property name="commandClass">
                <value>org.thing.spring.json.controller.SpringJsonForm</value>
            </property>
        <property name="formView"><value>jsonView</value></property>
        <property name="successView"><value>jsonView</value></property>
    </bean>
    <bean name="urlMapping" 
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello.json">simpleJsonPostFormController</prop>
           </props>
        </property>
    </bean>
    <bean name="viewResolver" 
        class="org.springframework.web.servlet.view.XmlViewResolver" />
</beans>

?

Spring view.xml

<beans>
    <bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView">
            <property name="jsonErrors">
                <list>
                        <ref bean="statusError" />
                        <ref bean="modelflagError" />
                </list>
        </property>
    </bean>
    
    <bean name="statusError" 
          class="org.springframework.web.servlet.view.json.error.HttpStatusError">
          <property name="errorCode"><value>311</value></property>
    </bean>
    <bean name="modelflagError" 
          class="org.springframework.web.servlet.view.json.error.ModelFlagError">
          <property name="name"><value>failure</value></property>
          <property name="value"><value>true</value></property>
    </bean>
        
</beans>

?

form.html

<head>
        <title>
                First Test Spring Json Demo
        </title>
        <script type="text/javascript" src="script/prototype.js"></script>
        <script type="text/javascript" src="script/behaviour.js"></script>
        <script type="text/javascript" src="script/behaviour-roles.js"></script>
        <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"/>
</head>
</head>
<body>
        <h1>Spring JSON DEMO</h1>
        <h2>Spring Ajax Post (SimpleFormControler and CommandController)</h2>
        <form method="post" id="form">
                <input id="placeofbirth" type="text" name="placeofbirth" ><br>
                <input id="birthday" type="text" name="birthday" ><br/>
                <br/>
                <b>place of birth : </b><span id="t_placeofbirth"></span><br/>
                <b>birthday : </b><span id="t_birthday"></span><br/>
        
        </form>
        <br/>
        <span id ="error" ></span>
        <br/>
        <button id="clearData">clear name</button>
        <<button id="sfc_postData">send data to SimpleFormController</button>
</body>

?

JavaScript behavi