`
theabab
  • 浏览: 41463 次
  • 性别: Icon_minigender_1
  • 来自: 江西
社区版块
存档分类
最新评论

spring:bind

阅读更多
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!--context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>


dispatch-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
   <bean id="defaultHandlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
   
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="register.htm">registerController</prop>
                <prop key="login.htm">loginController</prop>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>
   
   <bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/WEB-INF/jsp</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
</bean>
        <bean id="indexController"
                    class="org.springframework.web.servlet.mvc.ParameterizableViewController"
                    p:viewName="/register"/>
    <bean id="registerController" class="net.kingbit.actions.RegisterStudentController">
        <property name="sessionForm">
            <value>true</value>
        </property>
         <property name="commandName">
            <value>student</value>
        </property>


<property name="formView" value="/register"/>
        <property name="commandClass">
            <value>net.kingbit.pojos.Student</value>
        </property>
        <property name="validator">
           <ref bean="registerValidator"/>
        </property>
      </bean>
     
<bean id="loginController" class="net.kingbit.actions.LoginController"/>

   <bean id="registerValidator" class="net.kingbit.validator.StudentValidator"/>

</beans>


redirect.jsp:
<%--
Views should be stored under the WEB-INF folder so that
they are not accessible except through controller process.

This JSP is here to provide a redirect to the dispatcher
servlet but should be the only JSP outside of WEB-INF.
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<% response.sendRedirect("register.htm"); %>

register.jsp:
<%--
    Document   : register
    Created on : Jan 31, 2009, 10:49:30 AM
    Author     : Kang
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>



  </head>


  <body>
   <form action="<%=request.getContextPath() %>/register.htm" method="post" name="registerForm">
   
   <table>
        <tr><td><spring:bind path="student.name">
      name: <input type="text" name="${status.expression}"/>(必须输入) </td> <td>${status.errorMessage}</spring:bind></td>  </tr>
    <tr><td><spring:bind path="student.sex">
     sex:  <input type="text" name="${status.expression}"/>(必须输入,且为0或1)</td><td>
        ${status.errorMessage}</spring:bind>
     </td></tr>
    </table>

      
 
    
           <input type="submit" value="submit"/>

   </form>
  </body>
</html>


RegisterStudentController:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package net.kingbit.actions;

import net.kingbit.pojos.Student;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
*
* @author Kang
*/
public class RegisterStudentController extends SimpleFormController{


protected ModelAndView onSubmit(Object object, BindException be) throws Exception {
         Student stu=(Student)object;
         return new ModelAndView("/success","student",stu);
    }
}



StudentValidator:验证类

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package net.kingbit.validator;

import org.apache.oro.text.perl.Perl5Util;
import net.kingbit.pojos.Student;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
*
* @author Kang
*/
public class StudentValidator implements Validator {
  private static String SEX_REGEXP="/^[0-1]$/";
    public boolean supports(Class cls) {
        return cls.equals(Student.class);
    }

    public void validate(Object object, Errors errors) {
         Student student=(Student)object;
       if(student.getName().equals("")||student.getName().equals(null))
       {ValidationUtils.rejectIfEmpty(errors, "name", "name", "姓名必须输入");}

         if(student.getSex().equals("")||student.getSex().equals(null)){ValidationUtils.rejectIfEmpty(errors, "sex", "sex", "性别必须输入");}
        Perl5Util perl5Util=new Perl5Util();
        if(!perl5Util.match(SEX_REGEXP, student.getSex())){
            errors.rejectValue("sex", "not confirmat", null,"性别格式错误");}
    }

}



Student:一个简单的pojo
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package net.kingbit.pojos;

/**
*
* @author Kang
*/
public class Student {
   private String name;
   private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

}


跳转到表单页面的Controller必须是表单控制器!换颜色的地方就是要注意的地方!

分享到:
评论

相关推荐

    jakarta.xml.bind-api-2.3.3-API文档-中文版.zip

    Maven坐标:jakarta.xml.bind:jakarta.xml.bind-api:2.3.3; 标签:bind、xml、api、jakarta、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化...

    开源框架 Spring Gossip

    结合 JSTL &lt;spring:bind&gt; 标签 数据绑定的几个方法 &lt;spring:message&gt; 标签 &lt;spring:transform&gt; 标签 其它 View 层 除了 JSP View 层技术之外,您还可以使用其它的 View 层技术,或建立...

    使用Spring MVC表单标签_IT168文库.doc

    在低版本的Spring中,你必须通过JSTL或&lt;spring:bind&gt;将表单对象绑定到HTML表单页面中,对于习惯了Struts表单标签的开发者来说,Spring MVC的这一表现确实让人失望。不过这一情况已经一去不复返了,从Spring 2.0开始...

    springbind.zip

    springweb bind param

    springbind BindStatus

    自己总结: 后台rejectValue的设定方法 jsp界面的使用方法,属于分类很细了 又问题及时联系

    spring-web-2.5.jar

    org.springframework.web.bind.MissingServletRequestParameterException.class org.springframework.web.bind.RequestUtils.class org.springframework.web.bind.ServletRequestBindingException.class org.spring...

    spring开发中的jar包,json,annotation

    The import org.springframework.web.bind.annotation cannot be resolved. json.jar spring.jar mail.jar log4j.jar

    spring-framework-4.3.12.RELEASE

    Spring Framework 4.3.12 已发布,该版本包含 17 项修复和一些特定的改进,为即将推出的 Spring Boot 1.5.8 打好基础。请注意,Spring Framework 4.x 需要 JDK 6 及以上版本

    Spring-Reference_zh_CN(Spring中文参考手册)

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...

    Pivotal团队提供的微框架Spring Boot.zip

    import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example {  @RequestMapping("/")  String home() {  return "Hello World!";  }  ...

    javax.xml.bind-2.0.jar

    最近在学习Spring MVC 时候遇到了NoClassDefFoundError: javax/xml/bind/JAXBException 错误,产生原因是缺少javax.xml.bind Jar包.

    Guice与Spring框架的区别.pdf

    在这个方法中,我们使用了binder.bind方法将MyService接口绑定到MyServiceImpl实现类,并指定了Scopes.SINGLETON作用域,以确保MyServiceImpl对象是单例的。 Guice的其它使用特性 Guice还提供了一些其它的使用特性...

    Spring 2.0 开发参考手册

    2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@...

    spring-framework-3.0.5.RELEASE-dependencies-1

    javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...

    Spring中文帮助文档

    2.6.4. 将Spring 应用程序上下文部署为JCA adapter 2.6.5. 计划任务 2.6.6. 对Java 5 (Tiger) 支持 2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)...

    spring chm文档

    Spring Framework 开发参考手册 Rod Johnson Juergen Hoeller Alef Arendsen Colin Sampaleanu Rob Harrop Thomas Risberg Darren Davison Dmitriy Kopylenko Mark Pollack Thierry Templier Erwin ...

    Spring API

    2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...

    springboot 基础简易实例, maven项目

    import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; // @RestController返回的是json @RestController public class ...

    SPRING信息

    spring bind,SPRING信息mvc中的内置作用和操作

    spring-framework-3.0.5.RELEASE-dependencies-6

    javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...

Global site tag (gtag.js) - Google Analytics