How to redirect to external website on 404 Tomcat - spring

I am using spring 3 + Tomcat 7
In web.xml I have defined
<error-page>
<error-code>404</error-code>
<location>/filenotfound</location>
</error-page>
But i want to redirect to external url say www.google.com instead of /filenotfound . How can I do that ?

I am not sure whether giving the direct url in location will work. But you can put a jsp there and redirect to the url from the jsp.
<error-page>
<error-code>404</error-code>
<location>/filenotfound.jsp</location>
</error-page>
filenotfound.jsp
<%# page language="java" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:redirect url="http://hotmail.com" />

Related

Simple Spring Web Project Not Working

I've been trying to learn how to develop a Spring Web application and can't seem to get it working. Using the Spring Tool Suite, I created a new Spring Project using the Simple Spring Web Maven template, which basically is comprised of four files:
index.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<c:url value="/showMessage.html" var="messageUrl" />
Click to enter
</body>
</html>
/WEB-INF/web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
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>spring-web-test</display-name>
<!--
- Location of the XML file that defines the root application context.
- Applied by ContextLoaderListener.
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--
- Servlet that dispatches request to registered handlers (Controller implementations).
-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/mvc-config.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Uncomment and your base-package here:
<context:component-scan
base-package="org.springframework.samples.web"/> -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' -->
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
/WEB-INF/view/showMessage.jsp:
<!DOCTYPE html>
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta charset="utf-8">
<title>Welcome</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
This all seemed simple and basic enough, so I built it using Maven and packaged it into a WAR file. I then deployed it to a local TomEE 7 server, went to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT, and got to the very simple index.jsp page:
However, when clicking the link, which sends me to http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage.html, I get a 404 error from Tomcat:
I suspected it was because of the .html ending - based off what I understand of the dispatcher and view resolver, it would be looking for /WEB-INF/view/showMessage.html.jsp. However, trying to access http://localhost:8080/spring-web-test-0.0.1-SNAPSHOT/showMessage returns the same error. Looking at the Tomcat logs, I see
Nov 11, 2015 9:46:53 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage.html] in DispatcherServlet with name 'dispatcherServlet'
Nov 11, 2015 9:46:55 AM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/spring-web-test-0.0.1-SNAPSHOT/showMessage] in DispatcherServlet with name 'dispatcherServlet'
What am I doing wrong? The whole thing seems pretty simple, and I haven't even modified anything from the template Spring Tool Suite provides, so I'd have assumed it would work right out of the box. Unfortunately there doesn't seem to be a whole lot of resources online on developing Spring web applications for use in servlet containers, and of the ones I've found, nothing seemed to suggest I'm doing anything wrong.
Thanks.
Change your index.jsp to something like this
<c:url value="/showMessage" var="messageUrl" />
You don't need file extensions, because you've set them up in your mvc-config.xml.
Then you need a basic controller to handle the mapping. Something like this:
#Controller
public class WebController{
#RequestMapping(value = "/")
public String showMessage(){
return "index";
}
#RequestMapping(value = "/showMessage")
public String showMessage(){
return "showMessage";
}
}

ATG<dsp:setvalue> Trouble with setting values using JSTL

Got some trouble with using jstl in atg jsp page. Added
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %> and some library at /WEB-INF/lib (standard.jar, jstl.jar).
My web.xml
<web-app 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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
So <dsp:getvalueof param="id" var="prodId"/>
<c:set var="link" value="genericproduct.jsp?id=${prodId}"/> works fine and shows for example "genericproduct.jsp?id=prod180007",but ${prodId} shows nothing,at the moment <c:out value="${prodId}"/> works. And <dsp:setvalue bean="CustomFormHandler.errorURL" value="genericproduct.jsp?id=${prodId}"/> doesn't works - "genericproduct.jsp?id=${prodId}". Version of JSTL 1.0, application runs on jboss 4.0.5GA.I look forward to your reply.Thanks!
<%# page isELIgnored="false" %>
<%#taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>

FKeditor and Spring MVC Maven integration step by step

I want to use Fckeditor in my spring mvc. Could you provide instruction or link to set up step by step?
I did below steps but when I run, 404 not found [/springmvc/fckeditor/editor/fckeditor.html ]is showing.
Could you check me what I am wrong and guide me?
Firstly, as per documentation I added below code in pom.xml.
<dependency>
<groupId>net.fckeditor</groupId>
<artifactId>java-core</artifactId>
<version>2.6</version>
</dependency>
And in web.xml file, I added below code.
<servlet>
<servlet-name>ConnectorServlet</servlet-name>
<servlet-class>
net.fckeditor.connector.ConnectorServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ConnectorServlet</servlet-name>
<url-pattern>
/resources/fckeditor/editor/filemanager/connectors/*
</url-pattern>
</servlet-mapping>
After that I download FCKeditor_2.6.8.zip file and extract in the src/main/resources folder.
In the jsp page, I added taglib, javascript and tag.
<%# taglib uri="http://java.fckeditor.net" prefix="FCK" %>
function FCKeditor_OnComplete(editorInstance) {
window.status = editorInstance.Description;
}
<FCK:editor instanceName="EditorDefault">
<jsp:attribute name="value">This is some <strong>sample text
</strong>. You are using <a href="http://www.fckeditor.net">
FCKeditor</a>.
</jsp:attribute>
</FCK:editor>
But Fckeditor is still not working.
Could you guide me which is wrong?
Thank you,
i have used ckeditor, below are configrations
<dependency>
<groupId>com.ckeditor</groupId>
<artifactId>ckeditor-java-core</artifactId>
<version>3.5.3</version>
</dependency>
in jsp file
<%# taglib prefix="ckeditor" uri="http://ckeditor.com"%>
<script type="text/javascript" src="%=request.getContextPath()%>/common/script/ckeditor/ckeditor.js"></script>
<form:textarea path="body" maxlength="5000" /> <ckeditor:replace replace="body" basePath="../ckeditor/" />
make sure you copy js files also and copy it and specify it in jsp

Spring security JSP tags - which jars do I need?

I have a project that used to run successfully on Spring 3.0 with Spring-MVC. I'm moving it to Spring 3.1 and at the same time changing my jar management to Maven rather than manual download/copy to project/lib for Ant.
During loading the first (login) public page in the logs I see:
INFO: At least one JAR was scanned for TLDs yet contained no TLDs
Which makes me think one my taglib URLs isn't being matched to an appropriate taglib jar. So either the URL is incorrect or I don't have the appropriate jars.
In the top of my JSP:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
I think it is the security tag that is having issues, as on the following page (after login) I get a new error:
javax.servlet.ServletException: javax.servlet.jsp.JspException: java.io.IOException: No visible WebSecurityExpressionHandler instance could be found in the application context. There must be at least one in order to support expressions in JSP 'authorize' tags.
This is new to me - I hadn't had to define a 'WebSecurityExpressionHandler' when I was running the app with Spring 3.0 libraries, so either I'm missing a jar that provides the relevant stuff or something has changed in Spring 3.0 -> 3.1 in how I should be configuring the security of my application.
Edit:
I've found what looks like it should fulfil that role in my config:
`<bean id="expressionHandler" class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler" />`
And
<http auto-config="true" use-expressions="true" .... > in my security-context.xml
You should have at least the following ones:
spring-security-config
spring-security-web
spring-security-taglibs
additionally you can add spring-security-core but Maven should resolve it as a transitive dependency.

Internationalization sitemesh

I'm using freemarker, SiteMesh and Spring framework.
For the pages I use ${requestContext.getMessage()} to get the message from message.properties. But for the decorators this doesn't work. How should I do to get the internationalization working for sitemesh?
You have to use the fmt taglib.
First, add the taglib for sitemesh and fmt on the fisrt line of the decorator.
<%# taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator"%>
<%# taglib prefix="page" uri="http://www.opensymphony.com/sitemesh/page"%>
<%# taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%# taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt"%>
<fmt:setBundle basename="messages" />
In my example, the i18n file is messages.properties. Then you need to use the fmt tag to use the mesages.
<fmt:message key="key_of_message" />
If you prefer templates and the freemarker servlet instead you can enter the following in your templates:
<#assign fmt=JspTaglibs["http://java.sun.com/jstl/fmt"]>
<#fmt.message key="webapp.name" />
and in your web.xml:
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>messages</param-value>
</context-param>

Resources