2017-03-01 74 views
0

我試圖用IntelliJ和gradle作爲構建工具來做一個簡單的應用程序。我用jstl庫的輸入做了一個簡單的面板,但是他們是我猜想的服務器的一些問題,因爲瀏覽器沒有顯示任何輸入。這是我的第一個Spring項目,我從來沒有使用過IntelliJ和Gradle,所以有可能我犯了一個愚蠢的錯誤,我無法解決。Java Spring IntelliJ jstl標籤不起作用

createUser.jsp(SRC /主/ web應用/ WEB-INF /觀看次數):

<%@ 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; 
} 
} 

result from browser request:

+0

請分享[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 – CrazyCoder

回答

0

控制器代碼是稍有錯誤的。在get方法中,您沒有註冊模型名稱。 Spring窗體標籤正在打破。這可能是你的問題的原因。 更改這樣的控制器方法。

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

感謝您的回答。我改變了它,但仍然一樣。你有什麼其他想法可能是錯誤的?看起來像.jsp文件中的這三個頂部行沒有正確處理,因爲我在瀏覽器中將它們作爲文本返回。 – likebutter

+0

我沒有看到任何其他問題。我也沒有在屏幕截圖中看到文本字段。我認爲春天的形式標籤正在打破。在其他一些瀏覽器中試用。 –

+0

當我不使用這個標籤時,一切正常,我的意思是當我得到純html。我不認爲瀏覽器有問題。我試圖在三個瀏覽器上獲取這個頁面,每個都返回相同的結果。 – likebutter