Spring MVC – Url Filename View Controller | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • In MVC-Controller3 example as you can see we have not created any Controller class, here we use UrlFilenameViewController in xml file and use name attribute in bean tag. So based on the suffix and action it will view page. Example: if uri is success.code then it will open success.jsp.

Create Java Web Project

  • Open Eclipse
  • Go to File -> New -> Dynamic Web Project
  • Create MVC-Controller3 project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs (Used 2.X jars)
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-web-X.X.X.jar
    • spring-webmvc-X.X.X.jar
  • Also, add these jars in WEB-INF/lib folder for runtime.
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
		http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
	
	<display-name>MVC-Controller1</display-name>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<!-- default -->
	<servlet>
		<servlet-name>helloWorld</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>helloWorld</servlet-name>
		<url-pattern>*.code</url-pattern>
	</servlet-mapping>

</web-app>

helloWorld-servlet.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

	<!-- default handler mapping -->
	<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

	<!-- controller -->
	<bean name="/success.code" class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />

	<!-- view resolver -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

index.jsp

<h2>Hello World</h2>
<a href="./success.code">Success Page</a>

success.jsp

<h2>Hello World</h2>
<a href="./success.code">Success Page</a>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s