【问题标题】:Spring Framework JSP form postSpring Framework JSP 表单发布
【发布时间】:2016-09-21 15:21:41
【问题描述】:

我正在尝试使用 GlassFish Server 4.1.1 使用 Maven 和 NetBeans 8.1 中的 Spring 框架发布我的第一个 JSP 表单。

当我运行应用程序时,我的主页打开正常,这是我的 index.jsp,url 是 localhost:8080/mavenproject1。

当我单击表单上的提交按钮时,我的 url 是 localhost:8080/selectSearch 并出现 404 not found 错误。以下是我认为与完成这项工作和项目结构有关的文件。

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>

【问题讨论】:

    标签: java spring maven jsp spring-mvc


    【解决方案1】:

    发生的事情是找不到x.jsp。如果你要注释方法

    public String x(ModelMap map)
    

    喜欢这个

    public @ResponseBody String x(ModelMap map)
    

    然后它会简单地返回“x”,但就目前而言,它试图找到不存在的x.jsp

    或者您可以将其更改为返回 selectSearch,因为您有一个与该名称对应的视图,如下所示:

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

    【讨论】:

    • 我已经尝试了一些更改,但似乎没有任何东西可以解决这个问题。我注意到的是,如果我从 DefaultController 删除任何映射到 selectSearch 并将表单上的操作设置为 selectSearch 或 x 错误是相同的。似乎我的代码只是跳过了默认控制器。我是 Java 新手,所以我不确定是否需要为每个 jsp 设置一个控制器。
    • 我找到了 searchSelect jsp,但现在它显示的是 x 的返回值,而不是 searchSelect.jsp 中的 html
    • @dutchlab 那么它实际上并没有找到“searchSelect jsp”,它只是在您的Controller 中调用处理程序方法。您需要从您的方法中返回字符串“searchSelect”并且不要使用@ResponseBody注解
    • 是的,我刚刚想通了。谢谢。你把我推向了正确的方向。
    • 太棒了!很高兴听到。
    猜你喜欢
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2014-10-14
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2010-10-26
    相关资源
    最近更新 更多