bean not injecting on spring MVC - spring

im learning Spring MVC and i was working on a basic form example, but i dont why a bean its not injecting the information correctly so i would like to know if someone can direct me.
The controller
package com.carloscortina.Test;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.carloscortina.toy.model.Member;
#Controller
public final class NomineeController {
private static final Logger log=
Logger.getLogger(NomineeController.class);
private String thanksViewName ="thanks";
public void setThanksViewName(String thanksViewName) {
this.thanksViewName = thanksViewName;
}
#RequestMapping(method = RequestMethod.GET)
public Member form() { return new Member();}
#RequestMapping(method = RequestMethod.POST)
public String processFormData(Member member){
log.info("Processing nominee: " + member);
log.info("thanksViewName: " + thanksViewName);
return thanksViewName;
}
the root-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:p="http://www.springframework.org/schema/p"
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 -->
<bean id="formAnswer"
class="com.carloscortina.Test.NomineeController"
p:thanksViewName="thanks" />
</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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.carloscortina.Test" />
im using STS as IDE and the Spring mvc template included.
im not sure the bean its not being injected in the controller so when the form its submitted it redirects correctly, if i hardcode the thanks in the controller it works.
Thanks in advance for the help i know that this might be a basic error , so thank you.
*Edit
Well, maybe it related to the annotation driven, but I'm still not sure, i havent been able to make this thing work.
So the controller cant rad a bean from the root-context.xml?
or anyone can tell me how to do it with auto wire, the idea its just to not hardcode the value of thanksViewName on the controller.

You need to add the following to your servlet-context.xml configuration file, you don't need to declare annotated beans in the XML file.
<mvc:annotation-driven />
<context:component-scan base-package="com.carloscortina" />
My suggestion would be to download Spring Stool Suite (STS) and create a new spring template project (selecting the MVC template), it will create a runnable project and that way you can see how everything is put together.

Related

Why report:“No mapping found for HTTP request with URI “

first time build a springmvc web project,use jetty as web container,but when jetty started, but when request the url:"http://localhost:8080/hello/mvc" the
broswer report:
"Problem accessing /hello/mvc. Reason: ".And the console report:"十一月 18, 2018 10:34:16 上午 org.springframework.web.servlet.PageNotFound noHandlerFound
警告: No mapping found for HTTP request with URI [/hello/mvc] in DispatcherServlet with name 'mvc-dispatcher'
"
my web.xml:
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--param-name>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-name-->
<param-value>classpath*:spring/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
mvc-dispatcher-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">
<!-- scan the package and the sub package -->
<context:annotation-config/>
<context:component-scan base-package="com.zhangbing.springmvc">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<property name ="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsps/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>`enter code here`
</beans>
The hierarch of my project:
enter image description here
circled by blue line is the controller
The contoller helloMvcController:
package com.zhangbing.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping("/hello")
public class helloMvcController {
#RequestMapping("/mvc")
public String helloMvc()
{
return "home";
}
i search lots question like this,but no one can solved my problem,please help.
I believe, Spring can not find your controller.The problem might be with the component scan.
Please change your component scan directive from
<context:component-scan base-package="com.zhangbing.springmvc">
to
<context:component-scan base-package="com.zhangbing.springmvc.*">
Then lets see what is the output.

Spring MVC routing in Google App Engine

I'm trying to build a spring mvc web on google app engine. But I can't make the routing/mapping work.
I have my HelloController, which indicates that I want to map to /hello
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/hello")
public class HelloController {
#RequestMapping(method = RequestMethod.GET)
public String Index() {
return "hello";
}
}
and a very simple hello.jsp file
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>
however, when I try to access /hello, I will get 404 not found.
here is my xml mapping file mvc-servlet.xml
<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.example.web" />
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
What am I missing ? How to fix it ?
Do you have a web.xml ?
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
ContextLoaderListener ties the ApplicationContext lifecycle to
ServletContext lifecycle and automate the creation of
ApplicationContext. ApplicationContext is the place for Spring beans
and we can provide it’s configuration through contextConfigLocation
context parameter. root-context.xml file provides the configuration
details for WebApplicationContext.
Her eis a tutorial http://www.journaldev.com/2433/spring-mvc-tutorial-for-beginners-with-spring-tool-suite

Can't use freemarker with Spring MVC 3

I created a simple Spring MVC 3 application and want to use freemarker template engine. I cofigure *-context.xml as describes in off Spring's docs, but in browser I get 404 Page not found error. this is my code:
HelloWorldController.java
#Controller
#RequestMapping("/hello")
public class HelloWorldController {
private static final Logger log = Logger.getLogger(HelloWorldController.class);
#RequestMapping(value="/{name}", method = RequestMethod.GET)
public String hello(#PathVariable String name, Model model) {
String result = "Hello, " + name;
model.addAttribute("result", result);
return "hello";
}
}
this is my hello.ftl in WEB-INF/freemarker folder
<!doctype html>
<html>
<head>
<title>Hello</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${result}. </P>
</body>
</html>
and my servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- freemarker config -->
<beans:bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<beans:property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</beans:bean>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<beans:property name="prefix" value="" />
<beans:property name="suffix" value=".ftl" />
<beans:property name="cache" value="false" />
</beans:bean>
<context:component-scan base-package="org.example.simple" />
</beans:beans>
what is wrong and why I get 404 when I go to localhost:8080/simple/hello/username ?
please, help
EDIT:
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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
its probably wrong configuration for spring mvc, not freemarker. Seems that spring cant find your Controller. Did you add dispatcherServlet in your web.xml?

Spring error 404 when trying to access MyController

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>

Spring RequestMapping using PathVariable results in 404

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.

Resources