0

I am trying to get contentid value from the url querystring.

Ex: http://localhost:8080/app?contentid=10

I want to get the value of contentid is: 10 from the above url in my spring controller, how can I get this ?

Please note that if I pass any value there(like 10, 50,100, 200, ...etc - I can enter or pass any number to the url), so whatever I am passing to contentid, that value should get into my controller(System.out.println(contentid);).

Currently I am getting the below error after clicking of Submit button:

http://localhost:8080/app?contentid=

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='testData'. Error count: 1

I am getting below in my STS console.

2018-02-05 13:09:45.186  INFO 8428 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-02-05 13:09:45.187  INFO 8428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-02-05 13:09:45.231  INFO 8428 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 44 ms

Controller:

package com.example.demo;

import java.util.concurrent.ThreadLocalRandom;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@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";
    }
}

TestData.java:

package com.example.demo;
import java.io.Serializable; 
public class TestData implements Serializable { 
private static final long serialVersionUID = 1L; 
private String firstname; 
private String secondname; 
private int contentid; 

public TestData() { 
} 

//setters and getters

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); 
} 

}

app.html:

<!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>

pom.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>MySpringBootApp-2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>MySpringBootApp-2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
10
  • please post the detailed stack trace here Commented Feb 5, 2018 at 7:30
  • @dsharew, i posted the stack trace which i got in STS console. Commented Feb 5, 2018 at 7:41
  • you want to pass an array ? i mean like contentid= 10,20,30 ? or you would pass only one value. why is there a extra comma in your url? Commented Feb 5, 2018 at 9:07
  • @pvpkiran, i want to pass one value like: localhost:8080/app?contentid=10 or localhost:8080/app?contentid=20, etc(contentid can be any integer value). No comma in the url Commented Feb 5, 2018 at 9:28
  • the error message says, validation failed for testData. What are you passing as body in your post request? and how are you calling this url localhost:8080/app?contentid=10 ? from browser? Commented Feb 5, 2018 at 9:39

1 Answer 1

1

TestData.java has problema. int contentId can not be null. Change it to Integer contentId.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.