Spring MVC handler mapping not working - java

I am very new to Spring MVC and have been following tutorials but they don't seem to be working.
I can load my index.jsp file fine but when I try to map it to the Dispatcher Servlet I keep getting resource not found errors.
HelloWorldController:
package com.javatpoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
#RequestMapping("/hello")
public ModelAndView helloWorld() {
String message = "HELLO SPRING MVC";
return new ModelAndView("hellopage", "message", message);
}
}
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>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>*.html</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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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.javatpoint" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
my jsp files hellopage.jsp and welcomepage.jsp are in the folder jsp under my WEB-INF folder.
Index.jsp:
click|
click
I am trying to map hello.html to the HelloWorldController.java.
I would greatly appreciate any help as I am having problems mapping the first Spring MVC application.

The problem is with your servlet mapping, try changing it in your web.xml to:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
your url-pattern is set to *.html. Therefore only requests that ends with ".html" will be dispatched to spring mvc.

Add
<url-pattern>*.jsp</url-pattern>
to your servlet-mapping in web.js.

Can you try #RequestMapping(value="/hello",method = RequestMethod.GET ) ?

Your url pattern in the servlet-mapping is wrong.
You're saying go to the dispatcher servlet when the URL matches this:
*.html
So when you use "hello.html" the DispatcherServlet gets triggered.
However, there is no mapping for hello.html. There's only one for hello. HTML is static content, and it's not typical to serve that through a controller.
Try this:
Change the url-pattern to this:
<url-pattern>/</url-pattern>
Change the hello.html to hello.jsp
Change your method signature to return a string, and add the Model to the method.
#RequestMapping("/hello")
public String helloWorld(Model model) {
return "hello";
}
Your anchor tags should then point to the correct mapping. They'll look like this:
click|
So how does this work? These two pieces:
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
The above is a pretty standard. In the servlet mapping, it's saying that anything that matches that URL pattern should hit the servlet named spring you defined in the web.xml.
The view resolver is looking in the /WEB-INF/jsp/ directory for files ending in .jsp. In other words, Spring will look for /WEB-INF/jsp/<string>.jsp. So when you return a String hello in the method, it is actually going to attempt to resolve /WEB-INF/jsp/hello.jsp.
If you understand the above, you may have realized you can serve *.html files this way also. They would require their own view resolver, however.

Related

Spring MVC webapp not loading on browser

I am new to spring mvc and tomcat.
I have developed a demo spring mvc project and trying to deploy it on tomcat 9 through eclipse.
Server starts successfully but when i try to access the url from browser i get 404 with below error message on screen. :
Message The requested resource [/spring-mvc-demo/] 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.
Below are my code details :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<absolute-ordering />
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<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/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="main.webapp" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
HomeController.java
package main.webapp.springdemo.controller;
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class HomeController {
#PostConstruct
public void init() {
System.out.println("HomeController bean getting intiated");
}
#RequestMapping("/")
public String getMainMenu() {
return "main-menu";
}
}
I tried running this app on tomcat 9 through eclipse as well as manually but in both the scenarios i got same error.
Assuming the application deploys without errors (check your logs), the URI path you are using to access it is almost certainly wrong. In a servlet environment the URI path decomposes as:
<context-path><servlet-path><path-info>
where:
<context-path> is the prefix to your application. In the Eclipse server configuration page this is called just "path" and defaults to /<project-name>,
<servlet-path> is configured through the <servlet-mapping> element in your web.xml deployment descriptor,
<path-info> is the part usually used by Spring to perform its internal routing (unless alwaysUseFullPath is set on the HandlerMapping).
Therefore (theoretically) you should try accessing:
http://localhost:8080/projectName/spring-mvc-demo/
There is however another problem: your servlet mapping is an exact mapping, that does not match anything else beyond /projectName/spring-mvc-demo. You should replace it with a prefix mapping (see this question for an overview of servlet mappings):
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo/*</url-pattern>
</servlet-mapping>
If you want to shorten your URL, the DispatcherServlet is usually mapped as default servlet /. Be aware not to use the catchall prefix mapping /*, which would override the mapping of the JSP servlet.
Remark: There are some cases when Spring does not use the part after <servlet-path> for its routing, e.g. when you use an exact or extension mapping, the whole path after <context-path> is used.
Therefore if you use:
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc-demo</url-pattern>
</servlet-mapping>
you should specify:
#RequestMapping("/spring-mvc-demo")
public String getMainMenu() {
...
}
and use the URL http://localhost:8080/appName/spring-mvc-demo.

Spring MVC - Invalid view being executed (violating specified request mapping)

I have been trying to debug it for like hours and even tried to create the same project over and again. I don't know whether it's the issue due to IntelliJ IDEA or something else. I tried to google but couldn't find any solution. I am making a simple spring mvc demo application in intelliJ. A default structure was provided by intelliJ and a view "index.jsp" in web directory.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<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/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-mvc-demo-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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
DebugController.java
package com.luv2code.springdemo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Created by gaurav.ahirwar on 29/05/17.
*/
#Controller
public class DebugController {
#RequestMapping(value="/", method = RequestMethod.GET)
public String debug() {
return "hello";
}
}
The problem is that whenever I run the project and hit localhost:8080/ then index.jsp is executed but according to my configuration and mapping hello.jsp should be loaded. It may be possible that I am doing some silly mistake but I have tried as much as I can and now really frustrated. Please help me out. Thanks.
localhost:8080
Try this mapping:
#RequestMapping(value = {"", "/", "index.jsp"}, method = RequestMethod.GET)
I guess localhost:8080 equals "" path, but not /

spring adding external jar with controller not working

I have made a Java project having spring controller that handle url
Here is the controller code
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
#EnableWebMvc
#Controller
public class SmartContentValidator {
#RequestMapping(value = "/validate")
public String validate() {
System.out.println("Yo");
return "YO";
}
}
Then I exported above java project as jar file.
Then created a new web project and added the above jar in build path. Here is the web.xml and dispatcher-servlet.xml
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SmartContentValidatorTest</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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
dispatcher-servlet.xml
<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-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com" />
<context:annotation-config/>
<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>
Now when I try to hit http://localhost:8080/project_name/validate
it shows 404-not found on browser and
WARNING: No mapping found for HTTP request with URI [/SmartContentValidatorTest/validate/] in DispatcherServlet with name 'dispatcher'
in my eclipse console.
What is the issue? Am I missing something?
Edit
Project Structure
I am not sure if it helps with your case but I think that in standalone application there would be an issue as #EnableWebMvc should be used with Java config - class annotated with #Configuration, not with #Controller bean.
As you use xml config you can try put <mvc:annotation-driven />, see https://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-config-enable for more details.
You would also need to provide your application context as dispatcher servlet param like in example http://docs.spring.io/spring-flex/docs/1.0.x/reference/html/ch02s02.html:
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Normally this is caused by Spring not correctly finding your controller. If you enable DEBUG logging Spring will log what beans are registered when you start the application.
If it is correctly loading your controller then check that your root path is correct (I think this is your war name by default) which again should be displayed in the logging during startup.

HelloWorld SpringMVC - HTTP Status 404

Im trying to create my first Spring MVC project but I keep getting error http status 404.
If i try to open an .jsp file outside of /views (So not using Spring MVC) it works fine
Here are my files:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>fj21-tarefas</display-name>
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-context.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:component-scan base-package="br.com.caelum.tarefas" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
Controller
package br.com.caelum.tarefas.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class OlaMundoController {
#RequestMapping("/olaMundoSpring")
public String execute(){
System.out.println("Executando a lógica com Spring MVC");
return "ok";
}
}
Libs
aopalliance-1.0.jar
commons-logging-1.2.jar
javax.servlet.jsp.jstl-1.2.1.jar
javax.servlet.jsp.jstl-api-1.2.1-3.jar
jcl-over-slf4j-1.7.12.jar
joda-time-2.4.jar
log4j-over-slf4j-1.7.12.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.7.RELEASE.jar
spring-aspects-4.1.7.RELEASE.jar
spring-beans-4.1.7.RELEASE.jar
spring-context-4.1.7.RELEASE.jar
spring-context-support-4.1.7.RELEASE.jar
spring-core-4.1.7.RELEASE.jar
spring-expression-4.1.7.RELEASE.jar
spring-instrument-4.1.7.RELEASE.jar
spring-instrument-tomcat-4.1.7.RELEASE.jar
spring-jdbc-4.1.7.RELEASE.jar
spring-jms-4.1.7.RELEASE.jar
spring-messaging-4.1.7.RELEASE.jar
spring-orm-4.1.7.RELEASE.jar
spring-oxm-4.1.7.RELEASE.jar
spring-test-4.1.7.RELEASE.jar
spring-tx-4.1.7.RELEASE.jar
spring-web-4.1.7.RELEASE.jar
spring-webmvc-4.1.7.RELEASE.jar
spring-webmvc-portlet-4.1.7.RELEASE.jar
spring-websocket-4.1.7.RELEASE.jar
When your controller method returns a String, the String to be returned is the name of your view (jsp, template, etc).
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
According to the setup above (provided by you), Spring in intended to return a view located on /WEB-INF/views and the name of the view must has the suffix ".jsp".
Please, make sure the file "ok.jsp" is located inside this path, otherwise, Spring will not be able to load it.
In case "ok.jsp" is already located in this folder, please, provide us full stacktrace of the exception you are facing.
Yes, the view "ok.jsp" is located on the corrected folder.
I ended up fixing the issue by deleting all the libs and adding the dependency using maven.
If you are using spring, you should us spring boot to get started with any new service. Takes care of your basic dep tree. Refer to the tutorial at - http://projects.spring.io/spring-boot/#quick-start
Pretty straight forward.

Spring MVC: How to get the right URL for controllers?

I'm a newbie to web development as well as Spring MVC. I'm testing on a silly example for the controllers, however, it seems that the controller has never been called, or at least it doesn't work as expected.
The goal is to click on a hyperlink in the "test.jsp" page and direct to "customers.jsp" page, with the controller being called, so that a message will be passed to the "customers.jsp" page.
Now I have the "customers.jsp" page showing correctly but with no transferred message displayed. The URL is http://localhost:8080/WebProject/view/customers.jsp.
Could you please help me find out the problem? I suppose it's because of the URL mapped to the controller. But is there a way that I can check the right URL to map for controllers? Thanks!
* updates at the end *
The project structure is like this:
--webapp
|--view
|--*.jsp
|--WEB-INF
|--*.xml
Here is my code:
test.jsp
<a href='<c:url value="customers.jsp"/>'>show customers</a>
or
show customers
both works properly.
customers.jsp
<h2>return: ${list}</h2>
Controller:
#Controller
#RequestMapping("/customers")
public class MenuController {
#RequestMapping(method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
and the configuration:
web.xml
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
myDispatcher-servlet.xml
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
UPDATE for those who might see this post later :
There is indeed a problem of url mapping as indicated by #javafan and #Beri. But after changing the url to show customers I still got Error 404 at http://localhost:8080/WebProject/customers. This is in fact caused by a package scanning error, so the Controllers cannot be found. To solve this, simply change the line in the "myDispatcher-servlet.xml" file:
<context:component-scan base-package="com.example.spring.controller" />
<context:component-scan base-package="com.example.spring.model" />
to
<context:component-scan base-package="controller" />
<context:component-scan base-package="model" />
Hope it helps :)
Javatan is right:
Try
<protocol>/<host>:<port>/WebProject/customers
Inside InternalResourceViewResolver bean definition you define prefix and suffix, those values are used to find jsp files in yur application. And have nothing in common with urls that your application has created.
Your url paths are defined inside controllers( not inside InternalResourceViewResolver).
So after application name you are using paths defined inside controllers.
You can also define #RequestMapping("/customers") on a method:)
Check if you have in your bean definition line, that looks like this:
<context:component-scan base-package="com.project.web.controllers"/>
This definition will scan for all classes inside com.project.web.controllers package (and subpackages) to find classes marked with #Controller annotation. You can check for more here:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html, chapters:
17.3.1 - how to register a controller
17.2 -how web.xml looks like
I hope this will help.
Ok, I have run local example with JSP:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>myDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Controller:
#Controller
public class MenuController {
#RequestMapping( value="/customers", method = RequestMethod.GET)
public ModelAndView listCustomers() {
ModelAndView model = new ModelAndView("customers");
model.addObject("list", "controller: a list of customers");
return model;
}
}
dispatcher-servlet.xml
<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.programcreek.helloworld.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
You can use this code to get the url of the server:
"show customers"
And you are mapping the Controller to /customers
so the url should be:
http://localhost:8080/WebProject/customers/customers.jsp
You are directly calling customer.jsp. Instead you have to call controller which in turn pass the request to customer.jsp. in HREF attribute, you need to mention value which is given in #RequestMapping annotation in your controller.
For more detailed example, visit my blog Here

Categories