Note that there are some explanatory texts on larger screens.

plurals
  1. POvalidation form using Spring 3
    primarykey
    data
    text
    <p>i'm creating validation form on Spring 3, My problem is, that i saw a lot of examples with validation form. I even created one. but my form passing "result.hasErrors()" method, even when there are errors. </p> <p>My code is:</p> <p>Controller:</p> <blockquote> <p></p> </blockquote> <pre><code>package com.esb.sso; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import javax.servlet.http.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.validation.BindingResult; import java.util.Map; import javax.validation.Valid; import com.esb.sso.form.LoginForm; import javax.servlet.http.HttpServletRequest; /** * Handles requests for the application home page. */ @Controller @RequestMapping(value = "/") public class HomeController { LoginForm loginForm = new LoginForm(); model.put("loginForm", loginForm); return "home"; } @RequestMapping(method = RequestMethod.POST) public String validation(@Valid LoginForm loginForm, BindingResult result, Map model) throws IOException { logger.info("Login POST var"); logger.info(loginForm.getLogin()); logger.info(loginForm.getPassword()); if (result.hasErrors()) { logger.info("error"); return "home"; } model.put("loginForm", loginForm); return "logged"; } } </code></pre> <p>Validator:</p> <blockquote> <p>package com.esb.sso.form;</p> </blockquote> <pre><code>import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; public class LoginForm { @NotNull(message = "notNull") @Size(min=1, max=50, message="mote charters") private String login; @NotNull(message = "notNull") @Size(min=1, max=50, message="mote charters") private String password; public void setLogin(String login){ this.login = login; } public String getLogin(){ return login; } public void setPassword(String password){ this.password = password; } public String getPassword(){ return password; } } </code></pre> <p>View:</p> <blockquote> <p></p> </blockquote> <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Home&lt;/title&gt; &lt;/head&gt; &lt;body&gt; Autoryzacja!!! &lt;form:form action="" commandName="loginForm"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt;User Name:&lt;FONT color="red"&gt;&lt;form:errors path="login" /&gt;&lt;/FONT&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:input path="login" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Password:&lt;FONT color="red"&gt;&lt;form:errors path="password" /&gt;&lt;/FONT&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;form:password path="password" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="submit" value="Submit" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form:form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>web.xml:</p> <blockquote> <p></p> </blockquote> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/root-context.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;!-- Processes application requests --&gt; &lt;servlet&gt; &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;init-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt; &lt;/init-param&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;/&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>servlet-context:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt; &lt;!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --&gt; &lt;!-- Enables the Spring MVC @Controller programming model --&gt; &lt;annotation-driven /&gt; &lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --&gt; &lt;resources mapping="/resources/**" location="/resources/" /&gt; &lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --&gt; &lt;beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;beans:property name="prefix" value="/WEB-INF/views/" /&gt; &lt;beans:property name="suffix" value=".jsp" /&gt; &lt;/beans:bean&gt; &lt;context:component-scan base-package="com.esb.sso" /&gt; &lt;beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;beans:property name="basename" value="/WEB-INF/messages" /&gt; &lt;/beans:bean&gt; &lt;/beans:beans&gt; </code></pre> <p>and messages.properties:</p> <pre><code> NotNull.loginForm.login=must not be blank. NotNull.loginForm.password=must not be blank. Size.loginForm.login=Login size must be between 1 and 50 charters. Size.loginForm.password=Password size must be between 1 and 50 charters. </code></pre> <p><strong>I don't know where is the problem</strong></p> <p>Coul'd enybody help me ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload