I've set up some spring MVC configurations before but this time it seems that I'm missing something. Here comes the configuration :
web.xml :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml
</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml :
<context:annotation-config />
<context:component-scan base-package="mypackage.controller" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/gupld">uploadController</prop>
</props>
</property>
</bean>
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
ManController.java :
package mypackage.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class MainController {
#RequestMapping("/test")
public ModelAndView welcome() {
ModelAndView mav = new ModelAndView("_index");
return mav;
}
}
And when I call http://127.0.0.1:8080/mycontext/test/ I get:
http-8080-1 INFO 2012-12-26 09:27:24,799 SimpleUrlHandlerMapping - Mapped URL path [/test] onto handler 'mainController'
http-8080-1 WARN 2012-12-26 09:27:24,887 PageNotFound - No mapping found for HTTP request with URI [/mycontext/test/] in DispatcherServlet with name 'dispatcher'
Any idea?
You are using url-pattern as /test/* to load dispatcher servlet.
But you are sending /mycontext/test/ which is not mapping to provided url.
Try to use this,
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*/test</url-pattern>
</servlet-mapping>
Or Use This
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Related
I have a spring project in which i am applying an aspect before RequestMapping Annotation within Controller Class. i have tested the same in tomcat there was no issue but while i am writing the same code in jboss aop call is getting skipped.
the expression for pointcut is :
#Pointcut(" within(#org.springframework.stereotype.Controller *) && #annotation(requestMapping) && execution(public * *(..))")
web.xml
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config/springreport-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring_config/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
springreport-servlet.xml
<context:component-scan base-package="com.*"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:default-servlet-handler />
<aop:aspectj-autoproxy proxy-target-class="true"/>
First of all I have have generic http servlet which I want to run in specific contexts depending on URL mapping (so I switched to HttpRequestHandler implementation). Please consider simplified example. In web.xml I have following configuration:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>testServlet1</servlet-name>
<servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>testServlet2</servlet-name>
<servlet-class>local.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>testServlet1</servlet-name>
<url-pattern>/test1/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>testServlet2</servlet-name>
<url-pattern>/test2/*</url-pattern>
</servlet-mapping>
My servlet is implementation of HttpRequestHandler and looks like here:
public class TestRequestHandler implements HttpRequestHandler {
private Counter counter;
public void setCounter(Counter counter) {
this.counter = counter;
}
#Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.write("<h1>Hello World</h1>");
writer.write("<h1>" + counter.getName() + ": " + counter.getValue() + "</h1>");
}
}
So I want to init both servlet beans (testServlet1 and testServlet2) with different Counters beans and have one root context(applicationContext.xml) for common beans and two separate servlet contexts that could extend and override root context (testServlet1-servlet.xml and testServlet2-servlet.xml). So configuration should be something like this:
<bean id="counter" class="local.Counter">
<property name="name" value="CounterA"/>
</bean>
<bean id="testServlet1" class="local.TestRequestHandler">
<property name="counter" ref="counter"/>
</bean>
and
<bean id="counter" class="local.Counter">
<property name="name" value="CounterB"/>
</bean>
<bean id="testServlet2" class="local.TestRequestHandler">
<property name="counter" ref="counter"/>
</bean>
Is it possible and how could such configuration be implemented? I thought about DispatcherServlet but don't understand how it could be configured with implementation of HttpRequestHandler.
I have found a solution to my task. In few words - We create two DispatcherServlets and each servlet configuration has its own context which extends the root context. Here is the configuration:
web.xml - setup for two DispatcherServlets
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher1</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>dispatcher2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher1</servlet-name>
<url-pattern>/test1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher2</servlet-name>
<url-pattern>/test2</url-pattern>
</servlet-mapping>
applicationContext.xml - root context and common beans goes here
<bean id="counter" class="local.Counter">
<property name="name" value="CounterC"/>
</bean>
dispatcher1-servlet.xml - overrides implementation of common beans and specific configuration in own context
<bean id="counter" class="local.Counter">
<property name="name" value="CounterA"/>
</bean>
<bean name="testServlet" class="local.TestRequestHandler">
<property name="counter" ref="counter"/>
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/test1">testServlet</prop>
</props>
</property>
</bean>
I tried to find some explanation for what really happens when we do not register ContextLoaderListener in our web.xml file, mainly because I have a simple Spring + Hibernate app that works fine if I DO NOT declare ContextLoaderListener. When I do, I get 404 error.
web.xml:
<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/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
applicationContext.xml:
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp" />
</bean>
<context:component-scan base-package="org.myfantasticwebsite"/>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/pizzashop"/>
<property name="username" value="root"/>
<property name="password" value=""/>
<property name="validationQuery" value="SELECT 1"/>
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="packagesToScan">
<array>
<value>org.myfantasticwebsite</value>
</array>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
I also have index.jsp that just displays some stuff from database. All 3 files (web.xml, applicationContext.xml and index.jsp) are located in WEB-INF directory, and everything works fine until I add ContextLoaderListener bean to web.xml. After, I get Page not found error on address localhost:8080/pizzashop.
This is just a simple tutorial I found on internet, that's why all configuration is in one file. I was actually trying make separate config files - 2 files stemming from web.xml (dispatcher-servlet.xml with web related config and applicationContext.xml with everything else). Further more to extract hibernate specific config to hibernate.cfg.xml, and spring+hibernate config to hibernate-context.xml.
Can someone please explain why I get 404 error, I'm stuck here for a week now. Thank you.
This is the only controller - PizzaController:
#Controller
#RequestMapping("/")
public class PizzaController {
#Autowired private PizzaDAO pizzaDAO;
/**
* This handler method is invoked when
* http://localhost:8080/pizzashop is requested.
* The method returns view name "index"
* which will be resolved into /WEB-INF/index.jsp.
* See src/main/webapp/WEB-INF/applicationContext.xml
*/
#RequestMapping(method = RequestMethod.GET)
public String list(Model model) {
List<Pizza> pizzas = pizzaDAO.findAll();
model.addAttribute("pizzas", pizzas);
return "index";
}
}
UPDATE: web.xml with ContextLoaderListener:
<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/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
I am beginner in Web Service and using
Spring 3.0 and spring-webmvc-portlet 3.0
javax.portlet 2.0
I have controller as follows
#Controller(value = "myController")
#RequestMapping(value = "**VIEW**")
public class MyController {
// Controller logic
}
Now, I want to create Web Service using RESTful API in portlet environment.
Please guide me How can i write the Web Service which will return JSON or XML data.
I am still struggling with Web Service not getting WS called.
I am pasting my conf files
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/itemCatalog-portlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>webServiceTest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webServiceTest</servlet-name>
<url-pattern>/myWebService/*</url-pattern>
</servlet-mapping>
item-portlet.xml
<aop:aspectj-autoproxy />
<context:component-scan base-package="com.main.mypackage" />
<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="propertyEditorRegistrars">
<list>
<ref bean="myPropertyEditorRegistrar" />
</list>
</property>
</bean>
</property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language-ext</value>
</list>
</property>
</bean>
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean name="jsonView"
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
<property name="prefixJson" value="false" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
webServiceTest-servlet.xml
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
portlet.xml
itemCatalog
org.springframework.web.portlet.DispatcherPortlet
text/html
view
content.Language-ext
Controller
#Controller
public class WebServiceTest {
#RequestMapping(value = "/myWebService/testing", method = RequestMethod.GET)
public String testMethod() {
return "HELLO WORLD ! SUCCESS";
}
}
I am trying to Hit with
localhost:8080:/myappname/myWebService/testing
Getting no result.
To create Web Service in portlet environment.
1. We need to use org. springframework.web.servlet.DispatcherServlet which is front controller for all the controllers available. All the HTTP request will be dispatched using Dispatcher servlet.
Add an entry in web.xml
<servlet>
<servlet-name>springwebservice</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springwebservice</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Please refer below link for dispatcher servlet read carefully
http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html
Now importantly each DispatcherServlet must have own WebApplicationContext. WebApplicationContext is nothing but an xml file comprises of controllers,view resolver,beans,etc .
Create file named springwebservice-servlet.xml in WEB-INF. springwebservice-servlet.xml is a WebApplicationContext.
Note
Upon initialization of a DispatcherServlet, Spring MVC looks for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and creates the beans defined there, overriding the definitions of any beans defined with the same name in the global scope.
Make sure new WebAppicationContext is created for DispatcherServlet and configure it according to need.
Please guide me if mistaken somewhere.
I've been having a little trouble with View Resolution under Spring 2.5.5. Basically I'm just trying to show my view with a message from the controller passed in. The issue comes when the Controller returns the ModelAndView but the DispatcherServelt says it can't find a Handler.
All the files seem to be in the correct place. I think the issue is that Spring can't resolve the view. From what I've seen I'm using the InternalResourceResolver correctly. I'm just at a loss as to why it is failing.
Once I've made a request this is whats in the catalina.out log:
Feb 8, 2010 3:27:24 PM com.madebymn.newsExample.web.IndexController handleRequest
INFO: Handling a Request: /index.jsp
Feb 8, 2010 3:27:24 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/newsExample/WEB-INF/jsp/index.jsp] in DispatcherServlet with name 'catchAll'
Here's my web.xml:
<web-app 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>A News Example</display-name>
<description>A News Example</description>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext-jdbc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>catchAll</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>catchAll</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
Here's the 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="indexController" class="com.madebymn.newsExample.web.IndexController" />
<bean name="authorController" class="com.madebymn.newsExample.web.AuthorController">
<constructor-arg>
<ref bean="authorService" />
</constructor-arg>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/index.jsp">indexController</prop>
<prop key="/author/*">authorController</prop>
</props>
</property>
</bean>
<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/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
And here's my IndexController Class:
public class IndexController implements org.springframework.web.servlet.mvc.Controller
{
protected final Log logger = LogFactory.getLog(getClass());
#Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
logger.info("Handling a Request: " + request.getServletPath());
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("message", "someMessage");
return modelAndView;
}
}
The problem is that you mapped your DispatcherServlet as *.jsp, when views are JSPs too. Try to map DispatcherServlet to something different, like *.html