Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC redirect to jsp for specific url

I am using Spring MVC for AngularJS project.

I am serving JSON from the url prefixed with "/rest/*" . All the jsp files are accessed directly and routing is handled using angular-js.

I need to do custom validations before jsp files are accessed. For rest urls (url prefixed with "/rest/*"), i already have filters in place.

How can i configure dispatcher-servlet such that all the jsp files are accessed once validation is done by spring controller.

web.xml

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.gauti" />
    <mvc:annotation-driven />
</beans>
like image 758
Gautam Kumar Avatar asked Nov 03 '15 08:11

Gautam Kumar


People also ask

How can I redirect a URL to another URL in JSP?

The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.

How do I redirect a spring boot to another URL?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

How do I redirect a page in spring?

Try a URL http://localhost:8080/HelloWeb/index and you should see the following result if everything is fine with your Spring Web Application. Click the "Redirect Page" button to submit the form and to get the final redirected page.


1 Answers

You can always map more than one url to your dispatcher servlet.

 <servlet-mapping>
     <servlet-name>dispatcher</servlet-name>
     <url-pattern>/rest/*</url-pattern>
     <url-pattern>/non-rest/*</url-pattern>
  </servlet-mapping>

 <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:spring-config/dispatcher-servlet.xml
            </param-value>
        </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>

For validations You can configure mvc interceptors for your different url patterns. for e.g.

<mvc:interceptors>
        <mvc:interceptor>
          <mvc:mapping path="/**"/>
          <bean id="localeChangeInterceptor"
           class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
           <property name="paramName" value="j_lang" />
          </bean>
        </mvc:interceptor>
</mvc:interceptors>

And configure a common controller for your angular resources :

@Controller
@RequestMapping("/ng")
public class AngularResourcesController {

    @RequestMapping(value = "/**", method = RequestMethod.GET)
    public ModelAndView process(HttpServletRequest request) {
        String pageName = null;
        //create your custom jsp URL, modify accordingly to your jsp location. 
        pageName =request.getRequestURL().toString();
        //forward to internal jsp.
        return new ModelAndView(pageName);
    }

}
like image 173
Amit Parashar Avatar answered Nov 15 '22 10:11

Amit Parashar