2

我想生成一个动态网址,例如 http://example.com/path/to/page?name=ferret&color=purple

我可以将名称和颜色两个单独的输入字段从我的表单发送到控制器作为单个参数。有人可以帮我编写相应的jsp和spring控制器编码。

以下是我的控制器:

@RequestMapping(value = "/ws/jobs/{title}/{location}/{experience}")
     public ModelAndView openRequirementsRedirect(JobSearchRequest jobSearchRequest) throws Exception{
     ModelAndView model = new ModelAndView();
         model.addObject("title", jobSearchRequest.getTitle());
     model.addObject("location", jobSearchRequest.getLocation());
     model.addObject("experience", jobSearchRequest.getExperience());
     model.setViewName("openJobs/openjobs");
     return model;
}

我有 pojo :

public class JobSearchRequest {
    private String title;
    private String location;
    private String experience;
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public String getExperience() {
        return experience;
    }
    public void setExperience(String experience) {
        this.experience = experience;
    }
}

和 y jsp 调用是这样的:

window.location.href = example+"/abc?&title="+title+"&location="+location+"&experience="+experience;
4

2 回答 2

1

这是一个简单的例子:

@RequestMapping(value = "/project/{projectId}/{bookmark}", method = RequestMethod.GET)
public @ResponseBody boolean bookmarkProject(@PathVariable("projectId") UUID projectId,
                                             @PathVariable("bookmark") boolean bookmark) {
    return userService.bookmarkProject(projectId, bookmark);
}
于 2017-11-30T06:41:49.630 回答
0

基本上,您正在尝试读取查询参数,因此,您必须对 api 方法使用 @RequestParam 注释。

@RequestMapping(value = "/ws/jobs/{title}/{location}/{experience}")
public ModelAndView openRequirementsRedirect(   JobSearchRequest jobSearchRequest,
                                                @RequestParam(required = false, value = "name") String name,
                                                @RequestParam(required = false, value = "color") String color
 ) throws Exception{

     ModelAndView model = new ModelAndView();
     model.addObject("title", jobSearchRequest.getTitle());
     model.addObject("location", jobSearchRequest.getLocation());
     model.addObject("experience", jobSearchRequest.getExperience());
     model.setViewName("openJobs/openjobs");
     return model;
}

如果资源 url 是,上述方法将有效

http://example.com/title1/loc1/exp1?name=ferret&color=purple

于 2017-11-30T07:32:43.117 回答