Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Passing a list of String from Spring Controller to Javascripts

I would like to pass a list of String from Spring Controller to Javascripts. The Controller :

@PostMapping("/level1")
public String login (Model model){
    List<Question> questions = frageRepository.findAllQuestions();
    List<String> questionText = questions.stream().map(Frage::getText).toList();  
    model.addAttribute("questionText", questionText);
    log.info(spieler.toString());
    return "level1";
}

the body of level1.html

    <body>
       <div class="container mx-auto">
    
        <label class="text-center mt-4" id="thetext">Du liebst Abenteuer und möchte endlich reisen. 
         <br>
            Du hast ein bisschen Geld und das reicht für ein Ticket zur nächsten Insel.<br>
            Unglücklicherweise ist dein Schiff in einem Sturm gesunken. <br>
            Du hat glück, an einen Strand zu landen. <br><br>
            Jetzt musst du die Herausforderungen mit deinen SQL-Kenntnissen meistern.<br>
            Je schneller du die Probleme löst, desto besser ist dein Ranking.<br><br>
            Viel Spaß !!!<br>
        </label>
        <div class="d-flex justify-content-center">
            <input id="startButton" onclick="myFunction()" class="btn-secondary mt-4"
                   type="button" value="next">
        </div>
      </div>
       <script>
          var questionText = [[${questionText}]];
       </script>
       <script th:src="@{/script/questions.js}"></script>
   </body>

the question.js file

var i = 0;

function myFunction() {
    document.getElementById("thetext").innerHTML = questionText[i];
    i++;
}

How can i pass the list questionText to Javascripts code? I want that when the user clicks the next button, the browser will show him the next question in label "thetext" from the list. Or Is there another way to do this? I hope, you can understand me :(

Thank you very much !

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

just input th:inline="javascript" in the script tag

<script input th:inline="javascript">
    var questionText = [[${questionText}]];
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!