首页 文章

Java Spring IntelliJ jstl标签不起作用

提问于
浏览
0

我试图用Spring做一个简单的应用程序,使用IntelliJ和gradle作为构建工具 . 我做了一个简单的面板,其中包含来自jstl库的输入,但是我认为它们是服务器的一些问题,因为浏览器没有显示任何输入 . 这是我的第一个Spring项目,我从未使用过IntelliJ和gradle,所以我有可能犯了一些我无法解决的愚蠢错误 .

createUser.jsp(src / main / webapp / WEB-INF / views):

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

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Dodaj usera</title>
</head>
<body>

<form:form method="POST" modelAttribute="userDTO">
    <h1>Podaj imie: </h1>
    <form:input type="text" path="name" /> 
<h1>Podaj nazwisko: </h1> <form:input type="text" path="secondName" />
<h1>Podaj nr telefonu: </h1> <form:input type="text" path="phoneNumber" />
<h1>Podaj e-mail: </h1> <form:input type="text" path="eMail" />
</form:form> </body> </html>

controller.java:

@Controller
public class Controler {

@RequestMapping("/hello")
String hello(){
    return "hello";
}

@RequestMapping(value = "/userForm", method = RequestMethod.GET)
String userFormGet(){
    return "createUser";
}

@RequestMapping(value = "/userForm", method = RequestMethod.POST)
String userFormPost(@ModelAttribute("form") @Valid UserDTO userDTO, BindingResult result){
    if(result.hasErrors()){
        return "createUser";
    }
    else
        return "redirect:/hello";
}
}

configuration.java类:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="com.petkow")
public class MvcConfiguration extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver getViewResolver() {
    InternalResourceViewResolver resolver = new  InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Bean
public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
}

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
}

的build.gradle:

buildscript {
ext {
    springBootVersion = '1.5.1.RELEASE'
}
repositories {
    mavenCentral()
}
dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")

}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
baseName = 'transport-service'
version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
compile group: 'javax.servlet', name: 'jstl', version: '1.2'
compile 'javax.validation:validation-api:1.1.0.Final'
compile 'org.hibernate:hibernate-validator:5.0.1.Final'
}

入门课程:

@SpringBootApplication
public class TransportServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(TransportServiceApplication.class, args);
}
}

数据传输对象类:

public class UserDTO {

@NotBlank
@Length(min=2, max=50)
private String name;
@NotBlank
@Length(min=2, max=50)
private String secondName;
@Min(9)
@Max(9)
private long phoneNumber;
@NotBlank
@Email
private String eMail;

public String getName() {
    return name;
}

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

public String getSecondName() {
    return secondName;
}

public void setSecondName(String secondName) {
    this.secondName = secondName;
}

public long getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(long phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String geteMail() {
    return eMail;
}

public void seteMail(String eMail) {
    this.eMail = eMail;
}
}

result from browser request:

1 回答

  • 0

    您的控制器代码有点错误 . 在get方法中,您没有注册模型名称 . Spring表单标签正在破碎 . 这可能是您遇到问题的原因 . 改变这样的控制器方法 .

    @RequestMapping(value = "/userForm", method = RequestMethod.GET)
    ModelAndView userFormGet(){
        return new ModelAndView("page","userDTO",new UserDTO());
    }
    

相关问题