I would like to add some characters with accents in my ".jsp" and ".html" but when I write those characters with accent in those files and load my pages:
"è" becomes "é"
"ç" becomes "ç"
"é" becomes "Ã "
Steps:
I have configured Eclipse for creating all files with UTF-8 encoding (I have checked: all .jsp and .html files in the /src or /target folder are now UTF-8).
None of my .jsp or .html files (inside /src or /target folder) explicitely contains those "é", "ç", "à ".
All my .jsp contains those lines at the beginning :
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<!-- ... -->
</head>
I have added those parameters in my appengine-web.xml:
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
<!-- Added below -->
<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />
</system-properties>
I have also tried to add this thing showed in the GAE documentation but my application throws Exception :
<env-variables>
<env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>
GAE documentation says (source) :
To avoid conflicts with your local environment, the development server does not set environment variables based on this file, and requires that the local environment have these variables already set to matching values. (This does not apply to system properties.)
So I have removed this part and added the following environment variable in my system: DEFAULT_ENCODING = UTF-8
Further more:
When I use firebug to see the network, it shows Content-Type text/html; charset=utf-8 for my loaded page.
Google's tool has already added the <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> in my pom.xml when I've generated my project.
Interesting fact:
When I load html code from my text files with AJAX, there is no problem, "é" or "ç" characters are ok. I use the BalusC's code ( and you can notice that he uses response.setCharacterEncoding("UTF-8"); so I don't know where the problem comes from).
On the development environment (my localhost), if I replace <%# page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %> by this <%# page pageEncoding="UTF-8" %> characters with accents are ok, but only on my localhost, then when I upload my project to Google App Engine servers it's still not ok, firebug doesn't show Content-Type text/html; charset=utf-8 anymore, and W3C Markup Validation Service tells me :
Internal encoding declaration utf-8 disagrees with the actual encoding of the document (windows-1252)
My configuration:
Maven 3.2
IDE: Eclipse Luna
OS: Windows 7
I ran into the same issue. My solution was to add at the top of my .jsp files that generate html the following tag:
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Related
I've been learning about connection pools, and I have gotten one working by using Tomcat's PoolProperties. Now I would like to set one up using a context.xml file with Tomcat and IntelliJ, but I can't get this to work. How is this done?
A new project with a 4.0 Web Application framework can automatically create a web/WEB-INF directory with a web.xml file, but the web/META-INF directory containing a context.xml file is not created. If I do create web/META-INF/context.xml myself through the Tool Window > Project, and I use this to set up a connection pool as shown below, the file seems to be ignored.
This is my guess as to what might be happening, though I'm sure this is filled with errors: A WAR file is a packaged directory that is sent to Tomcat in an arrangement that Tomcat understands. web/WEB-INF/web.xml is a required file that is needed for a variety of reasons, one of which is to set up servlets. web/META-INF/context.xml is optional, and thus by default, IntelliJ does not create it. When a web/META-INF/context.xml is created manually, it must also be included into the WAR file otherwise IntelliJ will not send it to Tomcat.
Even if the previous paragraph is true though, I still have not gotten Tomcat to use context.xml after trying to add it to the WAR. Maybe I didn't add it correctly.
context.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Context>
<Resource name="jdbc/sql_connection_03" auth="Container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/follow_db_01"
username="root" password="pass"
maxactive="100" maxIdle="30" maxWait="10000"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="java.sql.DataSource"/>
</Context>
index.jsp
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<sql:query var="rs" dataSource="jdbc/sql_connection_03">
select ID, Description, PackageSize from products;
</sql:query>
<html>
<head>
<title>Index</title>
</head>
<body>
<c:forEach var="row" items="${rs.rows}">
${row.ID}<br>
${row.Description}<br>
${row.PackageSize}<br>
</c:forEach>
</body>
</html>
I had the same issue and adding context lookup to my java code to use the resource defined in context.xml did the job.
It's the accepted solution from: Tomcat and JDBC connection pooling
Still works with Tomcat 9.0.39
I'm very new to Spring mvc,for this question i've found many answers here but nothing is working for me and please help me to find out what mistake i'm doing here.
I have created a css file under WebContent>WEB-INF>resources>css>style.css and mapping is done under spring-servelet.xml and the code is
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.journaldev.spring" />
<!-- 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>
<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="resources/**" location="resources/" cache-period="31556926"/>
</beans:beans>
and i've called the css file from jsp and the jsp file is
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcome</title>
<!-- Custom Theme files -->
<link href="<spring:url value='resources/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<h1>Hello world!</h1>
<P>The time on the server is ${serverTime}.</p>
</body>
</html>
Now network is aborted error is displaying at console .As i told i'm newbie to this framework so please help me out other than giving a link to an answer because i've tried almost all.Thank you
Put your CSS folder inside webcontent and add below code servlet xml file
<mvc:resources mapping="/css/**" location="/css/" />
Now you will be able to take the CSS from JSP inside views folder using below code -
<link href="/css/yourCSS.css" rel="stylesheet" type="text/css" media="all" />
This should work otherwise let usknow!
So, you will need to do what vipin cp is suggesting. You need this:
<link href="<spring:url value='/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
This is because the you are using the mapping mapping="/css/**" , so your value needs to begin with the same thing as your mapping "/css/...", without the beginning slash, it doesn't match your mapping so spring doesn't know where to map it too, you could also change your mapping to mapping="css/**". Which ever you choose, they just need to match each other.
Once they match Spring can then resolve your urls to what they need to be.
Also, just a suggestion for readability. Specify all your spring:url elements at the top with a var argument and then you can reference that with standard SpEl.
And example is:
<spring:url value='/css/style.css' var='styleCss'/>
So, that tells Spring that the styleCss variable references the resolved url from /css/style.css. You can then reference that variable like:
<link href="${styleCss}" rel="stylesheet" type="text/css" media="all" />
Obviously, up to you, but I have always found it more readable.
For what it is worth, this can also be done with your JS imports if you have any.
This is my dispacter-servlet.xml file. When i deploy the project on resin pro 4.0.36 it loads my index page and the content BUT FAILS to load css files and images stored under the staticresources folder
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.dogears" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<import resource="classpath:beans.xml"/>
<mvc:resources mapping="/resources/**" location="/staticresources/" />
<mvc:annotation-driven />
Please can anyone tell me how to mapp my static resources folder, so that whenever the request is of the patter /resources/* it redirects the request to the static resources folder. the staticresources folder is in MyspringProject/src/main/webapps directory.
I believe you need to map your staticresources directory as a root directory. In Eclipse that would be a "Source Folder" and in IntelliJ a "Resources Root".
Refer to the following help docs depending on your IDE:
Eclipse: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-wizard-source-folder.htm
IntelliJ: https://www.jetbrains.com/webstorm/help/configuring-folders-within-a-content-root.html
Suppose your project directory structure is like
- src
- main
- webapp
- resources
- css
- abc.css
- images
- xyz.jpg
& your .html or .jsp pages are located in the directory as below
- webapp
- index.jsp
- pages
- welcome.jsp
then you can add link to your resources in index.jsp page with the URL BaseURL/index.jsp
<link href="resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
& to welcome.jsp with URL BaseURL/pages/welcome.jsp as
<link href="../resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="../resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
Hope this helps you.
I finally found my solution. I had deployed my spring webapp in the webapps folder of the resin directory.
I realised that <mvc:resource /> mapping tag does not work when you have deployed your spring app on resin server instead of tomcat.
Hence i solved this by first creating a war file of my project and then extracted the war file on the desktop and later placed all the contents from the war file in the webapps/root (and not webapps folder) folder of resin directory. and then from my index page i used JSTL TAG to include the external stylesheet.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<c:url value="/staticresources/externalcss.css"/>">
</head>
<body>
<h2 class="text_style">Hello World!</h2>
</body>
</html>
IT WORKS !!!
I am new to spring and am trying it.
I have a jsp file which includes css and images as follows
<link href="css/style.css" rel="stylesheet" type="text/css" />
Now i have a url path as /localhost/myapp/test/tst which is mapped to a controller which then forwards it to view jsp
view.jsp is present it
/web-inf/jsp/view.jsp
But when hit the path /localhost/myapp/test/tst the css and images path gets resolved to
/localhost/myapp/test/tst/css/style.css
Actaully i want it to come as /localhost/myapp/css/style.css
css and html are present in root of webapp
How i have following in my spring config
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />
I have what should be an easy issue to solve, but I'm having no luck.
In my servlet-servlet.xml file, I have the following beans (in addition to others):
<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>
<context:component-scan base-package="com.servlet.web" />
My test controller looks like this:
package com.servlet.web;
import java.util.Map;
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 TestController
{
protected final Log log = LogFactory.getLog(getClass());
#RequestMapping("/test")
public String methodName(Map<String, Object> map) {
map.put("someMessage", "some string here");
return "test";
}
}
My jsp view looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
So, when I view the jsp, I'd expect the value of someMessage (some string here), but I only get the following:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>servlet.com</title>
</head>
<body>
${someMessage}
</body>
</html>
When I crank up the logging, I see that my someMessage object is being place in the model:
22:21:17,425 DEBUG DispatcherServlet:852 - DispatcherServlet with name 'servlet' determining Last-Modified value for [/servlet/access/test]
22:21:17,426 DEBUG DefaultAnnotationHandlerMapping:183 - Mapping [/test] to handler 'com.servlet.web.TestController#762fef'
22:21:17,426 DEBUG DispatcherServlet:868 - Last-Modified value for [/servlet/access/test] is: -1
22:21:17,426 DEBUG DispatcherServlet:700 - DispatcherServlet with name 'servlet' processing GET request for [/servlet/access/test]
22:21:17,427 DEBUG HandlerMethodInvoker:158 - Invoking request handler method: public java.lang.String com.servlet.web.TestController.methodName(java.util.Map)
22:21:17,427 DEBUG DispatcherServlet:1070 - Rendering view [org.springframework.web.servlet.view.JstlView: name 'test'; URL [/WEB-INF/jsp/test.jsp]] in DispatcherServlet with name 'servlet'
22:21:17,427 DEBUG JstlView:328 - Added model object 'someMessage' of type [java.lang.String] to request in view with name 'test'
22:21:17,428 DEBUG JstlView:237 - Forwarding to resource [/WEB-INF/jsp/test.jsp] in InternalResourceView 'test'
22:21:17,429 DEBUG DispatcherServlet:666 - Successfully completed request
Obviously, my view is mapped correctly, but I can't seem to access model objects added to the request in the view. I've done this type of thing with Spring MVC many times in the past, but I must be missing something obvious here. Any ideas? Thanks.
Are you sure that evaluation of EL is enabled in your JSP? I sometimes had the problem, that it got turned of somehow. Try evaluating a simple expression like ${'test'} and see if 'test' appears.
You can also try enabling it with page directives or something else if EL should be disabled.
<%# page isScriptingEnabled="true" isELIgnored="false" %> //of course it has to be FALSE
(Sorry, I can't remember if this 100% correct. It might be 'isELEnabled')
I ran into the same problem, and after comparing 2 similar apps (one with EL working fine and the other not), noticed that the problem on my tomcat 7 depended on the webapp version specified in the web.xml of the application.
The same jsp using Web App 2.3 displays ${someMessage}. (BTW, this is what you get using maven archetype:generate with archetypeArtifactId=maven-archetype-webapp).
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
...
The same jsp using Web App 2.4 displays the model object properly:
<?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">
...
Hope that it helps!
I haven't used quite as much annotation configuration as you have with Spring MVC, so I'm not sure of all the things that are being automatically done with your setup. My only thought is this: should the method parameter be a ModelMap object? The examples I've seen before have all used ModelMap as the parameter type. Section 13.11.3 of this page is one of them: http://static.springsource.org/spring/docs/2.5.6/reference/mvc.html.
Like I said, I haven't used this type of auto-configuration before -- I do it slightly more manually, and extend my controllers from something like an AbstractController or a SimpleFormController.