This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 6 months ago.
These are the files of my Spring MVC project:
Java controller: AddController.java
package com.mycompany;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController {
#RequestMapping("/sum")
public String add(){
return "display.jsp";
}
}
War configuration file: 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>
<servlet>
<servlet-name>telusko</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
Index view page: index.jsp
<html>
<body>
<form action="sum">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit">
</form>
</body>
</html>
Display result view page: display.jsp
<%--
Created by IntelliJ IDEA.
User: yrbar
Date: 2022-08-11
Time: 2:59 p.m.
To change this template use File | Settings | File Templates.
--%>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
I am here
</body>
</html>
This is the pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>DemoMVC</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<name>DemoMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.22</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.30</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>DemoMVC</finalName>
</build>
</project>
This is my servlet configuration file: mycompany-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.mycompany"></ctx:component-scan>
</beans>
And my project structure looks like this:
I'm getting the following error: The requested resource [/DemoMVC_war/sum] is not available
How can i fix this?
With your provided files i have reproduced the WAR file and deployed it in tomcat.
When you browse to http://localhost:8080/DemoMVC/ your index.jsp is loaded, and when you enter form data you're posting to http://localhost:8080/DemoMVC/sum?t1=1&t2=2
This means the URL your spring servlet should listen to must include /sum.
Currently your servlet is setup to listen to the following endpoints:
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
But the requested URL /sum?t1=1&t2=2 does not include .htm, so this request is not forwarded to your Spring MVC servlet. Hence tomcat responds with a http 404 not found error.
You can solve this by updating your servlet configuration file to something like this:
<servlet-mapping>
<servlet-name>telusko</servlet-name>
<url-pattern>/sum/*</url-pattern>
</servlet-mapping>
You can read more on the working of the org.springframework.web.servlet.DispatcherServlet on the following page: https://docs.spring.io/spring-framework/docs/3.0.0.RC2/reference/html/ch15s02.html
Related
I'm using eclipse IDE and trying to run a simple HelloWorld Spring MVC app. While web/index.jsp doesn't have an issue, I cannot get the /WEB-INF/views/final.jsp to display correctly (I'm using http://localhost:8080/springmvc/home ). I keep getting (404-not found) error.
Below is my setup. Any suggestions?
Thanks.
pom.xml
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
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>
<!-- Configure dispatcher servlet -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Provide support for conversion, formatting and validation -->
<mvc:annotation-driven/>
<context:component-scan base-package="springmvc.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
HomeController.java
package springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#RequestMapping(value ="/home")
public String home()
{
System.out.println("This is home URL");
return "final";
}
}
index.jsp
<html>
<body>
Click here...
</body>
</html>
final.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h1>This is home Page</h1>
</body>
</html>
Project Structure is as below
I got the reason for this error I was executing this code on tomcat version 10, from version 10 package (javax) are replaced with jakarta so dispatcherServlet was not able to detect javax package and was giving this error. I downgraded my tomcat version to 9 and it worked fine another solution could be to use packages which support jakarta insted of javax.
I am trying to build a simple webapp to apply the learnings of spring MVC, the issue occurs while configurating the dispatcher servlet.
I get this message when i click on submit button in index.jsp
HTTP Status 404 – Not Found
Type Status Report
Message The requested resource [/DemoMVC/add] is not available
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
index.jsp
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="submit"><br>
</form>
</body>
</html>
below is the 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>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.first</groupId>
<artifactId>DemoMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>DemoMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.24</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.32</version>
</dependency>
</dependencies>
<build>
<finalName>DemoMVC</finalName>
</build>
</project>
dispatcher servlet
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<context:component-scan base-package="com.first"/>
<mvc:default-servlet-handler/>
<!-- ... -->
</beans>
Controller
package com.first;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController
{
#RequestMapping("/add")
public String add()
{
return "welcome";
}
}
First of all you would have to change the name of your dispatcher servlet to xyz-servlet.(here xyz is taken as any name you like but it should be followed by -servlet.)This will help web.xml to map your dispatcher servlet. Also your dispatcher servlet should be in WEB-INF folder.
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>xyz</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>xyz</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Also don't forget to add suffix and prefix in your xyz-servlet.xml file
Please make the following changes:
Add method attribute in . Example for 'post'
<form action="/add" method="post">
In controller either add method type OR use specific mapping annotation:
#RequestMapping(value="/add", method=RequestMethod.POST) OR #PostMapping("/add")
First I created a constructor in controller to check if controller is working.
#Controller
public class AddController
{
public AddController() {
System.out.println("################000");
}
#RequestMapping(value="/add", method = RequestMethod.GET)
public String add()
{
System.out.println("################12");
return "welcome.jsp";
}
}
and the console was printing ################000 but did not print ################12. Hence there was an issue in request mapping.
After checking
Spring MVC request mapping not working
tried implementing <mvc:annotation-driven /> in servlet and it worked
Ok So if all the things which i told you and others told you aren't working then the problem may be that you have not set the suffix amd prefix in your project.
xyz-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<context:component-scan base-package="com.first"/>
<mvc:default-servlet-handler/>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/webapp/jsp/"/><!--Set the path of where your have stored your view files-->
<property name = "suffix" value = ".jsp"/><!--Set the view resolver type eg. html, jsp-->
</bean>
</beans>
Now this may solve your issue!!
I was trying to execute Spring MVC addition example using with Maven but when I try to run the index.jsp page I'm getting this error:
HTTP Status 500 - Error instantiating servlet class org.springframework.web.servlet.DispatcherServlet
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet while executing spring mvc
pom.xml (to include the jars)
--------
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring1</groupId>
<artifactId>mvcexample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>mvcexample</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
1.pom to add maven dependencies
index.jsp
---------
<html>
<body>
<form action="add">
<input type="text" name="t1"><br>
<input type="text" name="t2"><br>
<input type="text" name="t3"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
2.the page which is used to take input
display.jsp
------------
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Result is :<%=request.getAttribute("result") %>
</body>
</html>
3.The display page to display the result
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>
<servlet>
<servlet-name>spring1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>spring1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
4.To define the dispatcher servlet
spring1-servlet.xml
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ctx="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">
<ctx:annotation-config></ctx:annotation-config>
<ctx:component-scan base-package="com.spring1.mvcexample"></ctx:component-scan>
</beans>
App.java
---------
package com.spring1.mvcexample;
#Controller
public class App
{
#RequestMapping("/add")
public ModelAndView add(HttpServletRequest request,HttpServletResponse response)
{
int i=Integer.parseInt(request.getParameter("t1"));
int j=Integer.parseInt(request.getParameter("t2"));
int k=Integer.parseInt(request.getParameter("t3"));
int l=i+j+k;
ModelAndView mv=new ModelAndView();
mv.setViewName("display.jsp");
mv.addObject("result",l);
return mv;
}
}
5.The code which contains controller
I have mentioned all classes and files I have used. I tried many times but could not resolve the error. Please suggest me how I can resolve the error. Thanks in advance.
You can simply add following tag load-on-startup under web.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>spring1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring1</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
you are missing some dependency jars in your pom.xml , kindly add those, some dependecy that i can see which are missing are
org.springframework.beans-3.0.1.RELEASE-A
org.springframework.core-3.0.1.RELEASE-A
org.springframework.web-3.0.1.RELEASE-A
org.springframework.web.servlet-3.0.1.RELEASE-A
org.springframework.web.portlet-3.0.1.RELEASE-A
org.springframework.web.struts-3.0.1.RELEASE-A
write below line under <servlet> tag:
<load-on-startup>1</load-on-startup>
Also make sure your web.xml and spring1-servlet.xml files should be under /WEB-INF folder.
I am new to this maven - spring project i done the coding and configurations for the project but when ever i try to run this project i am getting the 404 resource not found error
i tried this example.i follow the given directory structure in this example but it is not working it gives me the bellow error
i tried this link
HTTP Status 404 - /hello/
type Status report
message /hello/
description The requested resource is not available.
Apache Tomcat/8.0.32
Can any one help me to fix this
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
Click Here
</h3>
</center>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- <!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>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.opine.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.opine</groupId>
<artifactId>hello</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.0.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>hello</finalName>
</build>
</project>
helloworld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h2>
${message} ${name}
</h2>
</center>
</body>
</html>
HelloWorldController.java
package com.opine.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";
#RequestMapping(value="/hello")
public ModelAndView showMessage(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
if you are using STS IDE then open Server tab -> double click on tomcat server -> click on module tab -> click on Add Web Module and update the path with "HelloWord" and then hit the URL "http://localhost:8080/HelloWorld/hello".
Hope this will help you.
Try to change your web.xml configuration as follows:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Edit your context-param as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/root_context.xml</param-value>
</context-param>
Make a spring bean file with name root_context.xml inside WEB-INF folder with content as follows:
<?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.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
</beans>
I try to learn springmvc.But When I get to start,I encountered a problem.
I think it is a simply problem.And I search for it on internet and find a lot of examples.But i don't find the reason.Here is the code.
HomeControll.java
package cn.qingtianr.controll;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by jack on 16-5-20.
*/
#Controller
public class HomeControll {
#RequestMapping("/")
public String index(ModelMap model){
String s = "hhh";
model.addAttribute("hello",s);
return "index";
}
}
index.jsp
<%--
Created by IntelliJ IDEA.
User: jack
Date: 16-5-20
Time: 下午6:31
To change this template use File | Settings | File Templates.
--%>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${hello}
</body>
</html>
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>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
hello-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.qingtianr.controll"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.qingtianr</groupId>
<artifactId>springmvctest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>springmvctest Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.5.RELEASE</spring.version>
<spring-data.version>1.2.0.RELEASE</spring-data.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>springmvctest</finalName>
</build>
</project>
When I access the index.jsp.I get the ${hello} not hhh on my browser.Maybe I get a low level error.But I don't know it really.
So can anyone help me?Thanks.
Looks like the good old SpringMVC problem with DispatcherServlet mapping.
You mapped the servlet to /, meaning that it will get all the URL that no other servlet (including default container one) could resolve. One common issue with that is that Spring MVC DispatcherServlet will never receive the empty URL '/' (see this other answer of mine for a more detailed explaination).
So I assume that you directly called the index.jsp file without first hitting the controller. But then (of course) the hello model variable has not been put in request attributes and cannot be used.
How to fix:
To avoid directly hitting the jsp files, good practices recommend to put them under WEB-INF folder. That way, they will never be server by the servlet container, and will only be displayed if forwarded to from a controller => move index.jsp under WEB-INF/jsp/index.jsp and accordingly change <property name="prefix" value="/WEB-INF/jsp/"/> in jspViewResolver configuration.
Then use one of the solutions form the referenced post. I advise to map the DispatcherServlet to /* and to move static resources to a folder processed by a ResourceHttpRequestHandler using for example:
<mvc:resources mapping="/resources/**" location="/public-resources/"/>
as you are using xml configuration
Try this:
Add this before !DOCTYPE
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
To print a value:
<c:out value="${hello}"></c:out>
Full index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
</head>
<body>
<c:out value="${hello}" ></c:out>
</body>
</html>
Hope it helps.
Remove <mvc:default-servlet-handler/> from your spring configuration. You don't need it because you already have the SpringDispatcher in your web.xml and have a view resolver in your spring config.
Add a value to the request mapping other than "/". for example, #RequestMapping("/blam"). (the url for this will be http://<host>/<context>/blam).
Don't use "index.jsp" as your view file name. "index.jsp" is the default welcome file name.
This works for me:
no <mvc:default-servlet-handler/> in my servlet config.
My war file is named klabben.war (the context is "klabben").
#RequestMapping("/blam").
return "blam.jsp from the index handler method.
copy your index.jsp file content to blam.jsp.
use the url "http://<host>/klabben/blam" to display the "hello: sss" message.
Note: I was wrong about <mvc:default-servlet-handler/>. It provides value other than I thought.