I am trying to build a basic Java app with Spring MVC, however, I am having troubles with links within the app.
Here is my web.xml
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/welcome.jsp</url-pattern>
<url-pattern>/welcome.html</url-pattern>
</servlet-mapping>
My test-servlet.xml
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<context:component-scan base-package="com.springtest.controller"/>
<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/</value>
</property>
<property name="suffix">
<value>.jsp </value>
</property>
</bean>
The first page that gets displayed when running the app is the following:
<html>
<head>
<title>Spring MVC Tutorial</title>
</head>
<body>
<br>
<div style="text-align:center">
<h3>
<a href="welcome">Click to say hello </a>
</h3>
</div>
</body>
</html>
And welcome.jsp is this:
<html>
<head>
<title>Hello World MVC</title>
</head>
<body>${message}
</body>
</html>
Since this is my first time using Spring (and I know there are a lot of questions here asking/answering this - thus I haven't been able to find an answer that helps me), I followed an online tutorial .
My folder structer is similar to what they have in the tutorial, however, every time I click the link to go to the second page, I get a 404 Error saying that:
/SpringTutorial-1.0-SNAPSHOT/WEB-INF/jsp/welcome.jsp - is not available
Basically /
in the <url-pattern>
tag is enough.It will take of /welcome.jsp
or /welcome.html
.
<servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
No need to give relative path in value
property of prefix
.
<property name="prefix"> <value>/WEB-INF/jsp/</value> </property>
And one more thing is you can also try with InternalResourceViewResolver
in place of UrlBasedViewResolver
.
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp" />
<property name="suffix" value=".jsp" />
</bean>
Change your form action to welcome.html
<html>
<head>
<title>Spring MVC Tutorial</title>
</head>
<body>
<br>
<div style="text-align:center">
<h3>
<a href="welcome.html">Click to say hello </a>
</h3>
</div>
</body>
</html>
also change url pattern to / or *.html
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/test-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
also view resolver to internal resource view resolver and avoid giving relative url in it.
Also make sure that you have given @controller annotation. And URL mapping as @requestmapping("/welcome")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.