0

? hello, there. I am a student from Taiwan.

Recently I am studying JavaSpring, and I start with HelloWorld sample.

my problem is:

In index.jsp, this statement

WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(application);

I think it is used to register(or connect?) the ContextLoader in web.xml so that I could use it to get beans. But I would like to know that why I could only send "application" as a variable even I couldn't find any declaration of this variable in my program. How did that work without undefined error?

Even I entered the class it imported above, still has no found about this.

Here is the code that sample offer me that I should add to index.jsp file:

<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%
WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(application);
%>

and my web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/ctx_ut.xml</param-value>
    </context-param>
    <filter>
        <filter-name>requestContextFilter</filter-name>
        <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>requestContextFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>context</servlet-name>
        <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>

my bean setting ctx_ut.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>WEB-INF/hello.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="spring01.HelloWorld" scope="request">
        <property name="message" value="${hi}"></property>
    </bean>
    <bean id="seeyou" class="spring01.SeeYou" scope="request">
        <constructor-arg value="${seeYou}" />
        <constructor-arg ref="dataSource" />
    </bean>
    <bean id="goodbye" class="spring01.Goodbye" depends-on="seeyou"
        init-method="init" scope="request">
        <property name="helloWorld" ref="dataSource"></property>
        <property name="message" value="${goodbye}"></property>
    </bean>
</beans>

(I guess class file is not important here so I didn't put here, it's just some string setter and getter.)

Is there any concept I misunderstand or any document had described it? please tell me and share the link.

First ask, hope my problem is clear enough. Thanks.

  • 2
    This is not “Java Spring”, this is code from WW2 made worse by bad design. No one uses Spring XML config anymore, most new features aren’t supported in XML, and anyone accessing `ApplicationContext` From JSP should be fired. If you’re learning, try finding a better starting point. – Abhijit Sarkar Aug 18 '20 at 02:13
  • If you already know about servlet and jsp, you can pass attributes from Controller to jsp by using ModelMap – Lê Hoàng Dững Aug 18 '20 at 02:28
  • I got it. But actually I am learning Spring because my job(Corporate Internship ) is helping develop and maintain huge but old project of my company. By my supervisor I knew that the annotation something like @autowired is not that maintainable(actually I don't know it clearly). And also, this is just a little practice for me. so I know it's old as a knight in middle Ages but I still need to complete it and try my best to realize the code. And finally, is it real for "this is not Java Spring"? Or just it's too old and some joke? – JustBelieveMe Aug 18 '20 at 02:33
  • got it, I will learn more about servlet and jsp. these word didn't mentioned in the sample, thx! – JustBelieveMe Aug 18 '20 at 02:36
  • A note: https://stackoverflow.com/questions/38340291/jsp-what-is-wrong-with-scriptlets-and-what-to-use-instead. Furthermore, you should find out _exactly why_ your supervisor doesn't like `@Autowired`. If it's because component-scanning is brittle and your company prefers `@Bean` methods, that's okay, but the _particular reason_ for not liking it (because it's "too new" or "too old") is important. – chrylis -cautiouslyoptimistic- Aug 18 '20 at 02:59
  • According to my supervisor, telling me that our project is included hundreds or thousands of class, if `@Autowired` or `@Beans` or any others are used in the class file, it's very hard to maintain(maybe is not easily readable, I guess) for the next ten years. And also, they said they doesn't "don't like" annotation but the past ten years is using these old things. No matter rewrite or retest will cost too much. Only when unit test will use it. But perhaps the supervisor just don't want to waste too much time to explain so much to a little school student who just entered not fully one month. – JustBelieveMe Aug 18 '20 at 03:28
  • and about write code in jsp, they said it's just a little practice and not use in real project. – JustBelieveMe Aug 18 '20 at 03:31

0 Answers0