404 on REST #RequestMapping - spring-mvc

I'm trying to figure out why I'm getting a 404 error here, but I just can't see it.
Here is my web.xml:
<servlet>
<servlet-name>ep</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ep</servlet-name>
<url-pattern>/ep/*</url-pattern>
</servlet-mapping>
Here is my ep-servlet.xml (Only "beans" tags surround the following code):
<mvc:annotation-driven />
<context:component-scan base-package="server.service2">
</context:component-scan>
Here is the class itself:
package server.service2;
....
#Controller
#RequestMapping("/user")
public class LoginService
{
#RequestMapping(value = "/check/{username}", method = RequestMethod.GET)
public void checkUsername(#PathVariable("username") String username)
{
}
}
The url i'm hitting with a browser is:
http://<server>:<correct-port>/ep/user/check/username
And it comes back with a 404! Can anyone spot what I'm doing wrong here?

Hi #Amorgos you can order your files to next:
web.xml
<servlet>
<servlet-name>ep</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ep</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Add Support for Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
ep-servlet.xml
<context:annotation-config />
<context:component-scan base-package="server.service2" />
<beans:bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
<!-- Register the bean -->
<beans:bean class="server.service2.LoginService" />
I hopes these helps :)

Related

How to configure #Controller and #RequestMapping

I have a controller class using annotations for request mapping however it does not seem to be recognized when I try to connect browser to it
When I try to connect using url http://localhost:8088/carMart
I get the error:
The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Controller class:
package com.store.controller
#Controller
public class CarMartController {
#RequestMapping("/carMart")
public String navigateToCarMart( ModelMap model) {
return "carMart";
}
}
web.xml
<description>
Configuration file for the Store Application
</description>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/store-servlet.xml</param-value>
</context-param>
<servlet>
<servlet-name>store</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>store</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
store-servlet.xml:
<context:component-scan base-package = "com.store" />
<context:annotation-config/>
<bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" />
<property name = "suffix" value = ".jsp" />
</bean>
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<mvc:resources mapping="/jquery/**" location="/jquery/"/>
<import resource="/spring/store.xml" />
<import resource="/hibernate/hibernate-configuration.xml" />
I have tried various combinations for the RequestMapping but always the same issue, also tried setting base-package to com.store.controller but still same error

Primefaces Submit button of SelectOneButton does not work

As i am a newcomer for primefaces framework i tried to use SelectOneButton component with nearly the same code on primefaces.org's demo however i couldnt manage to get it work.
When i click on the submit button i cant see any result.
Below is my code for my xhtml and bean files. Any help is appreciated mostly. Thanks.
soru.xhtml file: (located under Webcontent/faces folder)
<h:form id="form">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="Options: " />
<p:selectOneButton value="#{buttonBean.number}">
<f:selectItem itemLabel="Option 1" itemValue="1" />
<f:selectItem itemLabel="Option 2" itemValue="2" />
<f:selectItem itemLabel="Option 3" itemValue="3" />
</p:selectOneButton>
</h:panelGrid>
<p:commandButton value="Submit" update="display"/>
<h:outputText id="display" value="#{buttonBean.number}" />
</h:form>
ButtonBean.java file:
package com.ali.deneme;
import java.io.Serializable;
import javax.annotation.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import java.util.UUID;
#ManagedBean
#ViewScoped
public class ButtonBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* my ButtonBean class file - Ali Kerim Erkan
*/
private Integer number;
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
My web.xml file:
<?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"
id="WebApp_ID" version="3.0">
<display-name>deneme</display-name>
<welcome-file-list>
<!-- <welcome-file>/faces/hello.xhtml</welcome-file> -->
<!-- <welcome-file>/faces/datatableRowSelectionRadioCheckbox.xhtml</welcome-file> -->
<welcome-file>./faces/soru.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
</web-app>
As #stg mentioned use
import javax.faces.bean.ManagedBean;
and also you are mapping two url-pattern to FacesServlet.
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FacesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FacesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Remove mapping with '/faces/' pattern and use mapping with '.xhtml' pattern when calling your url.
example: http://example.com/page.xhtml
and please dont use faces as a directory name because it is not a good directory name for a JSF app.
faces is a common url pattern since first days of jsf.
You are using a wrong import. Instead of
import javax.annotation.ManagedBean;
you have to use
import javax.faces.bean.ManagedBean;

Spring mvc 3 with tiles 2 gives 400 bad request

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>

Spring /BlazeDS /Flex Remoting HelloWorld ->Fault:Could not establish a connection

I am simply trying to do a hello world using the above three.
I have read/researched for a day but was not able to resolve the issue.
First some code snippets.
web.xml:
<display-name>BlazeDS</display-name>
<description>BlazeDS Application</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/*-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>flex.messaging.HttpFlexSession</listener-class>
</listener>
<servlet>
<servlet-name>flex</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>flex</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
flex-servlet.xml(my spring dispatcher is named flex in web.xml)
<flex:message-broker>
<flex:message-service
default-channels="my-streaming-amf,my-longpolling-amf,my-polling-amf" />
</flex:message-broker>
<!-- Expose the productService bean for BlazeDS remoting -->
<flex:remoting-destination ref="echoService" />
app-config.xml(spring beans)
<?xml version="1.0" encoding="UTF-8"?>
<!-- Implementation of ProductDAO using low-level JDBC -->
<bean id="echoService" class="com.example.day1.EchoService">
</bean>
remoting-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<adapters>
<adapter-definition id="java-object"
class="flex.messaging.services.remoting.adapters.JavaAdapter"
default="true" />
</adapters>
<default-channels>
<channel ref="my-amf" />
</default-channels>
Finally remote object in Flex 4.5:
<mx:RemoteObject id="ro"
destination="echoService"
fault="onFault(event)"
result="onResult(event)"
showBusyCursor="true">
</mx:RemoteObject>
I am using glassfish , flex builder 4.5 and my service class is just a hello world.
I can get it to work fine without Spring. But once I put Spring in the mix, my fault handler
says "Unable to connect to echoService. Any pointers appreciated.

Got Problems with Tuckey UrlRewriteFilter and Spring 3 Web MVC

I have a Spring Web MVC application and I want to use RESTful URLs. Regrettably I haven't found any configuration that works for me with Tuckey's UrlRewriteFilter.
I'm using the "DefaultAnnotationHandlerMapping" and I added "urlrewritefilter.jsp" to my classpath and copied "urlrewrite.xml" into "/WEB-INF/".
I wan't to achieve that xyz://www.domain.com/abc will be redirected (invisible for the user) to xyz://www.domain.com/app/abc to catch it with my Controller and to let xyz://www.domain.com/css untouched.
My "web.xml" is configured this way:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="xyz://java.sun.com/xml/ns/javaee" xmlns:xsi="xyz://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xyz://java.sun.com/xml/ns/javaee xyz://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- Context -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
</param-value>
</context-param>
<!-- Context Loader -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Tuckey UrlRewriteFilter -->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Dispatcher Servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
<!-- All the rest... -->
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>
</web-app>
My "urlrewrite.xml" is configured this way:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"xyz://tuckey.org/res/dtds/urlrewrite3.2.dtd">
<urlrewrite default-match-type="wildcard">
<rule>
<from>/**</from>
<to>/app/$1</to>
</rule>
<outbound-rule>
<from>/app/**</from>
<to>/$1</to>
</outbound-rule>
</urlrewrite>
My AbcController.java for mapping xyz://www.domain.com/abc has such methods:
#RequestMapping(value = "/app/abc", method=RequestMethod.GET)
public void displayRegistration(Model model)
{
...
}
But I always receive "HTTP Status 404" when trying to access xyz://www.domain.com/ or xyz://www.domain.com/abc :-(
It would be very nice if somebody could help.
Greetings
Benny
Did you try with
#RequestMapping(value = "/abc", method=RequestMethod.GET)
Really late reply.
You need to add the grouping symbols (.*)
<urlrewrite >
<rule>
<from>^/(en|de)/(.*)$</from>
<to >/$2?lang=$1</to>
</rule>
</urlrewrite>

Resources