社区首页 >问答首页 >spring boot 1.3.5表单: jsp未显示错误

spring boot 1.3.5表单: jsp未显示错误
EN

Stack Overflow用户
提问于 2016-07-11 19:34:54
回答 0查看 922关注 0票数 0

我正在将spring-mvc web应用程序从spring 4.X升级为spring boot war。

页面提供服务,表单被提交,验证被执行(并记录一个错误),但是jsp在表单中显示任何错误:错误

在THe -boot之外,spring-boot同样可以很好地工作。

为了确保我正确地设置了我的spring boot jsp应用程序,我简单地向现有的"spring-boot-sample-web-jsp“添加了一个表单post (参见https://github.com/spring-projects/spring-boot/tree/1.3.x/spring-boot-samples )

这是模型对象

代码语言:javascript
代码运行次数:0
复制
  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;
    }

}

以下是服务器端:

代码语言:javascript
代码运行次数:0
复制
@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("!Validation!");

    } else {

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

    return modelAndView;
}

下面是验证器

代码语言:javascript
代码运行次数:0
复制
@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变得混乱,所以略有删节)

代码语言:javascript
代码运行次数:0
复制
<%@ 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>

pom文件是(spring-boot-sample-web-jsp中未修改的)

代码语言:javascript
代码运行次数:0
复制
<?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>

==============

解决方案是不要在出错时创建新的模型对象(尽管在不是spring启动应用程序的情况下工作得很好):

代码语言:javascript
代码运行次数:0
复制
@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("!Validation!");

    } else {

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

    return modelAndView;
}
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38314802

复制
相关文章
spring boot 未授权访问
本来是在公司划水的一天,突然接到上级来电,丢给我一个目标站,让我帮忙透一透。
用户6343818
2021/01/26
2.5K0
spring boot 基于yml整合jsp
对应习惯了jsp开发的朋友来说,使用spring boot的时候也想使用jsp怎么办?本文将图文并茂的讲解怎么在spring boot中使用jsp。
凯哥Java
2019/06/28
8020
spring boot 基于yml整合jsp
spring boot jsp之Intellij异常
项目中用到spring boot进行带页面的开发,从github上的simples中下载了对应的spring-boot-sample-web-jsp项目,经简单修改之后死活启动不起来,各种异常。spring-boot以简单容易上手为核心宗旨,可为什么在引入jar时就会出现这么多问题呢?
程序新视界
2022/05/06
6030
深入Spring Boot (十二):集成JSP
话不多说,直接上代码。依赖管理pom.xml,需要添加servlet和嵌入式tomcat运行jsp需要的jar,详细依赖配置如下:
JavaQ
2018/07/26
8520
深入Spring Boot (十二):集成JSP
Spring Boot开启JSP页面热部署
在springboot中默认对jsp运行为生产模式,不允许修改内容保存后立即生效,因此在开发过程需要调试jsp页面每次需要重新启动服务器这样极大影响了我们的效率,为此springboot中提供了可以将默认的生产模式修改为调试模式,改为调试模式后就可以保存立即生效
诺浅
2020/08/21
6730
jsp表单验证
后面有代码,需要直接拿 toUpperCase()方法将字符串小写字符转换为大写 语法 public String toUpperCase() 或 public String toUpperCase(Locale locale) 参数 无 返回值 字符转换为大写后的字符串。 效果图如下: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE ht
天蝎座的程序媛
2022/11/18
3.1K0
jsp表单验证
Spring Boot错误处理
自定义一个类实现ErrorController,当系统发生404或者500错误的时候,就会自动进入到自定义的错误页面中,这要求在resources文件里面的templates文件内部建立一个error文件夹,里面放自定义错误页面的模板即可。当访问/error这个路径的时候,也会进入错误页面。
itlemon
2020/04/03
7090
spring boot 整合shiro 错误
1:No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
凯哥Java
2019/06/30
2.1K0
spring boot 整合shiro 错误
1:No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
凯哥Java
2022/12/15
1.3K0
spring boot 整合shiro 错误
Spring boot 数据源未配置异常
本文主要介绍了在Spring Boot项目中配置数据源,以及如何解决未配置数据源导致的异常问题。首先介绍了如何配置数据源,然后说明了Spring Boot在生成项目时自动注入数据源的原因。最后,提供了三种解决方案,包括排除数据源、在Spring Boot中排除数据源注入和在application.properties文件中配置数据源。
程序新视界
2018/01/08
1.6K0
Spring Boot 与 Kotlin 处理Web表单提交
我们在做web开发的时候,肯定逃不过表单提交,这篇文章通过Spring Boot使用Kotlin 语言 创建和提交一个表单。
全科
2018/08/15
7300
Spring Boot 测试错误 SQLFeatureNotSupported
因为上面的这句话 dataSource.getConnection( user, pass ) 在给出用户名和密码的时候提示不能获得连接池的错误。
HoneyMoose
2021/01/26
7080
Spring Boot 测试错误 SQLFeatureNotSupported
Spring Boot 测试错误 SQLFeatureNotSupported
因为上面的这句话 dataSource.getConnection( user, pass ) 在给出用户名和密码的时候提示不能获得连接池的错误。
HoneyMoose
2021/01/26
6490
Spring Boot 测试错误 SQLFeatureNotSupported
Spring Boot 与 Kotlin 验证web表单信息
在做web开发的时候,我们需要验证表单,确认用户提交的信息是安全的,比如用户名不能超过多少位,密码不能少于多少位等等。
全科
2018/08/15
1.2K0
spring boot整合jsp的时候访问页面错误日志:Path with "WEB-INF" or "META-INF":
虽然spring boot 官方不推荐使用jsp.然后凯哥qianqian的,想整合jsp。在整合过程中遇到了错误:
凯哥Java
2022/12/15
1.1K0
spring boot整合jsp的时候访问页面错误日志:Path with "WEB-INF" or "META-INF":
jsp表单的批量提交
如果数据量不大可以这样直接提交,如果数据一旦超过几十条那么会造成页面卡顿,同时ie会提示“此脚本运行时间过长,是否终止”,一旦点击是,那么表单就无法直接提交了;
西门呀在吹雪
2020/11/09
1.4K0
jsp表单的批量提交
spring boot整合jsp的时候访问页面错误日志:Path with "WEB-INF" or "META-INF":
虽然spring boot 官方不推荐使用jsp.然后凯哥qianqian的,想整合jsp。在整合过程中遇到了错误:
凯哥Java
2019/06/28
4.8K0
spring boot整合jsp的时候访问页面错误日志:Path with "WEB-INF" or "META-INF":
jsp实现表单提交跳转
处理页面,若用户名为“admin”,密码为“000”,则跳转到show.jsp,否则跳转login.jsp。
全栈程序员站长
2022/11/10
2.2K0
mybatis整合spring boot错误:.getTimeout()Ljava/lang/Integer;
在使用spring boot整合mybatis的时候,因为使用了自定义的分页拦截器。所以,依赖的jar有所升级。
凯哥Java
2019/07/01
1.3K0
点击加载更多

相似问题

Spring Boot 1.3.5范围会话

14

Spring Boot MVC应用程序JSP未显示

52

Spring Boot 1.3.5 Tomcat访问日志轮换

19

Spring-boot JSP未解析

11

Spring Boot未呈现JSP视图

20
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文