0

我已经通过 MVC 模板创建了 Spring MVC 项目,但我无法从 textinput 中获取值。有人可以建议我怎么做吗?带有项目结构的 home.jsp 的源代码可以在下面的屏幕截图中看到。AdminController 类的代码是:

@Controller
@RequestMapping(value = "/foo")
public class AdminController {

@RequestMapping(value = "/bar")
public String testAction(@RequestParam String fieldName) {
    // yourValue contain the value post from the html form
    return "yourview";
    }
}

这是来自包资源管理器和 home.jsp 源的屏幕截图

当我部署项目时,它从 address 开始http://localhost:8080/test/。哪里可以换成/test/别的?在点击提交按钮后,浏览器转发http://localhost:8080/foo/bar并显示HTTP status 404

4

2 回答 2

1

看看你在这里发布的图片。您的控制器请求映射是 /bar,而您形成的动作被称为 /foo/bar。看看这是不是错误。也为弹簧动作方法提供请求方法。

由于您使用 URL localhost:8080/foo/bar 访问应用程序,因此我建议对其进行一些更改。

从 Controller 中移除 RequestMapping 标签。它应该是 :

@Controller    
public class AdminController {

    @RequestMapping(value = "bar", method=RequestMethod.POST)
    public String testAction(@RequestParam String fieldName) {
      // yourValue contain the value post from the html form
       return "yourview";
    }
}

并给出表单动作名称,如 action="bar"。

于 2013-03-05T12:59:37.140 回答
1

404:

您的表单映射是绝对的

<form ... action="/foo/bar">

这就是为什么它正在命中http://localhost:8080/foo/bar并且您没有部署其上下文路径/foo因此为 404 的应用程序。

要更正此问题,请使用 spring 为您添加上下文路径:

<spring:url value="/foo/bar" var="form_url" />
<form ... action="${form_url}" method="POST">

字段名称:

您需要确定要映射到 testAction 参数的请求参数。

@RequestMapping(value = "/bar", method=RequestMethod.POST)
public String testAction(@RequestParam(value="fieldName") String fieldName) {
  // yourValue contain the value post from the html form
  return "yourview";
}

/测试:

看起来您正在使用 Tomcat 服务器来运行该应用程序。这可能在 pom.xml 中配置为使用 /${project.artifactId} (默认)启动应用程序,其中您的工件 ID 为“test”。(http://mojo.codehaus.org/tomcat-maven-plugin/run-mojo.html#path)。您可以通过配置 maven 插件来提供不同的值:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
      <path>WHATEVER</path>
    </configuration>
  </plugin>
于 2013-03-05T13:14:04.497 回答