2016-09-21 16 views
0

GlassFish Server 4.1.1を使用して、NetBeans 8.1でMavenとSpring Frameworkを使用して私の最初のJSPフォーム投稿を試みています。Spring Framework JSPフォーム投稿

アプリケーションを実行すると、私のホームページが正常に開きます。これはindex.jspであり、urlはlocalhost:8080/mavenproject1です。

フォームの[送信]ボタンをクリックすると、urlはlocalhost:8080/selectSearchに404エラーが見つかりません。以下は、私がこの仕事とプロジェクトの構造を作ることに関係すると思うファイルです。

enter image description here

のpom.xml

<?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> 

<groupId>com.mycompany</groupId> 
<artifactId>mavenproject1</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>war</packaging> 

<name>mavenproject1</name> 

<properties> 
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <spring.version>4.0.1.RELEASE</spring.version> 
    <jstl.version>1.2</jstl.version> 
    <javax.servlet.version>3.0.1</javax.servlet.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>mssql</groupId> 
     <artifactId>sqljdbc42</artifactId> 
     <version>4.2.6420.100</version> 
     <scope>runtime</scope> 
    </dependency> 

    <dependency> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>exec-maven-plugin</artifactId> 
     <version>1.2.1</version> 
    </dependency> 

    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-web-api</artifactId> 
     <version>7.0</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>${spring.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>javax.servlet</groupId> 
     <artifactId>javax.servlet-api</artifactId> 
     <version>${javax.servlet.version}</version> 
     <scope>provided</scope> 
    </dependency> 

    <dependency> 
     <groupId>jstl</groupId> 
     <artifactId>jstl</artifactId> 
     <version>${jstl.version}</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.0.1.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib</artifactId> 
     <version>3.2.4</version> 
    </dependency> 

</dependencies> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>3.1</version> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
       <compilerArguments> 
        <endorseddirs>${endorsed.dir}</endorseddirs> 
       </compilerArguments> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-war-plugin</artifactId> 
      <version>2.3</version> 
      <configuration> 
       <failOnMissingWebXml>false</failOnMissingWebXml> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-dependency-plugin</artifactId> 
      <version>2.6</version> 
      <executions> 
       <execution> 
        <phase>validate</phase> 
        <goals> 
         <goal>copy</goal> 
        </goals> 
        <configuration> 
         <outputDirectory>${endorsed.dir}</outputDirectory> 
         <silent>true</silent> 
         <artifactItems> 
          <artifactItem> 
           <groupId>javax</groupId> 
           <artifactId>javaee-endorsed-api</artifactId> 
           <version>7.0</version> 
           <type>jar</type> 
          </artifactItem> 
         </artifactItems> 
        </configuration> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

WebInitializer.java

package com.mycompany.config; 

import javax.servlet.ServletContext; 
import javax.servlet.ServletException; 
import javax.servlet.ServletRegistration.Dynamic; 

import org.springframework.web.WebApplicationInitializer; 
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; 
import org.springframework.web.servlet.DispatcherServlet; 

public class WebInitializer implements WebApplicationInitializer { 

    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException {   
     AnnotationConfigWebApplicationContext ctx = new  AnnotationConfigWebApplicationContext(); 
     ctx.register(AppConfig.class); 
     ctx.setServletContext(servletContext);  
     Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 
     servlet.addMapping("/"); 
     servlet.setLoadOnStartup(1); 
    } 

} 

AppConfig.java

package com.mycompany.config; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
     import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.springframework.web.servlet.view.JstlView; 
import org.springframework.web.servlet.view.UrlBasedViewResolver; 


@Configuration 
@ComponentScan("com.mycompany") 
@EnableWebMvc 
public class AppConfig extends WebMvcConfigurerAdapter { 

    @Bean 
     public UrlBasedViewResolver setupViewResolver() { 
     UrlBasedViewResolver resolver = new UrlBasedViewResolver(); 
     resolver.setPrefix("/WEB-INF/jsp/"); 
     resolver.setSuffix(".jsp"); 
     resolver.setViewClass(JstlView.class); 
     return resolver; 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/*"); 
    } 
} 

DefaultController.java

package com.mycompany.controllers; 


import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 



@Controller 
public class DefaultController { 
     public static void main(String[] args) throws Exception { 

    } 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public String index(ModelMap map) { 

     return "index"; 
    } 

    @RequestMapping(value = "/searchSelect", method = RequestMethod.POST) 
    public String x(ModelMap map) { 
     return "x"; 
    } 

} 

は何が起こっているJSPフォーム

<form id="agreeForm" class="form" method="post" action="/searchSelect"> 
    <div class="row"> 
     <div class="col-lg-4"> 
      <div class="input-group"> 
       <label class="checkbox-inline"><input type="checkbox" id="agreeChk" name="agreeChk" required>Agree</label> 
      </div> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="col-lg-4"> 
      <div class="input-group"> 
       <button type="submit" class="btn btn-success">Continue</button> 
      </div> 
     </div> 
    </div> 
</form> 

答えて

0

はそれがx.jspを見つけることができないです。あなたはこの

public @ResponseBody String x(ModelMap map) 

のような方法に

public String x(ModelMap map) 

に注釈を付けた場合、それは単に「x」を返しますが、それが今立っているとして、存在しないx.jspを見つけようとしています。このよう

それとも、その名前に対応するビューを持っているので、あなたが代わりにselectSearchを返すために、それを変更することができ、私はいくつかの変更と何もいじってい

@RequestMapping(value = "/searchSelect", method = RequestMethod.POST) 
public String x(ModelMap map) { 
    return "searchSelect"; 
} 
+0

は、問題を修正するようです。私が気づいたのは、DefaultControllerからselectSearchへのマッピングを削除し、フォーム上のアクションをselectSearchまたはxに設定した場合のエラーは同じです。私のコードはデフォルトコントローラをスキップしているようです。私はJavaに新しいので、私は各JSPのコントローラが必要かどうかわかりません。 – dutchlab

+0

searchSelect jspが見つかりましたが、xの戻り値がsearchSelect.jspのhtmlではなく – dutchlab

+0

@ dutchlabになっています。実際には、「controllerS」のハンドラメソッドを呼び出すだけの "searchSelect jsp" 。メソッドから "searchSelect"という文字列を返す必要があります。また、@ ResponseBodyアノテーションを使用しないでください。 –