Getting HTTP Status 404 - /hello/? - java

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>

Related

The requested resource [/DemoMVC_war/sum] is not available [duplicate]

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

Not able to access other jsp pages other then index.jsp?

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.

Spring MVC DispatcherServlet error

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.

Springmvc controll can't pass the value to jsp

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.

Spring mvc with maven giving noHandlerFound error

I created a spring mvc maven application as described below using spring tool suite Version: 3.6.4.RELEASE
here is the 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.mytcg</groupId>
<artifactId>firstapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>firstapp 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.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>firstapp</finalName>
</build>
</project>
here is the web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>firstappservlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>firstappservlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<display-name>Archetype Created Web Application</display-name>
</web-app>
here is the servlet-config.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.mytcg.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name= "prefix" value="/WEB-INF/jsp/"/>
<property name= "suffix" value=".jsp"/>
</bean>
<!--
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"> </bean>
-->
</beans>
here is the HelloController.java
package com.mytcg.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HelloController {
#RequestMapping(value ="/greeting" )
public String sayHell (Model model){
model.addAttribute("greeting", "Hellooo world");
return "hello";
}
}
here is the hello.jsp
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
<h1>${greeting}</h1>
</body>
</html>
After i run it, it shows "Hello World!"
But when i give the following url for greeting.html
http://localhost:8083/firstapp/greeting.html
it gives following error with 404
Apr 04, 2016 9:40:17 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/firstapp/greeting.html] in DispatcherServlet with name 'firstappservlet'
the your problem is in the your controller.
In your controller the #RequestMapping listen on the /greeting but you fire a http request form your borwser for http://localhost:8083/firstapp/greeting.html
you should change the your controller in this way:
#Controller
public class HelloController {
#RequestMapping(value ="/greeting.html" )
public String sayHell (Model model){
...
}
}
However the my personal advice is taht you should not tie you to a scpecific implementation and you should use for #RequestMapping method as you did configure the dispatcherServlet in this way:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
and use for your url a pattern like this: http://localhost:8083/firstapp/greeting
I hope that this can help you
change your web.xml like below
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<load-on-startup>2</load-on-startup>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
change your xml file name like servlet-config.xml to dispatcher-servlet.xml and configure DefaultAnnotationHandlerMapping
Your dispatcher-servlet.xmlis
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.mytcg.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name= "prefix" value="/WEB-INF/jsp/"/>
<property name= "suffix" value=".jsp"/>
</bean>
<!--configuring HandlerMapping -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
</beans>

Categories