Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Tuesday, June 16, 2015

AngularJS, Spring MVC, Tuckey URL Rewrite

I wanted to use html5Mode with an existing Spring MVC backend. This assumes that the front-end code will be deployed as part of the WAR file.
I did the following steps:

1. In Angular, I added the following html5Mode config:

$locationProvider.html5Mode(true).hashPrefix('!');

2. In Spring MVC, I added the Tuckey URL Rewrite Module in the Maven pom.xml:

<dependency>
<groupId>org.tuckey</groupId>
<artifactId>urlrewritefilter</artifactId>
<version>3.0.4</version>
</dependency>


3. Do a Maven Update 4. Open the web.xml and enter the following within the web-app tag (I left the debug line there)

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>DEBUG</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>


5. Create a urlrewrite.xml under the WEB-INF folder of the your Spring project and the contents should look like the following:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">

<!--

Configuration file for UrlRewriteFilter
http://www.tuckey.org/urlrewrite/

-->
<urlrewrite>

<rule>
<from>^/mypage$</from>
<to>/index.html</to>
</rule>

</urlrewrite>


And that is it! I had trouble before cause I was using 'index.html' in the 'to' tag instead of '/index.html'

Tuesday, July 15, 2014

[Java/Spring/Maven] Making JSTL work!!!!

Here are some settings to check to make sure JSTL works in your project. I'm using JSTL 1.1.2 and

Pom.xml dependencies ( Just dumping all dependencies including non-jstl related ones )

 <dependencies>
   <!-- Spring dependencies -->
 <dependency>
  <groupId>org.springframework</groupId>
 <artifactId>spring-aop</artifactId>
 <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context-support</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>${spring.maven.artifact.version}</version>
 </dependency>
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.maven.artifact.version}</version> 
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>       
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>      
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency>               
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.maven.artifact.version}</version>
    </dependency> 
   <!-- JSP, Servlet, JSTL dependencies -->
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>${version.javax.servlet}</version>
 </dependency>
 <dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>${version.javax.servlet.jsp}</version>
 </dependency>
 
 <dependency>
            <groupId>xmlbeans</groupId>
            <artifactId>xbean</artifactId>
            <version>2.2.0</version>
        </dependency>
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>jstl</artifactId>
  <version>${version.javax.servlet.jstl}</version>
 </dependency>
 <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
      <scope>compile</scope>
    </dependency>
   <!--  Codehaus Jackson dependencies -->
   <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-core-asl</artifactId> 
       <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-jaxrs</artifactId> 
        <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId> 
        <artifactId>jackson-mapper-asl</artifactId> 
        <version>1.9.1</version>
    </dependency>    
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-lgpl</artifactId>
        <version>1.9.1</version>
    </dependency>
    <!-- Jettison -->
    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- Apache Datasource -->
    <dependency>
  <groupId>commons-dbcp</groupId>
  <artifactId>commons-dbcp</artifactId>
  <version>1.2.2</version>
 </dependency>
    <!--  Oracle Driver -->
     <dependency> 
         <groupId>com.oracle</groupId>
         <artifactId>ojdbc14</artifactId>
         <version>10.2.0.4.0</version>
     </dependency>
    
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>myspringproject</finalName>
  </build>
  <properties>
   <spring.maven.artifact.version>2.5.6</spring.maven.artifact.version>
   <version.javax.servlet>2.4</version.javax.servlet>
   <version.javax.servlet.jstl>1.1.2</version.javax.servlet.jstl>
   <version.javax.servlet.jsp>2.1</version.javax.servlet.jsp>
  </properties>

JSP taglibs/charset (utf-8)


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Postman</title>
</head>
<body>

web.xml
The web-app attribute to configure the version might start giving you an error saying that the Dynamic Web Module cannot convert to <some version>. If you encounter this, right-click your project in the Project Explorer, go to the Project Facets, uncheck the Dynamic Web Module, Hit OK, and Update Maven Project. If you have the code below, it will set it as the assign version I believe.


<?xml version="1.0" encoding="UTF-8"?>
<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">