Controller
#Controller
public class Tester {
#RequestMapping(value="testPost", method = RequestMethod.POST)
public ModelAndView testPost(){
ModelAndView _mv = new ModelAndView();
_mv.setViewName("shared/post");
return _mv;
}
}
HTML
<form action="testPost" method="post">
<input type="submit" value="Submit" />
</form>
Web.xml
<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" id="WebApp_ID" version="3.0">
<display-name>iCubeHRS</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
<filter>
<filter-name>site_mesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>site_mesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>20</session-timeout>
</session-config>
</web-app>
Question
Once set "method" attribute to "POST", when you hit submit button, it always turn into 405 - Request method 'POST' not supported, if delete method attribute from conotroller, and delete method="post" from HTML as well, it works, anyone know how to solve this problem?
Update
I think I found the problem, this issue caused by sitemesh3, after i removed sitemesh3 features from web.xml, POST works fine, but I don't know how to solve it.
I am not sure if this is relevant to your setup but I had the same error (405-post not supported)
Initially I thought it was sitemesh related. However when I looked into it a bit more in my case it was because I was using <mvc:resources /> to provide a static mapping to the decorator.
it was <mvc:resources /> that was not accepting the post requests for the decorator file as sitemesh was trying to access it using a Post request.
I changed the mapping of my decorator file to make sure it was static and responded to POST and GET requests. More details here Spring: not accept POST request under mvc:resources? how to fix that
The code I used is
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/DecTest/**" value="myResourceHandler" />
</map>
</property>
<property name="order" value="100000" />
</bean>
<bean id="myResourceHandler" name="myResourceHandler"
class="org.springframework.web.servlet.resource.ResourceHttpRequestHandler">
<property name="locations" value="/DecTest/" />
<property name="supportedMethods">
<list>
<value>GET</value>
<value>HEAD</value>
<value>POST</value>
</list>
</property>
<!-- cacheSeconds: maybe you should set it to zero because of the posts-->
</bean>
Well as you figured out that the problem was with sitemesh. this link is to a project integrating springMVC with sitemesh
Related
I have a spring app in which i have controller
#RequestMapping(value = "/candidateHome", method = RequestMethod.GET)
public ModelAndView candidateHome() {
return new ModelAndView("candidateHome", "command", new Candidate());
}
a jsp name candidateHome
web.xml file
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/rest-servlet.xml</param-value>
</context-param>
and servlet config file rest-servlet.xml
<context:component-scan base-package="com.ojp.controller" />
<mvc:annotation-driven />
<bean id="viewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
.
when i am trying to access http://localhost:8080/RBTech/candidateHome,
I am getting error 'HTTP Status 404 - /RBTech/WEB-INF/candidateHome.jsp'.
Can any one tell me what is wrong in this code. Thanks
When I'm using InternalResourceViewResolver alone my views will be resolved correctly. When I add annotation-driven to my configuration file, my views are resolved but my resources are not. This is driving me crazy...
src
main
java
resources
css
js
ajaxHandler.js
webapp
WEB-INF
spring
appServlet
servlet-context.xml
views
index.jsp
internalview.jsp
web.xml
Here's my 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.introduction</display-name>
<servlet>
<servlet-name>ApplicationServlet</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>
</servlet>
<servlet-mapping>
<servlet-name>ApplicationServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>WEB-INF/views/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
The servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="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">
<context:component-scan base-package="com.tsystems.sample" />
<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>
<mvc:resources location="/js/**" mapping="/resources/js/" />
<!--mvc:default-servlet-handler/-->
<mvc:annotation-driven/>
</beans:beans>
The basic flow:
Index.jsp has a form that passes "sender:index" as POST to the indexController. this should fall down to the following method:
#RequestMapping(value = "/Forward", method = RequestMethod.POST)
public ModelAndView forward(#RequestParam(value = "sender", required = true) String sender, Model model) {
m_logger.info(String.format("Captured sender attribute: " + sender));
ModelAndView mav = new ModelAndView("internalview");
mav.addObject("sender", sender);
return mav;
}
This works so far, the info message appears in the server log and the internalview show up. In my internalview.jsp I try to load the js as follows:
<script type="text/javascript" src="<c:url value="/js/ajaxHandler.js"/>"></script>
ending up with a nice 404 error and the message below in the server.log:
[org.springframework.web.servlet.PageNotFound] (default task-20) No mapping found for HTTP request with URI [/spring.introduction/js/ajaxHandler.js] in DispatcherServlet with name 'ApplicationServlet'
If I remove annotation-driven from the config file even my view becomes 404 NOT FOUND. If I remove annotation-driven AND mvc:resources it works but of course the .js is not loaded.
How can I resolve this issue? (There are similar questions as this but after trying out the answers for those, none of them worked so while I accept that the question may be duplicate to others, I'm still opening because none of their answers solves the problem)
Web resources such as JavaScript and CSS should typically be placed under src/main/webapp directory. So in your case (based on your mvc:resources mapping), you should create resources directory in src/main/webapp and move the js and css directories from src/main/resources there.
src/main/webapp/resources/js
src/main/webapp/resources/css
I need to use Spring-mobile to detect the device. I have seen lot of examples with spring-mobile and spring mvc but none with webflow. Below is a sample webflow, I need to use device detection so I can redirect the page to mobile or table or desktop based on device.
webflow-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:webflow="http://www.springframework.org/schema/webflow-config"
xmlns:faces="http://www.springframework.org/schema/faces"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/webflow-config
http://www.springframework.org/schema/webflow-config/spring-webflow-config.xsd
http://www.springframework.org/schema/faces
http://www.springframework.org/schema/faces/spring-faces.xsd">
<!-- JSF Specific -->
<!-- A listener maintain one FacesContext instance per Web Flow request. -->
<bean id="facesContextListener"
class="org.springframework.faces.webflow.FlowFacesContextLifecycleListener" />
<!--- Executes flows: the central entry point into the Spring Web Flow system
- -->
<webflow:flow-executor id="flowExecutor">
<webflow:flow-execution-listeners>
<webflow:listener ref="facesContextListener" />
</webflow:flow-execution-listeners>
</webflow:flow-executor>
<!-- The registry of executable flow definitions -->
<webflow:flow-registry id="flowRegistry"
flow-builder-services="flowBuilderServices" base-path="/WEB-INF/flows">
<webflow:flow-location-pattern value="/**/*-flow.xml" />
</webflow:flow-registry>
<!-- Configures the Spring Web Flow JSF Integration -->
<faces:flow-builder-services id="flowBuilderServices"
development="true" />
<faces:resources />
<bean class="org.springframework.faces.webflow.JsfFlowHandlerAdapter">
<property name="flowExecutor" ref="flowExecutor" />
</bean>
<bean id="faceletsViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.faces.mvc.JsfView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".xhtml" />
</bean>
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="order" value="1" />
<property name="flowRegistry" ref="flowRegistry" />
<property name="defaultHandler">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
</bean>
<!-- Dispatches requests mapped to org.springframework.web.servlet.mvc.Controller
implementations -->
<bean
class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
</beans>
flow main
<?xml version="1.0" encoding="UTF-8"?>
<flow xmlns="http://www.springframework.org/schema/webflow"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/webflow
http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd">
<view-state id="welcome">
</view-state>
</flow>
Web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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>WebFlowSample</display-name>
<!-- - Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener. -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/application-config.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Resource mapping -->
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<!-- - Servlet that dispatches request to registered handlers (Controller
implementations). -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<!-- In order for JSF to bootstrap correctly -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
Controller
/**
* Handles requests for the application home page.
*/
#Controller
#RequestMapping("/wb")
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
/**
* Simply selects the home view to render by returning its name.
*/
#RequestMapping("/create/")
public String home(Device device, Model model) {
if (device == null) {
logger.info("no device detected");
} else if (device.isNormal()) {
logger.info("Device is normal");
} else if (device.isMobile()) {
logger.info("Device is mobile");
} else if (device.isTablet()) {
logger.info("Device is tablet");
}
// where main is the flow id for welcome page
return "main";
}
}
Updated:
I need the homecontroller class to call the flow, this is not the right way. But can anyone tell how to call?
(This is a reply to your comment, but too large to be a comment itself.)
In order to add interceptors to Webflow, add them to your FlowHandlerMapping bean definition. Like this:
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
...
<property name="interceptors">
<list>
<bean class="org.springframework.mobile.device.DeviceResolverHandlerInterceptor"/>
</list>
</property>
</bean>
The following is my application-context.xml
<context:component-scan base-package="org.godfather"></context:component-scan>
<mvc:view-controller path="/main.do" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
Question:
If I comment out <mvc:view-controller path="/main.do" /> then http://localhost:8181/tiles-app-1.1.1/header.do url is working, but if it is NOT commented out, then the url is not working, getting No mapping found for HTTP request with URI [/tiles-app-1.1.1/header.do] in DispatcherServlet with name 'mytiles' why is it so?
Web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
</param-value>
</context-param>
<servlet>
<servlet-name>mytiles</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mytiles</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Controller
package org.godfather;
#Controller
public class AppointmentController {
#RequestMapping("/header")
public void get() {
System.out.println("get()");
}
Thanks in advance.
You are passing the same Spring Bean definition file (/WEB-INF/application-context.xml) to the ContextLoaderListener and to the DispatcherServlet.
The DispatcherServlet should have its own Spring Bean definition file
See this question. All Spring MVC controllers and specific MVC xml should be in a different file.
I'm trying to add Apache Tiles to a simple Spring MVC webapp I'm playing with and I can't seem to get it to work (it worked without Tiles). Any request I make gives back 400 bad request, nothing appears in the log (even set to DEBUG) so I'm not sure where to start debbuging. As far as I can tell the Controller mapped method is never called as there's logging in there and it doesn't appear in the log (plus before that I would get a lot of debug info from spring about resolving the mapping to the controller before it was actually called - which now doesn't appear).
My config files are as follows (all under /WEB-INF/):
web.xml:
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>myapp</display-name>
<!-- Enable escaping of form submission contents -->
<context-param>
<param-name>defaultHtmlEscape</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Handles Spring requests -->
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
</web-app>
myapp-servlet.xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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:p="http://www.springframework.org/schema/p"
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.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">
<!-- The controllers are autodetected POJOs labeled with the #Controller
annotation. -->
<context:component-scan base-package="com.myapp.controller"
use-default-filters="false">
<context:include-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<!-- Allows for mapping the DispatcherServlet to "/" by forwarding static
resource requests to the container's default Servlet -->
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.tiles2.TilesView" />
</bean>
</beans>
tiles.xml
<tiles-definitions>
<definition name="product_detail" template="/WEB-INF/layout/detail.jsp">
<put-attribute name="header" value="/WEB-INF/view/header.jsp" />
<put-attribute name="banner" value="" />
<put-attribute name="body" value="/WEB-INF/view/product.jsp" />
<put-attribute name="footer" value="/WEB-INF/view/footer.jsp" />
</definition>
</tiles-definitions>
The layout just contains one div for each part wrapping a tag. All the views contain simple code like a header or a div.
And for the controller
ProductController.java:
#Controller
#RequestMapping("/product")
public class ProductController {
protected Logger logger = Logger.getLogger(getClass());
#Autowired
private ProductService productService;
#RequestMapping(value = "/{id}", method = RequestMethod.GET)
public ModelAndView getProduct(#PathVariable Long id) {
logger.info("GET product " + id);
Product product = productService.find(id);
ModelAndView mv = new ModelAndView("product_detail", "product", product);
return mv;
}
}
Deploying this with maven embedded tomcat plugin and going to localhost:8080/myapp/product/1 just gives HTTP 400 code without any other indication that something went wrong. There is a product in the DB with that id and everything from the controller down works, as I tried it before adding tiles.
Sorry for the code drop but I can't get this to work for some time now, and I have no idea what else to try or where to start debugging.
Is there some way to force logging what the problem was when a 400 bad request is returned?
You're missing the reference to your myapp-servlet.xml in the servlet configuration.
<!-- Handles Spring requests -->
<servlet>
<servlet-name>myapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/myapp-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myapp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>