Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 3.0.6 MVC@PathVariable和@RequestParam在JSP视图中为blank/empty_Spring_Spring 3_Spring Annotations - Fatal编程技术网

Spring 3.0.6 MVC@PathVariable和@RequestParam在JSP视图中为blank/empty

Spring 3.0.6 MVC@PathVariable和@RequestParam在JSP视图中为blank/empty,spring,spring-3,spring-annotations,Spring,Spring 3,Spring Annotations,我一直在尝试设置一个非常简单的控制器/视图,但就是无法让它工作。在我的web.xml中,我定义了一个名为servlet context.xml的,运行正常。在servlet context.xml中,我设置了: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframe

我一直在尝试设置一个非常简单的控制器/视图,但就是无法让它工作。在我的
web.xml
中,我定义了一个名为
servlet context.xml
,运行正常。在
servlet context.xml
中,我设置了:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"

<...other stuff in here... />

<mvc:annotation-driven />
在我的
student.jsp
视图中,我有:

<p>This is the page where you would edit the stuff for ${username}.</p>
<p>The URL parameter <code>studentid</code> is set to ${studentid}.</p>
当我请求
http://localhost:8080/application/student/xyz123/?studentid=456
,我得到了预期的视图,但所有变量都为空:

<p>This is the page where you would edit the stuff for .</p>
<p>The URL parameter <code>studentid</code> is set to .</p>
我怀疑我的
web.xml
servlet context.xml
的设置方式有问题,但我在任何地方都找不到罪魁祸首。据我所知,任何日志中都没有显示任何内容


更新:我的代码基于以下部分:

…这对我来说很好。我不明白为什么这个例子有效,但我的却不行。是因为他们使用的是
servlet context.xml

<annotation-driven conversion-service="conversionService">
    <argument-resolvers>
        <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
    </argument-resolvers>
</annotation-driven>

创建模型映射并将这些参数名称/值对添加到其中:

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
    model.put("username", username);
    model.put("studentid", studentid);

    return "student";
}

@PathVariable
意味着带注释的方法参数应该从被调用URL的路径中提取
@RequestParam
意味着必须从请求参数中提取带注释的方法参数。这些注释都不会导致将带注释的参数放入请求、会话或应用程序范围

${username}
表示“在响应中写入用户名属性的值(在页面、请求、会话或应用程序范围中)。因为在这些作用域中没有包含任何username属性,所以它不会写入任何内容


如果该方法返回一个ModelAndView对象,并且该模型包含一个
username
属性和一个
studentid
属性,那么代码就可以工作。

Aha!终于明白了

springmvcshocase
正在使用spring3.1,它将自动向模型公开
@PathVariable
s

正如@duffymo和@JB Nizet所指出的,对于早于3.1的Spring版本,使用
Model.put()
添加到模型中是需要做的事情

Ted Young用它为我指明了正确的方向。

  • @PathVariable
    是从uri中获取一些占位符(Spring称之为uri模板) -看
  • @RequestParam
    用于获取参数-请参阅
假设此Url
http://localhost:8080/MyApp/user/1234/invoices?date=12-2013年5月
(获取用户1234当前的发票)

@RequestMapping(value=“/user/{userId}/invoices”,method=RequestMethod.GET)
公共列表listUsersInvoices(
@PathVariable(“userId”)int user,
@RequestParam(value=“date”,required=false)date(日期或完整){
model.put(“userId”,用户);
型号:put(“日期”,dateOrNull);
}

非常感谢您的回答!我已经添加了一个来自Spring开发人员的示例——如果您能解释他们的工作原理,我将不胜感激。那么,也许我错了,Spring会自动将路径变量添加到模型中。参考文档说,只有在编译期间启用了cebugging时,这样的路径变量才起作用。看见尝试使用@PathVariable(“用户名”)和@PathVariable(“学生ID”)非常感谢您的回答!我已经添加了一个来自Spring开发人员的示例——如果您能解释他们的工作原理,我将不胜感激。
<annotation-driven conversion-service="conversionService">
    <argument-resolvers>
        <beans:bean class="org.springframework.samples.mvc.data.custom.CustomArgumentResolver"/>
    </argument-resolvers>
</annotation-driven>
@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
    model.put("username", username);
    model.put("studentid", studentid);

    return "student";
}
@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
            @PathVariable("userId") int user,
            @RequestParam(value = "date", required = false) Date dateOrNull) {
      model.put("userId", user);
      model.put("date", dateOrNull);

}