Reputation: 529
I have a pretty basic spring mvc web app set up, and this is the begining/section in question from my controller class
@Controller
public class MainController {
@Autowired
AssetDAO assetDAO;
@RequestMapping("/")
public String home() {
System.out.println("HERE!");
return "index";
}
and my index.jsp file
<%@page language="java" contentType="text/html; charset=UTF-8" paegeEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "~//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org.TR.html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> Welcome Page </title>
</head>
<body>
<div>
<h1> Asset Tracking Web App Test </h1>
<a href="EmployeeList">Get Employee Records</a>
</div>
</body>
</html>
but when I access localhost:8080/
I get a 404
error. however the message HERE
gets printed
EDIT: I added an application.properties file to my src/main/resources folder which fixed the issue... My question now is, how is this different from using an InternalViewResolver?? or is my understanding of this just wrong??
EDIT2: the file structure of the project is as follows
.
├── pom.xml
├── src
│ └── main
│ ├── java
│ │ └── com
│ │ └── assetTracking
│ │ ├── App.java
│ │ ├── AssetDAO.java
│ │ ├── Employee.java
│ │ └── MainController.java
│ ├── resources
│ │ └── application.properties
│ └── webapp
│ └── WEB-INF
│ ├── jsp
│ │ ├── employeeList.jsp
│ │ └── index.jsp
│ └── views.xml
├── tableCreatiosn.sql
├── tableEntries
leaving out all the dependency directories for brevity. Having the files sugggested by yates did not solve the problem. replacing them with application.properties does however
application.properties
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
welcome.message: Hello Test
Upvotes: 0
Views: 658
Reputation: 301
It is the jsp page not able to be rendered.
Try adding the following dependencies in your pom.xml.
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
You may need to change the jasper scope to "default" if you are running using Intellij IDE.
Upvotes: 0
Reputation: 46
If you don't have a viewResolver bean with the properties prefix and suffix on your servlet, spring doesn't know what the suffix of "index" is, so it can't find it. You can use a viewResolver to all your jsp files.
Here is the XML configuration:
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
Hope it helps.
EDIT
To configure the servlet add this to web.xml (In your case maybe is views.xml?):
<servlet>
<servlet-name>servletname</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servletname</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
And don't forget to create the servlet "servletname-servlet.xml", instead of servlet name you can out the name of your app. Then add the bean above to your servlet and it should work.
Check this, it may help you in case I forgot anything else.
Upvotes: 0
Reputation: 1402
Probably you haven't configured InternalViewResolver. If yes then it's locating index.jsp
at some incorrect location.
Following is the configuration for InternalViewResolver
XML:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"/>
<property name="suffix" value=".jsp"/>
</bean>
Java
@EnableWebMvc
@Configuration
@ComponentScan("<package_fully_qualified_name>")
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
Upvotes: 2