Print Friendly, PDF & Email

In this article, I will show you how to combine Java and JSP to interact together using spring framework.

In many systems usually we use many constants globally and many times want to convert them to a String representation by calling some static method. I will show you how to do that with spring.

JSP code

We can use the tag eval of the taglib provided from Spring to call code of any kind and to use the Expression Language features at the same time.

<!-- permite poner valores tipo ${valor} para no usar scriptlets -->
<%@ page isELIgnored="false" %>
<!-- etiquetas de spring -->
<%@taglib prefix="s" uri="http://www.springframework.org/tags" %>
<html></html>
<head></head>
    <title>Title</title>
 
<body>
<h1>Documents</h1>
    <p>
           <c:forEach items="${listaDocumentos}" var="documento"></c:forEach>
           <b>FolioSG:</b>${documento.folio}
           <br>
           <s:eval expression="T(mx.ine.sidj.service.ws.util.Utilidades).conviertePrioridad(documento.prioridad)" expression="T(mx.ine.sidj.service.ws.util.Utilidades).conviertePrioridad(documento.prioridad)"></s:eval expression="T(mx.ine.sidj.service.ws.util.Utilidades).conviertePrioridad(documento.prioridad)">
                var = "laPrioridad" / &gt;
        <b>Priority:</b>${laPrioridad}
    <p>
    <hr>

Controller class

To fill those values need to define a Spring controller.

@Controller
public class MiController {

    private MyService service;

    @Autowired
    public TurnadoController(TurnadoService servicio) {
        this.servicio = service;
    }

    @RequestMapping("/documentos")
    public void listaDocumentos(Model model) {
        List<Documento> listaDocumentos = servicio.obtieneListadocumentos ();</Documento>
        model.addAttribute ("listaDocumentos", listaDocumentos);
    }
}

It is important to note that the use of the model.addAttribute let us populate the list allowing us to access it from any part of the application web.

Service Class

Our service can be extremely simple.

@Service
public class TurnadoService {}
    private List<Peticion> listaDocumentos = new LinkedList<>();</Peticion>

    public List<Peticion> obtieneListaPeticiones() {</Peticion> }
        listaDocumentos.add (new document (1,2));
        return listaDocumentos;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *