I decided to move my authorization functionality to another controller, however this caused some issues for me. Mainly, my callback controller cannot forward the GET request to the appropriate dispatcher servlet
DispatcherServlet with name 'Auth' processing GET request for [/user/username] (this should be 'Dashboard' dispatcher servlet)
My view layer is making use of spring, tiles 2, and thymeleaf integration. I have found this comment on stackoverflow, which shed some light on my situation. https://stackoverflow.com/a/13406096/2276284
"Secondly, when using Spring + Tiles, and returning some JSP in your tiles definition, it is treated as an internal forward request, and handled by the same servlet as the original request."
I found this stackoverflow comment as well, but it did not work for me. https://stackoverflow.com/a/17232532/2276284 Suggesting returning a new ModelAndView, or using the HttpServletResponse to redirect.
How can I successfully redirect to another controller/view?
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="pue" version="3.1">
<!-- webapp properties -->
<display-name>PUE</display-name>
<description>Personal User Engagement for Instagram</description>
<!-- session listener -->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- root context -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/app-context.xml</param-value>
</context-param>
<!-- spring security filter -->
<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>
<!-- custom redirect filter -->
<filter>
<filter-name>pueUsernameAuthFilter</filter-name>
<filter-class>abnd.pue.auth.service.impl.PUEUsernameAuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>pueUsernameAuthFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet config -->
<servlet>
<servlet-name>Auth</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/servlets/auth-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>IgOauth</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/servlets/igoauth-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Dashboard</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/servlets/dashboard-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- servlet mapping config -->
<servlet-mapping>
<servlet-name>Auth</servlet-name>
<url-pattern>/logout</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>IgOauth</servlet-name>
<url-pattern>/ig/oauth/callback</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Dashboard</servlet-name>
<url-pattern>/user/{username}</url-pattern>
<url-pattern>/user/{username}/profile</url-pattern>
<url-pattern>/user/{username}/assets</url-pattern>
</servlet-mapping>
<!-- mime types -->
<mime-mapping>
<extension>ico</extension>
<mime-type>image/x-icon</mime-type>
</mime-mapping>
</web-app>
spring-thymeleaf-tiles.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.xsd">
<!-- thymeleaf view resolvers with tiles integration -->
<bean id="tilesConfigurer" class="org.thymeleaf.extras.tiles2.spring4.web.configurer.ThymeleafTilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles/tiles.xml</value>
</list>
</property>
</bean>
<!-- template resolver -->
<bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/tiles/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="utf-8" />
<property name="cacheable" value="false" />
</bean>
<!-- template engine -->
<bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean id="tilesDialect" class="org.thymeleaf.extras.tiles2.dialect.TilesDialect" />
</set>
</property>
</bean>
<!-- tiles view resolver -->
<bean id="tilesViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="viewClass" value="org.thymeleaf.extras.tiles2.spring4.web.view.ThymeleafTilesView" />
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="utf-8" />
<property name="order" value="1" />
</bean>
</beans>
IgOauthController
package abnd.pue.ig.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import abnd.pue.ig.service.intf.IgOauthService;
#Controller
public class IgOauthController {
#Autowired
private IgOauthService igOauthService;
private static final Logger logger = LoggerFactory.getLogger(IgOauthController.class);
/**
*
* #param request
* #return String
* #throws IOException
*/
#RequestMapping(value="/ig/oauth/callback")
public ModelAndView igCallback(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
// get code parameter
String code = request.getParameter("code");
// attempt to get user name from callback
String username = igOauthService.processCallbackRequest(request, session, code);
// if username isn't empty redirect to user page
if (!username.isEmpty()) {
logger.info("username set: " + username);
return new ModelAndView ("redirect:/user/" + username);
}
logger.info("no username set");
// otherwise return to index TODO error display
return new ModelAndView("redirect:/");
}
}
Alright, thanks for the help guys. So, the issue was that I could not redirect my view away from the default DispatcherServlet. I was also using invalid url patterns in my web.xml, while trying to create dynamic urls.
The answer was restructuring the wiring of my servlet paths, and changing the default servlet from Auth, to Dashboard. I also implemented UrlRewriteFilter to redirect all traffic from root context ("/") to "/login"
Here are the affected changes.
web.xml
<!-- servlet mapping config -->
<servlet-mapping>
<servlet-name>Auth</servlet-name>
<url-pattern>/login</url-pattern>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>IgOauth</servlet-name>
<url-pattern>/ig/oauth/callback</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Dashboard</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
IgOauthController
#Controller
#RequestMapping(value = "/ig/oauth")
public class IgOauthController {
#Autowired
private IgOauthService igOauthService;
private static final Logger logger = LoggerFactory.getLogger(IgOauthController.class);
/**
*
* #param request
* #return String
* #throws IOException
*/
#RequestMapping(value="/callback")
public void igCallback(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
// get code parameter
String code = request.getParameter("code");
// attempt to get user name from callback
String username = igOauthService.processCallbackRequest(request, session, code);
// if username isn't empty redirect to user page
if (!username.isEmpty()) {
logger.info("username set: " + username);
response.sendRedirect("/" + username + "/dashboard");
return;
}
logger.info("no username set");
// otherwise return to index TODO error display
response.sendRedirect("/login");
return;
}
}
DashboardController
#Controller
#RequestMapping(value = "/{username}")
public class DashboardController {
#RequestMapping(value = "/dashboard", method=RequestMethod.GET)
public ModelAndView userDashboard() throws InstagramException {
...
}
#RequestMapping(value = "/profile", method=RequestMethod.GET)
public ModelAndView userProfile() throws InstagramException {
...
}
#RequestMapping(value = "/assets", method=RequestMethod.GET)
public ModelAndView userAssets() throws InstagramException {
...
}
}
Related
I've got error "WARN : org.springframework.web.servlet.PageNotFound - No mapping for GET /web/board/getBoardList"
Please help to find my mistakes....
It seems to be a problem with servlet-context.xml.
This is my package hierarchy.
enter image description here
BoardController.java
package com.jione.web.board.controller;
import javax.inject.Inject;
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.jione.web.board.service.BoardService;
#Controller
#RequestMapping(value = "/board")
public class BoardController {
#Inject
private BoardService boardService;
#RequestMapping(value = "/getBoardList", method = RequestMethod.GET)
public String getBoardList(Model model) throws Exception {
model.addAttribute("boardList", boardService.getBoardList());
return "board/index";
}
}
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 https://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>classpath:spring/*-context.xml</param-value>
<!-- <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>classpath:servlet-context.xml</param-value>
<!-- <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>
<!-- Character Set Filter -->
<filter>
<filter-name>encodingFilter</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-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context https://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.jione" />
</beans:beans>
Adding this as a solution, also. The path was wrong, it also included /web, but the controller didn't that part of the path mapped.
My application doesn't work with requestmapping, the request always works with the same method handleRequest, the controller is RestController.java and the URLs redirects to it when has the path /REST2/*, it works well but always with the same method. But the method "update" never works when I send the request to locahost:9080/myapp/REST2/rrr and if I change the URL i.e. localhost:9080/myapp/REST2/XXX also the request is going to handleRequest method.
The RequestMapping doesn't work well, I tried with other solutions but doesn't work..
Do you have any idea?
Restcontroller.java
public class RestController implements Controller{
private Configuration config;
private static String[] requestHeaders = {"accept", "pragma"};
private static String[] responseHeaders = {};
public void setConfig(Configuration config)
{
this.config = config;
}
#RequestMapping(value="/rrr") public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String asset_name = request.getParameter("assetid");
response.setContentType("text/plain"); // Set content type of the response so that jQuery knows what it can expect.
response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
String values = "Working"+ asset_name;
response.setContentLength(values.length());
PrintWriter out = response.getWriter();
out.println(values);
return null;
}
#RequestMapping(value ="/home")public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
Arrays.sort( requestHeaders );
Arrays.sort( responseHeaders );
String values;
String asset_name = request.getParameter("assetid");
...
response.setContentLength(values.length());
//
PrintWriter out = response.getWriter();
out.println(values);
return null;
}}
spring-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" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- View resolver. Prepends prefix /WEB-INF/jsp and suffix .jsp to view names. -->
<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>
<!-- Always use full path when mapping URLs to bean names. -->
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<!-- Home page. -->
<bean name="/home.app" class="com.fatwire.wem.sample.HomeController">
<property name="config" ref="config" />
</bean>
<!-- Installation page. -->
<bean name="/install.app" class="com.fatwire.wem.sample.InstallController">
<property name="config" ref="config" />
</bean>
<!-- Layout page. -->
<bean name="/layout.app" class="com.fatwire.wem.sample.LayoutController" />
<!-- REST proxy page. -->
<bean name="/REST/**" class="com.fatwire.wem.sample.ProxyController">
<property name="config" ref="config" />
</bean>
<!-- REST proxy page. -->
<bean name="/REST2/**" class="com.fatwire.wem.sample.RestController">
<property name="config" ref="config" />
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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" >
<!-- Bootstrap Spring configuration to be used by both SSO and MVC -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- WEM SSO Listener -->
<listener>
<listener-class>com.fatwire.wem.sso.SSOListener</listener-class>
</listener>
<!-- Configure Spring MVC -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.app</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/REST2/*</url-pattern>
</servlet-mapping>
<!-- WEM SOO Filter -->
<filter>
<filter-name>WEM SSO Filter</filter-name>
<filter-class>com.fatwire.wem.sso.SSOFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>WEM SSO Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Request AJAX
function load_form(data){
var asset_id = data;
$.post('REST2/home', {
assetid : data,
}, function(responseText) {
alert(responseText);
});
}
function update_form(){
var asset_id = "XXX";
$.post('REST2/rrr', {
assetid : asset_id,
}, function(responseText) {
alert(responseText)
});
Try to add #Controller and #RequestMapping annotations to RestController classlike this:
#Controller
#RequestMapping(value="/REST2")
public class RestController implements Controller{
And add method=RequestMethod.POST the #RequestMapping of methods like this:
#RequestMapping(value ="/home", method=RequestMethod.POST)
...
#RequestMapping(value ="/rrr", method=RequestMethod.POST)
I have spring project with web.xml configuration
<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>
Here is my servlet-context.xml
resources location="/, classpath:/META-INF/web-resources/" mapping="/resources/**" />
<default-servlet-handler/>
<context:component-scan base-package="pk.training.basitMahmood.web.controller" />
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jspx" />
</beans:bean>
Here is my controller
#RequestMapping("/contacts")
#Controller
public class ContactController {
final Logger logger = LoggerFactory.getLogger(ContactController.class);
#Autowired
private ContactService contactService;
#RequestMapping(method = RequestMethod.GET)
public String list(Model uiModel) {
logger.info("Listing contacts");
List<Contact> contacts = contactService.findAll();
uiModel.addAttribute("contacts", contacts);
logger.info("No. of contacts: " + contacts.size());
return "contacts/list";
}
} //end of class ContactController
Now when i select run on server then i get the following page
But when i change the url to http://localhost:9090/ch17_i18nSupport/contacts then i get the error that
I have list.jspx in my contacts folder. Why i am getting not found error?
Thanks
Although this was solved by Basit in the comments, I've also added it as an answer:
No adapter for handler indicates that the #RequestMapping methods in your controller aren't being picked up. Do you have a <mvc:annotation-driven /> tag in your servlet-context.xml?
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>
I recently migrated my codes from jboss 4.2.3 to jboss 7. There's something kind of weird and I can't figure out the reason. I used annotation #Controller at the top of my handler class, but it does not work any more. When I changed to use xml instead, it works fine. Any anybody give some hints?
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>asweb</display-name>
<filter>
<filter-name>springCharacterEncodingFilter</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>
</filter>
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>springCharacterEncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/ws/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>
com.opensymphony.module.sitemesh.filter.PageFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>simple</servlet-name>
<servlet-class>org.sonatype.mavenbook.web.SimpleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple.do</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>aswebmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>aswebmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/tld/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/tld/fmt.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
aswebmvc-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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="com.xxx.appstore.web.controller" />
<context:component-scan base-package="com.xxx.appstore.web.controller.admin" />
<bean id="webController" class="com.xxx.appstore.web.controller.WebController"/>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="local" />
</bean>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="synchronizeOnSession" value="true"/>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="order" value="1" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My Controller Class
package com.xxx.appstore.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class WebController
{
private static Log logger = LogFactory.getLog(WebController.class);
#RequestMapping("home.do")
public String home2(HttpServletRequest request, ModelMap model) {
return "";
}
#RequestMapping("/home.do")
public String home(HttpServletRequest request, ModelMap model) {
return "web/home";
}
}
I have updated the configuration files, which work for me for most annotations.
Spring 2 annotations have some incompatibilities with newer versions of JBoss; annotations in a Spring 2 app of mine were not being recognized in JBoss 5, and I needed to upgrade Spring to v3 for it to work. Here's a Spring JIRA on it. Notice that the fix version is in v3.0 RC1.
Not sure if its the same issue with JBoss 7, but it seems possible.
Yep - if you want to use spring mvc on jboss 7 you'll want to upgrade spring mvc to 3. One of the new features is better support for CDI. Once you make the upgrade you'll still need to do some legwork if you want to map ejb's directly into your servlet. You'll want to add something like this into your spring configs somewhere to wire the ejb:
<jee:local-slsb id="ejbReference" jndi-name="ejb/exampleEjb"
business-interface="example.ExampleEjb/>
As indicated in this userguide:
http://docs.redhat.com/docs/en-US/JBoss_Web_Framework_Kit/1.2/html/Spring_Developer_Guide/ch07s05s02.html#ejb-reference
You can then inject ejb's into the servlet like this:
#EJB(mappedName="java:global/earfile/jarfile/exampleEjb!com.app.ExampleEjb")
private ExampleEjb ejb;
You can find out the exact jndi reference for your ejb in the logs but this is essentially how to wire sprinc mvc with jboss' cdi support.