首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将querystring的值从url传递到spring控制器?

如何将querystring的值从url传递到spring控制器?
EN

Stack Overflow用户
提问于 2018-02-02 14:59:21
回答 2查看 2.6K关注 0票数 0

我想要从给定的url中获取查询字符串的传递值,比如:http://localhost:8080/app?contentid=10我想从spring控制器中的上述url中获取contentid is: 10的值,我该如何获取呢?

请注意,如果我在那里传递任何值(比如10,50,100,200,...etc -它可以是任何数字),所以无论我传递给contentid的是什么,这个值都应该进入我的控制器。我只想从那个url获取这个HTML值,而不是从我的contentid页面,或者我不想从我的html页面传递。目前我在控制器中从下面的代码中获得null,我没有得到传递的值(比如从url中得到10 )。我如何才能做到这一点?提前感谢您的帮助!

app.html:

代码语言:javascript
运行
复制
<form action="#" th:action="@{/app}" th:object="${TestData}" method="post">
             <div class="form-group">
                <label for="firstname">First Name: </label> <input type="text"
                    class="form-control" id="firstname" name="firstname" ></textarea>
            </div> 
            <div class="form-group">
                <label for="secondname">Second Name:</label> <input type="text"
                    class="form-control" id="secondname" name="secondname" />
            </div>  
            <button type="submit" value="Submit">Submit</button>
            </form>

TestData.java:

代码语言:javascript
运行
复制
public class TestData implements Serializable{
    private String firstname;
    private String secondname;
    private int contentid;

    //setters and getters

    public TestData() {}
     public TestData(String firstname, String secondname, int contentid, ){
     this.firstname = firstname;
     this.secondname = secondname;
     this.contentid = contentid;
     }

     @Override
        public String toString() {
            return String.format(
                    "TestData[firstname=%s, secondname=%s, contentid=%d]",
                    firstname, secondname, contentid);
    }

}

控制器:

代码语言:javascript
运行
复制
public class TestDataController {
    @Autowired
   TestService testDataService;
     @RequestMapping(value="/app", method=RequestMethod.POST)
     public String testDataSubmit(@RequestParam(required=false) Integer contentid, @ModelAttribute TestData testData, Model model, HttpServletRequest request) {
     String id = request.getQueryString();
     System.out.println("My Id: "+id);//null 
     System.out.println("URL Parameter: "+testData.getContentid());//0
     System.out.println("URL Parameter passed objectid: "+contentid); //null
     testDataService.saveTestDataDetails(testData);
      return "app";
         }

    }

服务:

代码语言:javascript
运行
复制
@Service
public class TestService {

    @PersistenceContext
    private EntityManager entityManager;

    public void saveTestDataDetails(TestData testData) {
    StoredProcedureQuery sp = entityManager.createStoredProcedureQuery("APP.TESTDATA");
     sp.registerStoredProcedureParameter("firstname", String.class, ParameterMode.IN);
     sp.registerStoredProcedureParameter("secondname", Integer.class, ParameterMode.IN);
     sp.registerStoredProcedureParameter("contentid", Integer.class, ParameterMode.IN);

     sp.setParameter("firstname", testData.getFirstname());
     sp.setParameter("secondname", testData.getSecondname());
     sp.setParameter("contentid", testData.getContentid());     

    sp.execute();

    }

    }
EN

回答 2

Stack Overflow用户

发布于 2018-02-02 17:37:18

在每次聊天讨论后,我将为您提供一个有效的解决方案,您可以根据自己的需要指定任何东西。

app.html:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"
    xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<head>

<title></title>

</head>
<body>

<th:block th:if="${contentid != null}">
<div th:text="${'contentId: ' + contentid}"></div>
</th:block>

    <form action="#" th:action="@{/app(contentid=${contentid})}" th:object="${TestData}"
        method="post">
        <div class="form-group">
            <label for="firstname">First Name: </label>
            <input type="text" class="form-control" id="firstname"
                name="firstname" />
        </div>
        <div class="form-group">
            <label for="secondname">Second Name:</label>
            <input type="text" class="form-control" id="secondname"
                name="secondname" />
        </div>
        <button type="submit" value="Submit">Submit</button>
    </form>
</body>
</html>

控制器:

代码语言:javascript
运行
复制
@Controller
public class MyController {

    @GetMapping("/app")
    public String getApp(Model model) {
        return "app";
    }

    @PostMapping("/app")
    public String postApp(@RequestParam(required=false) Integer contentid, @ModelAttribute TestData testData, Model model, HttpServletRequest request) {
        System.out.println(contentid);
        if(contentid == null) {
            contentid = ThreadLocalRandom.current().nextInt(0, 100 + 1);
        }
        model.addAttribute("contentid",contentid);
        return "app";
    }

}
票数 1
EN

Stack Overflow用户

发布于 2018-02-02 15:55:08

首先,更改html代码th:action="@{/app?contentid=10},然后为类TestData中的实例变量添加setter和getter方法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48577315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档