簡體   English   中英

Java Spring IntelliJ jstl標簽不起作用

[英]Java Spring IntelliJ jstl tags doesn't work

我試圖使用IntelliJ和gradle作為構建工具使用Spring做一個簡單的應用程序。 我用來自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" /> <br />
    <h1>Podaj nazwisko: </h1>
    <form:input type="text" path="secondName" /> <br />
    <h1>Podaj nr telefonu: </h1>
    <form:input type="text" path="phoneNumber" /> <br />
    <h1>Podaj e-mail: </h1>
    <form:input type="text" path="eMail" /> <br />
</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;
}
}

瀏覽器請求的結果:

您的控制器代碼有誤。 在get方法中,您沒有注冊模型名稱。 Spring表單標簽已損壞。 這可能是您遇到問題的原因。 像這樣更改Controller方法。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM