2017-03-01 7 views
0

IntelliJを使用してSpringで簡単なアプリケーションを作成しようとしていて、ビルドツールとして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メソッドでは、モデル名を登録していません。春のフォームタグが壊れています。これはあなたの問題の原因かもしれません。 このようなコントローラの変更方法。

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

あなたの答えをありがとう。私はそれを変えましたが、それでも変わりません。何が間違っているかもしれない他のアイデアはありますか? .jspファイルのこの3つの最上位行は、ブラウザでテキストとして返されるため、正しく処理されていないようです。 – likebutter

+0

他の問題はありません。私はあなたのスクリーンショットのテキストフィールドも見ていませんでした。私は春のフォームタグが壊れていると仮定しました。他のブラウザでこれを試してみてください。 –

+0

私はこのタグを使用しないときはすべてが大丈夫です。純粋なhtmlを取得すると意味します。私はブラウザに問題があるとは思わない。私は3つのブラウザでこのページを取得しようとしましたが、それぞれ同じ結果を返します。 – likebutter

関連する問題