0

i'am working on an application with springmvc, i was wondering how can i inject a sping service in my jsp page.i've tried somethings but its doesn't seem to work

my service look like that :

            @Service
            @Transactional
            public class CompteMarcheService {

                @Autowired
                private Compte_ProjetRep service;


                public int test()
                {
                    return 10;
                }

and what i've added in my root-context

       <bean class="
         org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="exposedContextBeanNames">
              <list>
                 <value>CompteMarcheService</value>
              </list>
           </property>
        </bean>

and finaly , the jsp page

      <c:set var="clients" scope="request" value="${CompteMarcheService.test()}"/>

but when i try to display the clients var like that : ${clients} i get empty field , any help please i need to know how to inject correctly my service

thanks.

nedal jedidi
  • 77
  • 3
  • 11

3 Answers3

0

From spring documentation:

5.10.6 Naming autodetected components

When a component is autodetected as part of the scanning process, its bean name is generated by the BeanNameGenerator strategy known to that scanner. By default, any Spring stereotype annotation ( @Component, @Repository, @Service, and @Controller) that contains a name value will thereby provide that name to the corresponding bean definition.

If such an annotation contains no name value or for any other detected component (such as those discovered by custom filters), the default bean name generator returns the uncapitalized non-qualified class name. For example, if the following two components were detected, the names would be myMovieLister and movieFinderImpl:

@Service("myMovieLister") 
public class SimpleMovieLister { }

@Repository 
public class MovieFinderImpl implements MovieFinder { }

Try to use compteMarcheService instead of CompteMarcheService.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="exposedContextBeanNames">
    <list>
      <value>compteMarcheService</value>
    </list>
  </property>
</bean>

<c:set var="clients" scope="request" value="${compteMarcheService.test()}"/>
Marcelo Keiti
  • 1,200
  • 9
  • 10
0

If you would like toe xpose classes (beans) to JSP's you could do it as either a Tag library or there is also one more way

<bean    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="exposeContextBeansAsAttributes" value="true"/>
</bean>

<bean id="myBean" class="com.x.MyClass"/>

Here is a post

How to inject spring beans into a jsp 2.0 SimpleTag?

Community
  • 1
  • 1
Paul John
  • 1,626
  • 1
  • 13
  • 15
0

Just have done the same thing using annotations:

@Bean
public ViewResolver tilesViewResolver() {
  final UrlBasedViewResolver = new UrlBasedViewResolver();
  ...
  viewResolver.setExposeContextBeansAsAttributes(true);
  viewResolver.setExposedContextBeanNames("myBean");
  return viewResolver;
}

And in my .jsp:

<c:set var="myBeanData" scope="request" value="${myBean.myMethod()}"/>
<a id="id" href="${myBeanData}">Link</a>
Josef
  • 2,869
  • 2
  • 22
  • 23
Shalguev
  • 81
  • 3