Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autowired spring service into a jsp?

As mentioned in the title, i need to autowire a service in my page.jsp.. "i know that's not recommended to do it "

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>


<%@ page import="org.springframework.context.ApplicationContext"%>
<%@ page
    import="org.springframework.web.servlet.support.RequestContextUtils"%>

<%@ page import="com.fussa.fyby.service.Test"%>
<%@ page import="com.fussa.fyby.model.PIL_P_APPLCTN"%>


<%
    ApplicationContext ac = RequestContextUtils.getWebApplicationContext(request);
    Test s = (Test) ac.getBean("fussafyby");
    PIL_P_APPLCTN app = s.getByKey(13);
%>


<c:out value="azeerty"></c:out>

<c:out value="${ app.APPLCTN_CD }"></c:out>

<select name="listeGroupes" id="listeGroupes">

    <option value="123">123</option>

    <option value="${ app.APPLCTN_CD }">${ app.APPLCTN_CD }</option>
    <option value="123">${ s.afficher() }</option>

</select>

My service:

@Component("fussafyby")
@Transactional
public class Test {

    @Autowired
    private SessionFactory sessionFactory;

    protected Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    public PIL_P_APPLCTN getByKey(int key) {
        return (PIL_P_APPLCTN) getSession().get(PIL_P_APPLCTN.class, key);
    }



    public String affiche() {
        return "FuSsA";
    }
}

Only azeerty message and 123 option in the select are shown..

Thanks for any advices..

like image 884
FuSsA Avatar asked Jun 22 '16 21:06

FuSsA


2 Answers

You should not even try to do that... JSP are translated to java source and compiled to java classes by the servlet container, and the Java EE specification does not say where they go, so you cannot have spring scan them, so annotation is not an option

Worse, the JSP cannot be Spring beans because they are created by the servlet container outside of the application context, so XML injection cannot work either.

And even full AspectJ cannot be used because once again you have no control on where JSP classes lie so you cannot even use a load-time weaver on them.

The problem is not that "that's not recommended to do it ", it is that JSP are special classes managed by the servlet container. You can use Java code in scriplets, but you cannot manage them as normal Java classes.

BTW, and more generaly, don't you think that there can be good reasons for not recommending too much Java code in scriptlets?

like image 125
Serge Ballesta Avatar answered Jan 04 '23 17:01

Serge Ballesta


I think you may not use @Autowired annotation in the JSP. But you have used some Support class. I can help you with these solutions:
Solution 1:
if You are using Spring MVC,
just simply pass your service to the JSP using ModelAndView.
Example:

Suppose You have Controller:

@Controller
public void TestController{

    @Autowired
    private TestService tService;

    @RequestMapping("someURL")
    public ModelAndView displayPage{
    //do some stuff
    return new ModelAndView("myView").addObject("tService",tService);
    }
}

JSP:

<html>
...
${tService.myMethodIWantToUse(..)}
...
</html>



Solution 2:
Use ApplicationContext in your JSP to access any bean like this

Example:

@Component("tsc")
public void TestServiceClass{
//Your code goes here
}

Inside JSP:

ApplicationContext aC = RequestContextUtils.getWebApplicationContext(request);
TestServiceClass tsc1 = (TestServiceClass) aC.getBean("tsc");
//Code to access your service Classes methods.

Hope this will help.

like image 27
Tahir Hussain Mir Avatar answered Jan 04 '23 15:01

Tahir Hussain Mir