Использование @QueryParam в Spring - PullRequest
0 голосов
/ 09 мая 2020

Я ищу простой пример использования @QueryParam в Spring. Я думал о том, чтобы иметь ссылку, которая выглядит так, чтобы получить значение id, запустить мой класс java и представить возвращаемую строку. Моя проблема в том, что возвращаемое значение не найдено.

http://localhost: 8080 / recipesinfo? Id = 274596

Произошла непредвиденная ошибка (тип = внутренняя ошибка сервера, статус = 500). Исключение, оценивающее выражение SpringEL: "recipesinfo.step" (шаблон: "recipesinfo_result" - строка 12, столбец 22)

DemoApplication. java

RecipesInfo repinfo = new RecipesInfo();
@RequestMapping("/recipesinfo")
public String getInfo (@RequestParam("id") int id) throws IOException {

    repinfo.execute(id);

    return "recipesinfo_result";
 }




public static void main(String[] args) throws Exception {
    SpringApplication.run(DemoApplication.class, args);

}        

recipesinfo_result. html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Recipes</title>
</head>
<body>
<p class="title"><a  th:text="${recipesinfo.step}"></a><br></body>
</html>

RecipesInfo. java

public String execute(int id) throws IOException { 

//using id in a Http request here and returning String step (the problem isn't here)

      return step;
      }

Я сделал нечто подобное в веб-службе, которая работает, но она просто возвращает JSON.

1 Ответ

0 голосов
/ 09 мая 2020

Вам необходимо привязать параметры запроса к модели, чтобы передать ее на вашу страницу просмотра.

@RequestMapping("/recipesinfo")
public String getInfo (@RequestParam("id") int id, Model theModel)) throws IOException {
    theModel.addAttribute("recipesinfo",id);

    return "recipesinfo_result";
 }

recipesinfo_result. html

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Recipes</title>
</head>
<body>
<p class="title"><a  th:text="${recipesinfo}"></a><br></body>
</html>
...