How to autowired spring service into a jsp?

一世执手 提交于 2020-02-03 08:34:27

问题


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


回答1:


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?




回答2:


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.




回答3:


Inside jsp:

TestService testService = ApplicationContextProvider.getApplicationContext().getBean(TestService.class);
testService.testMethod();

You can also use String parameter (service name) for getBean() method




回答4:


Spring can do this for you (now) by exposing your bean names into the request context, you just need to configure it in the viewResolver.

From: https://raibledesigns.com/rd/entry/spring_mvc_jstlview_and_exposecontextbeansasattributes

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="exposeContextBeansAsAttributes" value="true"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=".jsp"/>
</bean>



回答5:


You want to call spring service method from JPS page right?.. Simply follow below steps, it worked for me...

Let's say you want to access AnimalService class.

step1:

AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class);

if your AnimalService is dependent on another class let's say FoodService ..then step1 should be as below..

AbstractApplicationContext appContext = new AnnotationConfigApplicationContext(AnimalService.class,FoodService.class);

in short, you need to add all your dependent classes there.

step2:

AnimalService animalservice = appContext.getBean(AnimalService.class);
animalservice.yourMethod();


来源:https://stackoverflow.com/questions/37978687/how-to-autowired-spring-service-into-a-jsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!