2016-07-11 13 views
0

spring-mvc web appをspring 4.Xからバージョンアップして春ブート戦にしています。spring boot 1.3.5形式:jspのエラーが表示されない

ページでは、フォームがポストされ、機能、検証が実行(およびエラーを記録)が、JSPは、フォーム内のエラーを示していている:エラー

同じJSPは、ばねブーツの罰金外に動作します。

私は単に既存の「春・ブート・サンプル・ウェブのjsp」に、フォームのポストを追加しました私は正しく私の春ブーツJSPアプリを設定していてくださいするには(https://github.com/spring-projects/spring-boot/tree/1.3.x/spring-boot-samplesを参照)

ここでは、ここで

@Autowired 
private EmailSaveValidator emailSaveValidator; 

@RequestMapping("/saveEmail.html") 
public ModelAndView processEmail(@ModelAttribute("myModel") EmailHolderPageModel pageModel, BindingResult result){ 

    ModelAndView modelAndView = null; 

    emailSaveValidator.validate(pageModel, result); 

    if(result.hasErrors()){ 
     modelAndView = new ModelAndView("enterEmail"); 
     EmailHolderPageModel pm = new EmailHolderPageModel(""); 
     modelAndView.addObject("myModel", pm); 
     System.err.println("!!!Failed Validation!!!"); 

    } else { 

     modelAndView = new ModelAndView("thankyou"); 
     ThankyouPageModel thankYoupageModel = new ThankyouPageModel(); 
     modelAndView.addObject("thankyouModel", thankYoupageModel); 
    } 

    return modelAndView; 
} 

はバリ

次のとおりです。ここで

package sample.jsp; 
    import java.io.Serializable; 
    public class EmailHolderPageModel implements Serializable { 
    private String emailAddress; 

    public EmailHolderPageModel() { 
     super(); 
    } 

    public EmailHolderPageModel(String emailAddress) { 
     super(); 
     this.emailAddress = emailAddress; 
    } 


    public String getEmailAddress() { 
     return emailAddress; 
    } 

    public void setEmailAddress(String emailAddress) { 
     this.emailAddress = emailAddress; 
    } 

} 

モデルオブジェクトは、サーバー側でありますここ

@Component 
public class EmailSaveValidator implements Validator { 

    public boolean supports(Class candidate) { 
     return EmailHolderPageModel.class.isAssignableFrom(candidate); 
    } 

    public void validate(Object obj, Errors errors) { 

     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailRequired", "required field"); 

    } 
} 

は、JSP(stackoverflowのが混乱してきているので、少し切り捨て)

<%@ page session="false"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 

<html> 
<head> 
    <title>test entering email</title> 
</head> 
<body> 

    <form:form commandName="myModel" method="POST" action="saveEmail.html" > 
     <form:errors path="emailAddress" htmlEscape="false" /> 
     <div id="formIntro"> 
      <spring:message text="enter email address" /> 
      <p><strong><label> 
       <spring:message text="email address:" /> </label><form:input path="emailAddress" size="35" maxlength="200"/> 
       </label> 
      </strong></p> 
     </div> 
     <input type="submit" value="Submit" /> 
    </form:form> 

</body> 
</html> 

ポンポンファイルされる(春・ブート・サンプルウェブ-JSPから変更されていない)

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <parent> 
     <!-- Your own application should inherit from spring-boot-starter-parent --> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-samples</artifactId> 
     <version>1.3.6.BUILD-SNAPSHOT</version> 
    </parent> 
    <artifactId>spring-boot-sample-web-jsp</artifactId> 
    <packaging>war</packaging> 
    <name>Spring Boot Web JSP Sample</name> 
    <description>Spring Boot Web JSP Sample</description> 
    <url>http://projects.spring.io/spring-boot/</url> 
    <organization> 
     <name>Pivotal Software, Inc.</name> 
     <url>http://www.spring.io</url> 
    </organization> 
    <properties> 
     <main.basedir>${basedir}/../..</main.basedir> 
     <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot> 



    </properties> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 


     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</artifactId> 
      <scope>provided</scope> 
     </dependency> 




     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-validator</artifactId> 
     </dependency> 



    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <configuration> 
        <useSystemClassLoader>false</useSystemClassLoader> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

です==============

そして解決策は、エラー時に新しいモデルオブジェクトを作成しないことです(ただし、スプリングブートアプリケーションでは機能しません)。

@RequestMapping("/saveEmail.html") 
public ModelAndView processEmail(@ModelAttribute("myModel") EmailHolderPageModel pageModel, BindingResult result){ 

    ModelAndView modelAndView = null; 

    emailSaveValidator.validate(pageModel, result); 

    if(result.hasErrors()){ 
     modelAndView = new ModelAndView("enterEmail"); 
     // !! SOLUTION !! 
     // DO NOT CREATE A NEW MODEL OBJECT 
     // !! SOLUTION !! 
     // EmailHolderPageModel pm = new EmailHolderPageModel(""); 
     modelAndView.addObject("myModel", pageModel); 
     System.err.println("!!!Failed Validation!!!"); 

    } else { 

     modelAndView = new ModelAndView("thankyou"); 
     ThankyouPageModel thankYoupageModel = new ThankyouPageModel(); 
     modelAndView.addObject("thankyouModel", thankYoupageModel); 
    } 

    return modelAndView; 
} 
+0

私はブートを使用していないが、あなたは可能試すことができますコアスプリングクラスのログをデバッグし、それが何かの光を放つかどうかを確認してください – bphilipnyc

答えて

0

commandNameは、モデルクラスの名前と同じでなければなりません。

public class EmailHolderPageModel{}... 

そして、あなたのコントローラ内の任意の関数:

public ModelAndView anythink(@Valid EmailHolderPageModel email...) 

だから、あなたの中にJSPフォームは次のようになります。

<form:form commandName="emailHolderPageModel" />... 
+0

私の場合は、私のケースでは、渡された同じモデルオブジェクトを返すことによって解決されました。しかし、それは必要でした:) –

+0

...... \ o/...... –

関連する問題