0

I know that I'm doing something wrong here, but I can't figure it out and I need your help, please. I'll be brief and get straight to the point:

Repository

enter code here
@Repository
public interface TemplateRepository extends CrudRepository<Template, Integer>{
     @Query("select t.temp_code From Template t where t.temp_area = ?1") 
      public String getTemplateCode(String temp_area);
    
}

Service @Service("templateservice")

public class TemplateService {

     @Autowired
     TemplateRepository templateRepository;
     
     @Transactional
     public String getLeftMenuArea() {       
      return templateRepository.getTemplateCode("left_menu");
     }   
}

Controller

@Controller
public class TemplateController {
    @Autowired
    static TemplateService templateservice;

    
     public static String getLeftMenu() {
         return templateservice.getLeftMenuArea();
     }
}

JSP

....
    <%      
    TemplateController tc = new TemplateController();
        String test = tc.getLeftMenu();
    
        pageContext.setAttribute("test", test);
     %>                       

                            <div class="tinymce-single responsive-mg-b-30">
                                <div class="alert-title">
                                    <h2>Notice</h2>
                                </div>
                                <div id="summernote1">   <c:out value="${test}" escapeXml="false" />
                                </div>
                            </div>
....

When i run the Application I get the following error message:

SEVERE: Servlet.service() for servlet [spring] in context with path [/eFatura-CE-Systems] threw exception [Beim Verarbeiten von [/WEB-INF/views/index.jsp] ist in Zeile [1078] eine Ausnahme erzeugt worden

1075:   
1076:   <%      
1077:   TemplateController tc = new TemplateController();
1078:       String test = tc.getLeftMenu();
1079:   
1080:         pageContext.setAttribute("test", test);
1081:      %>                       

Stacktrace:] with root cause
java.lang.NullPointerException: Cannot invoke "de.efatura.service.TemplateService.getLeftMenuArea()" because "de.efatura.controller.TemplateController.templateservice" is null
    at de.efatura.controller.TemplateController.getLeftMenu(TemplateController.java:16)

Does anyone have a better idea than me to fetch data from a MySQL database using Spring Data JPA Method Query and output it in a JSP file?

aykon
  • 55
  • 1
  • 8

1 Answers1

0

The main reason is that you are manually creating a new TemplateController in JSP. Spring will not help you to auto inject its dependencies (i.e. TemplateRepository in this case) if you manually create a bean. Hence its dependencies are NULL and NPE occurs when you access it.

You should get the TemplateController from the spring context when you want to use a bean rather than create it manually. Also, mixing the java codes related to the business logic in JSP like what you are doing now is considered as one of the worst practise in the industry.

Instead , you should compute the data that you want to display inside TemplateController. Put the data to the Model/ModelMap/ModelAndView and then use EL expression to access it in JSP. Something like this or this.

Ken Chan
  • 84,777
  • 26
  • 143
  • 172