Creating Spring configuration beans

Go to the /WebContent/WEB-INF/ directory and create an XML file called spring-mvc-kotlin-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

<mvc:annotation-driven />
<context:component-scan
base-package="mvckotlin"
/>
<mvc:default-servlet-handler />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

In the spring-mvc-kotlin-servlet.xml configuration file, we mentioned a <context:component-scan> tag. All the components from the mvckotlin package and all its child packages will now be loaded by the Spring:

  • This will load our MVCKotlinApp.class and also assign a viewResolver bean.
  • <property name="prefix" value="/WEB-INF/jsp/" /> will resolve the view and add a prefix string named /WEB-INF/jsp/.
  • Note that we have returned a ModelAndView object with the view name welcome in our MVCKotlinApp class.
  • This will be resolved to the /WEB-INF/jsp/greeting.jsp path.
  • There's a web.xml file under the /WebContent/WEB-INF/ directory. If you don't find it, create it in the /WebContent/WEB-INF/ directory. Here's a piece of code from web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>spring-mvc-kotlin</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>index.html</welcome-file>

</welcome-file-list>
<servlet>
<servlet-name>spring-mvc-kotlin</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc-kotlin</servlet-name>
<url-pattern>/index.jsp</url-pattern>
<url-pattern>/greeting.jsp</url-pattern>
</servlet-mapping>
</web-app>

web.xml will map DispatcherServlet with the /greeting.jsp URL pattern. Furthermore, note that we have mentioned index.jsp as a greeting file.

After initialization, DispatcherServlet will look for a file named [servlet-name]-servlet.xml in the WEB-INF folder. The value of the servlet XML file prefix name, and value of the <servlet-name> tag in web.xml, have to be the same. In our example, the name of the servlet is spring-mvc-kotlin-servlet.xml.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.134.130