Localized Form Submission using Ajax - ajax

I am using springs framework with Tomcat 7 & MySQL. I am using UTF-8 encoding to submit form data for Local Language. This works fine using post through a JSP. However, when using Ajax Based Form submission it shows junk characters as request parameters in server.
Please let me know if I am missing anything..
** My Ajax Code**
xmlHttp.open("POST", "viewTeluguScript.htm", true);
//xmlHttp.setRequestHeader("Content-type", "multipart/form-data;charset=UTF-8");
xmlHttp.setRequestHeader("Content-length", parameters.length);
// xmlHttp.setRequestHeader("Content-Type", "text/html;charset=UTF-8");
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=UTF-8");
//xmlHttp.setRequestHeader("pageEncoding","UTF-8");
//xmlHttp.setRequestHeader("accept-charset","UTF-8");
xmlHttp.setRequestHeader("content-language","tel");
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(parameters);
**My Controller code**
public ModelAndView viewTeluguScript(HttpServletRequest request, HttpServletResponse response) throws SQLException, CannotGetJdbcConnectionException, Exception {
//textInfo = java.net.URLDecoder.decode(request.getParameter("teluguText"), "UTF-8");
textInfo = request.getParameter("teluguText");
/* StringBuffer jb = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
jb.append(line);
} catch (Exception e) {
}
System.out.println("request is :::: "+jb.toString());*/
ModelAndView mv = new ModelAndView("viewQuestions");
//request.getSession().setAttribute("workingModule", "questions");
return mv;
}
enter code here
**My 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 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-security.xml
classpath:applicationContext-common-business.xml
classpath:applicationContext-common-authorization.xml
</param-value>
</context-param>
<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>
<locale-encoding-mapping-list>
<locale-encoding-mapping>
<locale>en</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
<locale-encoding-mapping>
<locale>tel</locale>
<encoding>UTF-8</encoding>
</locale-encoding-mapping>
</locale-encoding-mapping-list>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>g2l</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--<load-on-startup>2</load-on-startup>-->
</servlet>
<servlet-mapping>
<servlet-name>g2l</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!-- <session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>-->
</web-app>

try adding a content-encoding http header
your browser uses Unicode to represent data to display. Resources coming from a web server can optionally have encoding information attached in http header.
If there is a content-encoding header, the browser converts the resource to the internal unicode representation using encoding from the response header. If there is no content-encoding header, the browser assumes, that the file encoding is the same as the page which requested the resource.

Related

p:fileUpload doesn't work using #Autowired bean annotation

I recived error when try upload file using p:fileUpload.
I tried maven clean and maven install for update dependeces.
I changed web.xml many times. I belive the problem is relationship with my anotation, i m using #Autowired (Spring) but i change it an donsen't work.
How i resolve this problem?
I m using:
Primefaces 6.0,
Java 8,
Spring Data,
Spring MVC
my web xml:
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<url-pattern>*.jsf</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
JSF:
<p:fileUpload id="fileDescAto" auto="true" multiple="false"
invalidFileMessage="Tipo de Arquivo Inválido:"
disabled="#{not empty searchDocumentoBean.descAtoArquivoFileName}"
update="messagesDialog #this panelGroupFileDescription"
allowTypes="/(\.|\/)(pdf)$/" sizeLimit="10485760"
invalidSizeMessage="Tamanho de arquivo excedido (10MB):"
fileUploadListener="#{searchDocumentoBean.adicionaArquivoUpload}"
label="Escolher Arquivo..." mode="advanced" />
Bean:
public void adicionaArquivoUpload(FileUploadEvent ev) {
String[] name = ev.getFile().getFileName().split("\\\\");
descAtoArquivo = ev.getFile().getContents();
descAtoArquivoFileName = name[name.length-1];
}
public void removeArquivoUpload() {
descAtoArquivo = null;
descAtoArquivoFileName = null;
}// GET AND SET OMITED
Head annotation My bean:
#Component
#Scope("view")
#ManagedBean(name = "searchDocumentoBean")
#URLMappings(mappings = {
#URLMapping(id ="searchDocumento", pattern = "/searchDocumento", viewId = "/resources/pages/documento/searchDocumento.jsf")
})
ERROR CONSOLE WHEN I TRY USE FILE UPLOAD
javax.faces.event.AbortProcessingException error obtained during the processing of RENDER_RESPONSE 6: UIComponent-ClientId
08:43:55,231 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-/0.0.0.0:8080-2) Error Rendering View[/resources/pages/documento/searchDocumento.xhtml]: javax.faces.event.AbortProcessingException: java.lang.NullPointerException
at org.primefaces.context.PrimePartialResponseWriter.startMetadataIfNecessary(PrimePartialResponseWriter.java:303) [primefaces-6.0.jar:6.0]
at org.primefaces.context.PrimePartialResponseWriter.startUpdate(PrimePartialResponseWriter.java:142) [primefaces-6.0.jar:6.0]
Caused by: java.lang.NullPointerException
at org.primefaces.util.ResourceUtils.filterStylesheets(ResourceUtils.java:96) [primefaces-6.0.jar:6.0]
at org.primefaces.context.PrimePartialResponseWriter.startMetadataIfNecessary(PrimePartialResponseWriter.java:287) [primefaces-6.0.jar:6.0]
... 49 more
08:43:55,246 SEVERE [javax.enterprise.resource.webcontainer.jsf.context] (http-/0.0.0.0:8080-2) JSF1073: javax.faces.event.AbortProcessingException obtido durante o processamento de RENDER_RESPONSE 6: UIComponent-ClientId=, Message=java.lang.NullPointerException
08:43:55,240 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (http-/0.0.0.0:8080-5) Error Rendering View[/resources/pages/documento/searchDocumento.xhtml]: javax.faces.event.AbortProcessingException: java.lang.NullPointerException
UPDATE 27/09/2017
I recive error:
JBWEB000281: Servlet mapping specifies an unknown Servlet name Faces Servlet´
Where an error in my web.xml?
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>openEntityManager</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
This solve my problem:
Listener method in p:fileUpload is never invoked in primefaces
Besides That.
i need especify location the Faces Servlet:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
javax.faces.webapp.FacesServlet
I needed especify resource primefaces depends for fileupload works fine>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
In some versions of primefaces it is not necessary to specify this configuration. I am currently using version 6.0, which believed that this specification was not necessary in web.xml because it is a newer version, but it was necessary.

Spring MVC - ContextLoaderListener loaded twice

So my head really hurts - I have read about Role/Purpose of ContextLoaderListener in Spring and I cannot go one step further.
I have a really old project and I'm upgrading it. So far I've managed to bring it from spring 2.x to spring 4.x and from Java 7 to Java 8, but I stuck on running application on tomcat 8. It loades ContextLoaderListener twice!? On tomcat 7 starts without problem.
Tomcat 7: apache-tomcat-7.0.29
Tomcat 8: apache-tomcat-8.5.11
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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 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">
<display-name>VERSION-1.7.1</display-name>
<context-param>
<param-name>listenPort_http</param-name>
<param-value>80</param-value>
</context-param>
<context-param>
<param-name>listenPort_https</param-name>
<param-value>8443</param-value>
</context-param>
<context-param>
<param-name>daoType</param-name>
<param-value>ibatis</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>ApplicationResources</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.fallbackLocale</param-name>
<param-value>en</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:META-INF/applicationContext-*.xml
/WEB-INF/applicationContext-*.xml
</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>
/WEB-INF/faces-specification-config.xml,
/WEB-INF/faces-operating-config.xml,
/WEB-INF/faces-popup-config.xml,
/WEB-INF/faces-mabr-config.xml
</param-value>
<description></description>
</context-param>
<filter>
<filter-name>clickstreamFilter</filter-name>
<filter-class>com.opensymphony.clickstream.ClickstreamFilter</filter-class>
</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>
<filter-name>rewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
</filter>
<filter>
<filter-name>exportFilter</filter-name>
<filter-class>org.displaytag.filter.ResponseOverrideFilter</filter-class>
</filter>
<filter>
<filter-name>uploadExtensionsFilter</filter-name>
<filter-class>
org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>10m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>
<filter>
<filter-name>compressionFilter</filter-name>
<display-name>Compression Filter</display-name>
<filter-class>org.opendls.webapp.filter.GZIPFilter</filter-class>
</filter>
<filter>
<filter-name>mySecurityFilter</filter-name>
<display-name>Security Filter</display-name>
<filter-class>org.opendls.webapp.filter.MySecurityFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>mySecurityFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>mySecurityFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>clickstreamFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>exportFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>compressionFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>uploadExtensionsFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.opensymphony.clickstream.ClickstreamListener</listener-class>
</listener>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<listener>
<listener-class>org.opendls.webapp.listener.StartupListener</listener-class>
</listener>
<listener>
<listener-class>org.opendls.webapp.listener.SessionListener</listener-class>
</listener>
<listener>
<listener-class>org.opendls.webapp.listener.ShutdownListener</listener-class>
</listener>
<listener>
<listener-class>org.opendls.webapp.listener.UserCounterListener</listener-class>
</listener>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>wsit-jaxws-fromjava</servlet-name>
<display-name>wsit-jaxws-fromjava</display-name>
<description>JAX-WS endpoint - fromjava</description>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>wsit-jaxws-fromjava</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>1440</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>400</error-code>
<location>/index.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/403.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/viewExpired.jsp</location>
</error-page>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/primary</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/secondary</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
StartupListener.java
public class StartupListener extends ContextLoaderListener
implements ServletContextListener {
private static final Log log = LogFactory.getLog(StartupListener.class);
public void contextInitialized(ServletContextEvent event) {
if (log.isDebugEnabled()) {
log.debug("initializing context...");
}
super.contextInitialized(event);
ServletContext context = event.getServletContext();
String daoType = context.getInitParameter(Constants.DAO_TYPE);
// if daoType is not specified, use DAO as default
if (daoType == null) {
log.warn("No 'daoType' context carameter, using hibernate");
daoType = Constants.DAO_TYPE_HIBERNATE;
}
// Orion starts Servlets before Listeners, so check if the config
// object already exists
Map config = (HashMap) context.getAttribute(Constants.CONFIG);
if (config == null) {
config = new HashMap();
}
// Create a config object to hold all the app config values
config.put(Constants.DAO_TYPE, daoType);
context.setAttribute(Constants.CONFIG, config);
// output the retrieved values for the Init and Context Parameters
if (log.isDebugEnabled()) {
log.debug("daoType: " + daoType);
log.debug("populating ...");
}
setupContext(context);
}
public static void setupContext(ServletContext context) {
ApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
RightsManager rightsManager = (RightsManager) appContext.getBean("rightsManager");
context.setAttribute(Constants.AVAILABLE_ROLES, rightsManager.getAllRights());
if (log.isDebugEnabled()) {
log.debug("initialization complete [OK]");
}
}
}
I have tried many things on web.xml and StartupListener.java but I couldn't manage to load ContextLoaderListener only once. I am getting the same error over and over again:
Error:
27-Jan-2017 02:25:29.217 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log No Spring WebApplicationInitializer types detected on classpath
27-Jan-2017 02:25:30.071 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Initializing Spring root WebApplicationContext
27-Jan-2017 02:25:33.060 SEVERE [localhost-startStop-1] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.opendls.webapp.listener.StartupListener
java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:297)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.opendls.webapp.listener.StartupListener.contextInitialized(StartupListener.java:48)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4725)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5189)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
27-Jan-2017 02:25:33.071 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
27-Jan-2017 02:25:33.075 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log Closing Spring root WebApplicationContext
27-Jan-2017 02:25:33.405 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log ContextListener: contextInitialized()
27-Jan-2017 02:25:33.407 INFO [localhost-startStop-1] org.apache.catalina.core.ApplicationContext.log SessionListener: contextInitialized()
where is the problem?
ok - the problem is resolved
as M.Denim mentioned there were some duplicate listeners which was triggering the same ApplicationContext to load.
There was .tld file in WEB-INF which was containing the same listeners which was in web.xml, the file was created by ant task. A new implementation of Tomcat 8.x read them as noPluggabilityListeners and load was triggered second time.

Request Mapping in Spring MVC - back to previous URL

I have a problem with redirect in Spring MVC. I have a controller with few methods:
//PRINT ALL WORKERS
#RequestMapping("/print")
public String listWorkers(Model model)
{
model.addAttribute("workerList", workerService.getAllWorkers());
return "print";
}
//EDIT WORKER DATA
#RequestMapping("/edit")
public String redirectWorker(HttpServletRequest request)
{
String parameter = request.getParameter("workers");
String path = "redirect:/edit/" + parameter;
return path;
}
#RequestMapping("/edit/{worker}")
public String editWorker(#PathVariable("worker")
String login, Model model)
{
model.addAttribute("worker", workerService.getWorker(login));
return "edition";
}
When I'm using in my program for example method with "print" in request mapping, my URL is:
http://localhost:8080/WWP/print
But when I'm using my method with "edit/{worker}" in request mapping and after that I used method with "print" I got an URL:
http://localhost:8080/WWP/edit/print
Cause my "print" was added after "edit" which isn't necessary. How to return to previous URL:
http://localhost:8080/WWP/print
I suppose that I have to change my RequestMapping annotation. But I don't know how to do this.
EDIT:
Ok, here is my web.xml file:
<?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 http://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>/WEB-INF/spring/root-context.xml, /WEB-INF/spring/appServlet/security.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>/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>
<!-- Filtry -->
<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>
<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>
</web-app>
I used the "c:url" in the JSP and it worked.
For example:
It will be remember the previous URL (It's wrong):
My Events
It will be good:
<c:url var="myEventsUrl" value="/events/my" />
My Events
URL is always link to: _http://localhost:8080/WWP/events/my

Dispatcher servlet mapping

I am trying to use spring-security
Before all of the configuration
http://localhost:9090/app/login2.xhtml
request, works as i expected.
I added a controller:
#Controller
#RequestMapping("/auth")
public class LoginController {
#RequestMapping(value = "/login", method = RequestMethod.GET)
public String getLoginPage(#RequestParam(value="error", required=false) boolean error,
ModelMap model) {
return "login2.xhtnml";
}
}
I have in web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:META-INF/spring-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
With this configuration when i call
http://localhost:9090/app/login2.xhtml
Error comes
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/app/login2.xhtml] in DispatcherServlet with name 'spring'
BUT when i change configuration mapping to
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
http://localhost:9090/app/login2.xhtml works as i expected
but
http://localhost:9090/app/auth/login
gives no error, no exception, no redirection, i think dispatcher servlet can not know about this request.
http://localhost:9090/app/app/auth/login
works with
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
My understanding:
dispatcher servlet use "http://localhost:9090/" as base for searching login2.xhtml
and use "http://localhost:9090/app" for /auth/login URL.
I do not know where to set this, and why they are different.
Have you added the SpringSecurityFilterChain to the web.xml?
<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>
Could you past the registered "Request Bindings" if the container starts (from the logfile)?

Can not connect JAX-RS service to MVC template

I'm attempting to make use of JAX-RS' (Jersey) MVC pattern. Attempts to reach http://localhost:8080/myproject/foos/test result in an error that reads:
java.io.IOException: The template name, /view, could not be resolved to a fully qualified template name
http://localhost:8080/myproject/foos results in the same error.
What am I missing?
Resource:
package resources;
import com.sun.jersey.api.view.Viewable;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("foos")
public class FooResource {
#GET
#Produces(MediaType.TEXT_HTML)
public Viewable get() {
return new Viewable("/index", this);
}
#GET
#Path("{id}")
#Produces(MediaType.TEXT_HTML)
public Viewable get(#PathParam("id") String id) {
return new Viewable("/view", id);
}
}
Views:
WEB-INF / jsp / resources / FooResource
index.jsp
view.jsp
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/(resources|images|js|styles|(WEB-INF/jsp))/.*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<description>Set the default, base template path to the WEB-INF folder.</description>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/jsp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Made the following changes:
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>jersey</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>controllers</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/((WEB-INF/views))/.*</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
<param-value>/WEB-INF/views/</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.Redirect</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.config.feature.FilterForwardOn404</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Resource:
#Path("foos")
public class FooResource {
#GET
#Produces(MediaType.TEXT_HTML)
public Viewable index() {
return new Viewable("/foos/index", this);
}
#GET
#Path("{id}")
#Produces(MediaType.TEXT_HTML)
public Viewable view(#PathParam("id") String id) {
return new Viewable("/foos/view", id);
}
}
Views:
\welcome.jsp
\WEB-INF\views\foos\
index.jsp
view.jsp
I had this same error running under Jetty 9. The application ran fine using mvn clean jetty:run but had this error when packaged as a war and deployed under Jetty. This is the fix in web.xml that worked for me:
<init-param>
<param-name>com.sun.jersey.config.property.JSPTemplatesBasePath</param-name>
- <param-value>/WEB-INF/views/</param-value>
+ <param-value>/WEB-INF/views</param-value>
</init-param>
Yep, that's it. So, hopefully this helps someone who stumbles across this. My config is basically the same as craig's, but had the extra slash.
From initial inspection I think you want to put index.jsp and view.jsp directly in WEB-INF/jsp.
The name should be a fully qualified name like /index.jsp or /index.html.

Resources