I'm in trouble and would like your help. I'm a beginner in Spring MVC (and Spring at all). I have followed the http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/ but it isn't working on. I added a welcome file (index.jsp). When i enter (http://localhost:8080/SpringMVC) all right. But when i add the controller pattern (http://localhost:8080/SpringMVC/welcome), it doesn't work (HTTP Status 404). Here my configs:
web.xml
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
mvc-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.mkyong.common.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
and my folder structure is:
-> src
-> main
-> java
-> com
-> mkyong
-> common
-> controller
-> HelloController.java
-> resources
-> webapp
-> index.jsp
-> WEB-INF
-> mvc-dispatcher-servlet.xml
-> web.xml
-> pages
-> hello.jsp
Someone can help me?
404 means that the requested resource cannot be found. Make sure your controller is annotated with:
#Controller and #RequestMapping("/welcome")
From the link:
#Controller
#RequestMapping("/welcome")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
You have your servlet mapped to '/', and the controller you want to hit is accepting requests for '/welcome'
The request you need to make should be (http://localhost:8080/welcome). I don't know why he has that extra 'SpringMVC' in his example.
Also, add <mvc:annotation-driven/> to your mvc-dispatcher-servlet.xml to make sure it's using annotations on your controller(s). And add the mvc XML namespace also. My namespaces look like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
I would suggest getting rid of that welcome file until you have your controller working correctly to keep things simple
You must have a Controller mapped to "/".
#Controller
#RequestMapping("/")
public class StartController {
#RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
}
Include this library on your view to enable ${}
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
This will solve your problem.
Related
I am new to MVC.
I am trying to build a simple hello world application using Spring MVC.
I am getting 404 error as href doesn't goes to hello.jsp. I have my hello.jsp under my WEB-INf and index,jsp is under Web apps. Below is my code for the application. Thank you all in advance
#Controller
public class HelloController {
#RequestMapping(value = "/hello",method = RequestMethod.POST)
public ModelAndView mymethod(){
return new ModelAndView("hello","msg","Hello First Spring");
}
}
/////////////////////////// spring.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"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
///////////////////////// 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>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/.jsp</url-pattern>
</servlet-mapping>
</web-app>
//////////////////////////// index.jsp ////////////////////////////
<html>
<body>
<h2>Hello World!</h2>
<%= java.util.Calendar.getInstance().getTime() %>
click here
</body>
</html>
//////////////////////////// hello.jsp /////////////////////////
Message is: ${msg}]
[1]: https://i.stack.imgur.com/XUJ9j.png
Change your controller to GET
#RequestMapping(value = "/hello",method = RequestMethod.GET)
public ModelAndView mymethod(){
return new ModelAndView("hello","msg","Hello First Spring");
}
Also change your web.xml
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Situation : I am trying out a spring webapp. The name of app is spring-mvc-jquery-file-upload
My web.xml is as follows :
<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
And rest-servlet.xml is as follows :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.hmkcode.spring.mvc.controllers" />
<mvc:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
Now when application is run , index.html is displayed successfully . it has a following anchor tag :
Next
The method Next() that handles this request and returns a form.jsp is as follows :
#Controller
#RequestMapping("/controller")
public class FileController {
#RequestMapping(value="/next", method = RequestMethod.GET)
public ModelAndView Next() {
return new ModelAndView("C:\\Users\\Varrox\\Desktop\\"
+ "spring-mvc-jquery-file-upload\\src\\main\\"
+ "webapp\\form.jsp","wl-entity",new Sample());
}
}
Problem : The problem i am facing is that when i click on next link located in index.html i am getting an error saying "No mapping found for HTTP request with URI [/spring-mvc-jquery-file-upload/rest/controller/C:/Users/Varrox/Desktop/spring-mvc-jquery-file-upload/src/main/webapp/form.jsp] in DispatcherServlet with name 'rest'".
as you can see when Next() method in controller returns ModelAndView instance along with the viewname the app prefixes spring-mvc-jquery-file-upload/rest/controller before the viewname and that is why no mapping is found.
How do i remove this prefix so that mapping can occur correctly and my form.jsp is displayed .
You expect a Java app to be able to load a ressource that is not in the classpath. This will not work.
You need a viewResolver, that will be used to find a view by its name.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
In this exemple, note the prefix that handle the root path, and suffix the file extension
Then you simple use the file name :
return new ModelAndView("form","wl-entity",new Sample());
Full exemple here Spring MVC file upload example
I am working on Spring MVC sample. when i use the / it works well.
but when it comes to /xxx, it return WARNING.
WARNING: No mapping found for HTTP request with URI [/SimpleSpringMVC/index] in DispatcherServlet with name 'report'
I had searched many answers, but all of them don't work well.
And here are details of my sample.
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:tool.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>report</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/report-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>report</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
report-servlet.xml
<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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:utils="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
SimpleController.java
package com.tian.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller("Report")
public class SimpleController {
public SimpleController() {
System.out.println("Initial ...");
}
#RequestMapping("/index")
public String home() {
return "home";
}
#RequestMapping(value = "/info", method = RequestMethod.GET)
public #ResponseBody String getInfo() {
return "hello world";
}
}
tool.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-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="com.tian.controller" />
</beans>
Ok, i don' t know what is tool.xml for, but i would do something like this, put this line in report-servlet.xml <context:component-scan base-package="com.tian.controller" /> before <mvc:annotation-driven /> then in your web.xml put something like this :
<servlet>
<servlet-name>report</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>report</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
And take a look at this controller,
#Controller
#RequestMapping(value="/login")
public class LoginController {
#RequestMapping(method=RequestMethod.POST)
#ResponseBody
public String receiveLoginRequest(#RequestParam String username,#RequestParam String password){
//DO WHAT YOU WANT
}
}
And when you test a controller, localhost:8080/report/login/YOURMETHODMAP
You want to understand how to config your context my friend so instead of giving you a botched answer that doesn't really help you, here is a beautiful answer : ANSWER BY informatik01, giving you a nice and simple lecture
Long story short web application contexts are hierarchical and your mapping is ignored (more or less)
Hope this helps.
I am new to Spring. I am trying to integrate Spring in a section of a web app.
It has to work with urls like :
http://localhost:9080/myfolder/myspring
My web.xml includes:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/myspring-context.xml
</param-value>
</context-param>
<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/config/applicationContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/springviews</url-pattern>
</servlet-mapping>
applicationContext.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:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- registers all of Spring's standard post-processors for annotation-based configuration -->
<context:annotation-config />
<tx:annotation-driven/>
<tx:jta-transaction-manager/>
<bean id="properties" class="springcop.pojo.TestObject">
</bean>
</beans>
my myspring-context.xml is :
<?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.0.xsd
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">
<!-- Scans within the base package of the application for #Components to configure as beans -->
<!-- #Controller, #Service, #Configuration, etc. -->
<context:component-scan base-package="springcop"/>
<!-- Configures the #Controller programming model -->
<mvc:annotation-driven/>
<!-- Resolve logical view names to .jsp resources in the /WEB-INF/views directory -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/springviews/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
And here is MyController
package springcop;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class MyController {
public MyController () {
System.out.println("--->GestioneController");
}
#RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
System.out.println("--->test");
return "test";
}
}
The up is runniong without errors. Spring seems to work as I have printed out in my log "--->GestioneController which is in the constructor of my COntroller.
Anyway, when I open in the browser
http://localhost:9080/myfolder/myspring/test
to execute the test method in MyController I get 404 error.
What's should I do to make it work?
Thanks.
Assuming that the name of your webapp is myfolder as listed in the question, the url would be...
http://localhost:9080/myfolder/springviews/test
The problem is you have the dispatcher servlet mapped to springviews. You need to map the dispatcher servlet as follows in web.xml...
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/<url-pattern>
</servlet-mapping>
Edit: Also, you have put your spring configuration in a context-param, but you did not add the listener to pick it up. Add the following to your web.xml....
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
I have a spring application and I can't solve a problem. When I use a PathVariable, the value in my RequestMapping is getting added to the URI and the DispatchHandler is getting confused.
For example, when I request /Dashboard/project/1131/
I get HTTP Status 404 - /Dashboard/project/1131/WEB-INF/jsp/projectDetail.jsp
The project/1131 gets added into the path for some reason
When I request /Dashboard/projects Spring finds my .jsp and presents it. /project/{projectId} doesn't work - I get the behavior described above.
Here's my controller
#Controller
public class ProjectController {
protected final Log logger = LogFactory.getLog(getClass());
#RequestMapping(value = "projects", method=RequestMethod.GET)
public String projects() {
return "/projects";
}
#RequestMapping(value="/project/{projectId}", method=RequestMethod.GET)
public String project(#PathVariable("projectId") String projectId, ModelMap map) {
map.put("projectId", projectId);
logger.info("Project Id: " + projectId);
return "/projectDetail";
}
}
My configuration
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Webapp name -->
<display-name>Dashboard</display-name>
<!-- default file name -->
<welcome-file-list>
<welcome-file>home</welcome-file>
</welcome-file-list>
<!-- spring listener (all spring jars need to be in WEB-INF/lib -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- set servlet to DispatcherService -->
<servlet>
<servlet-name>dashboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- hand all requests to dispatcher service -->
<servlet-mapping>
<servlet-name>dashboard</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
AppConfig
#Configuration
public class AppConfig {
#Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}
dashboard-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:mvc="http://www.springframework.org/schema/mvc"
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/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-3.0.xsd">
<!-- the packages to scan for annotations -->
<context:component-scan base-package="com.dennisstevens.dashboard.controller" />
<context:component-scan base-package="com.dennisstevens.dashboard" />
<context:component-scan base-package="com.dennisstevens.dashboard.domain" />
<!-- Tell Spring that MVC components are #Annotation Driven -->
<mvc:annotation-driven />
<!-- Tell Spring Dispatch Handler to look in WebContent/resources/ for resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Tell Spring to scan for config annotations -->
<context:annotation-config/>
<!-- TODO: Figure out how to load this in AppConfig -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
</beans>
This has to be simple - but I have looked through the documentation and searched the internet for hours. It looks like I am doing everything right from what I can see.
I think having a leading slash in resolver.setPrefix("/WEB-INF/jsp/"); should fix it for you.