1

The other solutions didn't help me. If the spring project is imported into Eclipse, the other solutions don't work. When I click on the enter key, after entering the data, the url to the page that should return the result is: http://localhost:8080/getParlamentare4?circoscrizione=Calabria

ParlamentareController.java

//@GetMapping("/cerca_parlamentare_dalla_circoscrizione")
@RequestMapping("/cerca_parlamentare_dalla_circoscrizione")
public ModelAndView getParlamentare4(@RequestParam String circoscrizione)
{
    ModelAndView mv = new ModelAndView();
    List<Parlamentare> parlamentari = 
 parlamRepo.findByCircoscrizione(circoscrizione);
    System.out.println(parlamRepo.findByCircoscrizione(circoscrizione));
    mv.addObject(parlamentari);
    mv.setViewName("lista_parlamentari_circoscrizione.jsp");
    return mv;  
}

If I put the Timeleaf dependency, it doesn't even open the initial index page for me. @RestController e @ResponseBody didn't work, also because @ResponseBody is not for me.

web.xml

 <servlet>
 <description></description>
 <display-name>Cerca_parlamentare_dalla_circoscrizione</display-name>
 <servlet-name>Cerca_parlamentare_dalla_circoscrizione</servlet-name>
 <servlet- 
 class>com.giuggiola.controller.Cerca_parlamentare_dalla_circoscrizione</servlet- 
 class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Cerca_parlamentare_dalla_circoscrizione</servlet-name>
 <url-pattern>/cerca_parlamentare_dalla_circoscrizione</url-pattern>
 </servlet-mapping>

cerca_parlamentare_dalla_circoscrizione.jsp

    <body>

    <h2>Ricerca parlamentare in base alla circoscrizione:</h2>
    <form action="getParlamentare4"> <!-- action="show_parlamentare3.jsp"getParlamentare4 
    -->
    <table>
    <tr>
    <td>Circoscrizione:</td>
  <td> <input type="text" name="circoscrizione"><br></td>
   </tr>
   <tr>
   <td><input type="submit" value="invio"><br></td>
   </tr>
   </table>
</form>
  
</body>
</html>

lista_parlamentari_circoscrizione.jsp

 <title>Lista parlamentari di quella circoscrizione</title>
</head>
<body>

 <!--   <form action="getParlamentare4"> -->
     <c:forEach items="${parlamentari}" var="parlamentare">

<td>Nome:</td>
    <td><c:out value="${parlamentare.getPk().getNome()}"/></td>
    <br>
    </tr>
    <tr>
     <td>Partito:</td>
    <td><c:out value="${pk.partito}"/></td><br>
    </tr>
    <tr>
     <td>Circoscrizione:</td>
   <c:forEach items="${pk.getCircoscrizione()}" var="CIRCOSCRIZIONE">
      <td><c:out value="${CIRCOSCRIZIONE}"/></td> <br>
       </c:forEach>
   </tr>
   <br>
    <tr>
   <td>Data di nascita:</td>
   <td><c:out value="${parlamentare.data_nascita}"/><br></td>
 </tr>
 <tr>
     <td>Luogo:</td>
   <td><c:out value="${parlamentare.luogo}"/><br></td>
   </tr>
   <tr>
     <td>Titolo di studi:</td>
   <td><c:out value="${parlamentare.titolo_studi}"/><br></td>
   </tr>
   <tr>
   <td>Mandati:</td><br>
   <c:forEach items="${parlamentare.mandati}" var="mandato">
       <table>
        <tr>
       <td>Mandato:</td> 
       <td><c:out value="${mandato}"/></td><br>
       </tr>
       </table>
       </c:forEach>
 </tr>
 
 <tr>
     <td>Commissioni:</td>
      <c:forEach items="${parlamentare.commissioni}" var="commissione">
       <table>
        <tr>
       <td>Commissione:</td> 
       <td><c:out value="${commissione}"/></td><br>
       </tr>
       </table>
       </c:forEach>
</c:forEach>

<!--   ${parlamentari} -->
</body>
</html>

STACKTRACE:

2022-10-10 16:21:42.171 DEBUG 5128 --- [nio-8080-exec-6] 
o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error? 
circoscrizione=Calabria", parameters={masked}
2022-10-10 16:21:42.172 DEBUG 5128 --- [nio-8080-exec-6] 
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to 
org.springframework.boot.autoconfigure.web.servlet.error.
BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-10-10 16:21:42.205 DEBUG 5128 --- [nio-8080-exec-6] 
o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, 
text/html;q=0.8]
2022-10-10 16:21:42.205 DEBUG 5128 --- [nio-8080-exec-6] 
o.s.web.servlet.view.JstlView            : View name 'error', model {timestamp=Mon 
Oct 10 16:21:42 CEST 2022, status=404, error=Not Found, message=No message available, 
path=/getParlamentare4}
2022-10-10 16:21:42.222 DEBUG 5128 --- [nio-8080-exec-6] 
o.s.web.servlet.DispatcherServlet        : Error rendering view 
[org.springframework.web.servlet.view.JstlView: name 'error'; URL [error]]

javax.servlet.ServletException: Circular view path [error]: would dispatch back to 
the current handler URL [/error] again. Check your ViewResolver setup! (Hint: This 
may be the result of an unspecified view, due to default view name generation.)
    at 
org.springframework.web.servlet.view.InternalResourceView.
prepareForRendering(InternalResourceView.java:210) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.view.InternalResourceView.
renderMergedOutputModel(InternalResourceView.java:148) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.view.AbstractView.render
(AbstractView.java:316) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.DispatcherServlet.render
(DispatcherServlet.java:1405) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.DispatcherServlet.processDispatchResult
(DispatcherServlet.java:1149) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.DispatcherServlet.doDispatch
(DispatcherServlet.java:1088) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.DispatcherServlet.doService
(DispatcherServlet.java:964) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.FrameworkServlet.processRequest
(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at org.springframework.web.servlet.FrameworkServlet.doGet
(FrameworkServlet.java:898) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.65.jar:4.0.FR]
        at org.springframework.web.servlet.FrameworkServlet.service
(FrameworkServlet.java:883) ~[spring-webmvc-5.3.23.jar:5.3.23]
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) 
~[tomcat-embed-core-9.0.65.jar:4.0.FR]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.springframework.web.filter.RequestContextFilter.doFilterInternal
(RequestContextFilter.java:100) ~[spring-web-5.3.23.jar:5.3.23]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter
(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter
(OncePerRequestFilter.java:102) ~[spring-web-5.3.23.jar:5.3.23]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.springframework.web.filter.OncePerRequestFilter.doFilter
(OncePerRequestFilter.java:102) ~[spring-web-5.3.23.jar:5.3.23]
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter
(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationFilterChain.doFilter
(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationDispatcher.invoke
(ApplicationDispatcher.java:711) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationDispatcher.processRequest
(ApplicationDispatcher.java:461) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationDispatcher.doForward
(ApplicationDispatcher.java:385) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.ApplicationDispatcher.forward
(ApplicationDispatcher.java:313) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.StandardHostValve.custom
(StandardHostValve.java:403) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.StandardHostValve.status
(StandardHostValve.java:249) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.StandardHostValve.invoke
(StandardHostValve.java:171) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.valves.ErrorReportValve.invoke
(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.core.StandardEngineValve.invoke
(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.catalina.connector.CoyoteAdapter.service
(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.coyote.http11.Http11Processor.service
(Http11Processor.java:399) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.coyote.AbstractProcessorLight.process
(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process
(AbstractProtocol.java:890) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun
(NioEndpoint.java:1789) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.tomcat.util.net.SocketProcessorBase.run
(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run
(TaskThread.java:61) ~[tomcat-embed-core-9.0.65.jar:9.0.65]
        at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]

ParlamentareRepository.java

@Query(value="select nome from Parlamentari where 
circoscrizione=?1 order by nome",nativeQuery = true)
public List<Parlamentare> findByCircoscrizione(String 
circoscrizione);

Thanks to who helps me.

I added the file:

WebConfig.java

@EnableWebMvc
@Configuration
@ComponentScan("com.giuggiola.controller")
public class WebConfig implements WebMvcConfigurer {
    // All web configuration will go here
    
    
    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver bean = new 
         InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/webapp/");
        bean.setSuffix(".jsp");
        return bean;
    }
    
    @Bean
    public BeanNameViewResolver beanNameViewResolver(){
        return new BeanNameViewResolver();
    }
    
    @Bean
    public View lista_parlamentari_circoscrizione() { //JstlView
    return  new 
    JstlView("/webapp/lista_parlamentari_circoscrizione.jsp");
     }

I edited web.xml with

    <servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

on the page cerca_parlamentare_dalla_circoscrizione.jsp

<form action="/Parlamento/lista_parlamentari_circoscrizione"> 

because it is the secondary page where you see the result inserted in the page cerca_parlamentare_dalla_circoscrizione.jsp

ParlamentareController.java

@GetMapping("cerca_parlamentare_dalla_circoscrizione")
//@GetMapping("/lista_parlamentari_circoscrizione")
//@RequestMapping("/cerca_parlamentare_dalla_circoscrizione")
public String 
getParlamentare4(@RequestParam(value="circoscrizione[]") 
String[] circoscrizione)
{//public String 
controllerMethod(@RequestParam(value="myParam[]") String[] 
myParams){
    ModelAndView mv = new ModelAndView();
    List<Parlamentare> parlamentari = 
parlamRepo.findByCircoscrizione(circoscrizione);
System.out.println(parlamRepo.findByCircoscrizione
(circoscrizione));
    mv.addObject(parlamentari);
    mv.setViewName("lista_parlamentari_circoscrizione.jsp");
    return "lista_parlamentari_circoscrizione";
            //mv;   
}

I have tried all the combinations in the getMapping

 /Parlamento/lista_parlamentari_circoscrizione
 /lista_parlamentari_circoscrizione
 /Parlamento/cerca_parlamentare_dalla_circoscrizione
 /cerca_parlamentare_dalla_circoscrizione

STACKTRACE:

 2022-10-12 11:31:41.074 DEBUG 1696 --- [nio-8080-exec-1] 
 o.s.web.servlet.DispatcherServlet        : Error rendering 
 view [org.springframework.web.servlet.view.JstlView: name 
 'error'; URL [error]]

 javax.servlet.ServletException: Circular view path [error]: 
 would dispatch back to the current handler URL 
 [/Parlamento/error] again. Check your ViewResolver setup! 
 (Hint: This may be the result of an unspecified view, due to 
 default view name generation.)

    
  • If you use Spring mvc, you need to define the DispatcherServlet in your web.xml, not your controller. Then, you will also need a ViewResolver. Then, your form `action` has to point to the path defined by the value your (commented out) `@GetMapping` annotation (which is also a bit messi since you usually submit forms via POST or PUT). See [this link](https://www.baeldung.com/spring-boot-dispatcherservlet-web-xml) to figure out how Spring mvc works. – The Frozen One Oct 10 '22 at 14:47
  • @The Frozen One Look at the changes please – utente1234567890 Oct 12 '22 at 09:56
  • web.xml looks good but your action must hit the GetMapping, so use the commented out one (you also need the leading slash): `@GetMapping("/lista_parlamentari_circoscrizione")`. And you probably need a `@RequestMapping("/Parlamento")` annotation on class level if you don't already have. you should also use the method attribute in your form entity: `
    ` And you probably have to clean up your controller method. I don't see a view resolver, which doesn't mean that you don't have one.
    – The Frozen One Oct 12 '22 at 10:12
  • In WebConfig there is internalResourceViewResolver (). Isn't that the Viewresolver? – utente1234567890 Oct 12 '22 at 10:24
  • in application.properties there is server.servlet.context-path=/Parlamento – utente1234567890 Oct 12 '22 at 10:28
  • Sorry, haven't seen the ViewResolver, that should be fine. Context-path should also be ok. Have you read [this answer](https://stackoverflow.com/a/58080267/7132739)? – The Frozen One Oct 12 '22 at 11:32
  • yes I already have the error pages, In application.properties I have: view.prefix:/webapp/ suffix:.jsp Are these instructions not enough instead of the ViewResolver? I commented on them in case they conflict – utente1234567890 Oct 12 '22 at 11:42
  • on a tutorial it says to put – utente1234567890 Oct 12 '22 at 11:45
  • Well, I think you already have configured a view resolver (probably twice), so that's ok. Please try to implement one of the options in the answer I linked in my previous comment. – The Frozen One Oct 12 '22 at 11:48
  • now the stacktrace has changed:View name 'error', model {timestamp=Wed Oct 12 15:27:05 CEST 2022, status=400, error=Bad Request, trace=org.springframework.web.bind.MissingServletRequestParameterException: Required request parameter 'circoscrizione[]' for method parameter type String[] is not present – utente1234567890 Oct 12 '22 at 13:34
  • Ok, this looks good and the error message is absolutely clear: You defined a **required** parameter (implicitly required which is the default in Spring mvc): `@RequestParam(value="circoscrizione") String[] circoscrizione` But you obviously haven't passed this param in your request. So - either add the param to your request as you tried in your first shot: `.../Parlamento/lista_parlamentari_circoscrizione?circoscrizione=Calabria` - or make the request param optional: `@RequestParam(value="circoscrizione", required = false)` which means that the param is null if not provided. – The Frozen One Oct 12 '22 at 14:02
  • Also remove the brackets [] from "value" or just remove value completely, then, the request param must match the param name in your controller method, which is the case here. – The Frozen One Oct 12 '22 at 14:03
  • I used: public String getParlamentare4(@ModelAttribute("circoscrizione[]") String[] circoscrizione) and now the stacktrace tells me: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No primary or single unique constructor found for class [Ljava.lang.String;] with root cause java.lang.IllegalStateException: No primary or single unique constructor found for class [Ljava.lang.String; ......javax.servlet.ServletException: Circular view path [error]: – utente1234567890 Oct 12 '22 at 14:17
  • Use a List instead of an array, there should be a constructor in place: `public String getParlamentare4(@ModelAttribute("circoscrizione") List circoscrizione)`. – The Frozen One Oct 12 '22 at 14:25

0 Answers0