Pagination with Spring Hibernate - java

I am trying to implement Spring Pagination in my (Spring MVC / Hibernate / MySQL) project under Eclipse STS so I've followed the example in [A Pagination Technique Using Spring][1] since I found it recommended here in Stackoverflow in many answers to similar questions. Now I have a question and a problem:
Question: How can I import the pagination tag file into my project? Specially that I don't see any Tag folder under my project WEB-INF folder?
Problem: I am facing some trouble as each time I try to build / run the project I get error:
java.lang.ClassNotFoundException: org.springframework.web.context.support.StandardServletEnvironment
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at org.springframework.web.servlet.HttpServletBean.(HttpServletBean.java:90)
at org.springframework.web.servlet.FrameworkServlet.(FrameworkServlet.java:211)
at org.springframework.web.servlet.DispatcherServlet.(DispatcherServlet.java:303)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:357)
at java.lang.Class.newInstance(Class.java:310)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:138)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1114)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1672)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:662)
I have double checked my code and I don't see what might be wrong. So can anyone please tell me what I might be doing wrong?
Controller:
public String listVolDisc(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
List searchResults = ivoldiscservice.getAllVolDisc();
PagedListHolder pagedListHolder = new PagedListHolder(searchResults);
int page = ServletRequestUtils.getIntParameter(request, "p", 0);
pagedListHolder.setPage(page);
int pageSize = 10;
pagedListHolder.setPageSize(pageSize);
model.addAttribute("pagedListHolder", pagedListHolder);
return "VolDiscount";
}
JSP:
<c:url value="/paging.do" var="pagedLink">
<c:param name="action" value="list"/>
<c:param name="p" value="~"/>
</c:url>
<div class="section">
<h2 class="section_title">Volume Discounts</h2>
<tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
<div class="table">
<div class="table_header">
<div class="cell">ID</div>
<div class="cell">From</div>
<div class="cell">To</div>
<div class="cell">Discount</div>
</div>
<%-- <c:forEach var="voldiscount" items="${voldiscountList}"> --%>
<c:forEach var="voldiscount" items="${pagedListHolder}">
<div class="table_row">
<div class="cell important">${voldiscount.id}</div>
<div class="cell">${voldiscount.rangeStarts}</div>
<div class="cell">${voldiscount.rangeEnds}</div>
<div class="cell">${voldiscount.discount}</div>
</div>
</c:forEach>
</div><!-- .table -->
<tg:paging pagedListHolder="${pagedListHolder}" pagedLink="${pagedLink}"/>
</div><!-- .section -->
DAO:
public List<VolumeDiscount> getAllVolDisc() {
return sessionfactory.getCurrentSession().createQuery("from VolumeDiscount v").list();
}
Thanks for your time

It seems you have missing dependency: spring-web.

Related

MalformedStreamException when uploading file in Spring Boot 2.2.5 application

After upgrading Spring Boot from 2.2.4 to 2.2.5 in my application, I get the following exception when uploading files with a multipart/form-data form:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly] with root cause
org.apache.commons.fileupload.MultipartStream$MalformedStreamException: Stream ended unexpectedly
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:1033)
at org.apache.commons.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:931)
at java.io.InputStream.read(InputStream.java:101)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:98)
at org.apache.commons.fileupload.util.Streams.copy(Streams.java:68)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:346)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:113)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:159)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.resolveMultipart(CommonsMultipartResolver.java:143)
at org.springframework.web.servlet.DispatcherServlet.checkMultipart(DispatcherServlet.java:1178)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1012)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
...
The form looks like this:
<form id="addPhotosForm" method="post" action="/images/add" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-4">
<div class="input-group">
<input type="file" name="images" multiple>
</div>
</div>
<div class="col-sm-6 pull-left">
<input name="submit" type="submit" value="Save">
</div>
</div>
</form>
And the controller looks like this:
#RequestMapping(value = "/images/add")
public String addPhoto(#RequestParam(value = "images", required = false) MultipartFile[] multipartImages) {
//...
}
Works fine after updating Spring Boot to version 2.2.6.

Unterminated <html:form tag JSP

I have a small problem . When i use form without if condition like
<html:form action="/customerDepositReport.doo?showCustDepsForPrintingReceipt_action=showCustDepsForPrintingReceipt">
it works fine but when i enclosed it in my if else like
<% if(session.getAttribute("correctionEntry")!=null) { %>
<html:form action="/preEntryCorrection.doo?entryCorrection_action=entryCorrection">
<input type="hidden" name="showDepositsList" value="">
<% }else{ %>
<html:form action="/customerDepositReport.doo?showCustDepsForPrintingReceipt_action=showCustDepsForPrintingReceipt">
<% } %>
It gives me an exception :
org.apache.jasper.JasperException: /jsp/custmngmt/reports/viewCustomerDepositReportCriteriaForOldReceipt.jsp(356,0) Unterminated <html:form tag
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:132)
org.apache.jasper.compiler.Parser.parseBody(Parser.java:1646)
org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:976)
org.apache.jasper.compiler.Parser.parseCustomTag(Parser.java:1247)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1421)
org.apache.jasper.compiler.Parser.parse(Parser.java:130)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:255)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:194)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:360)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:607)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:312)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:326)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:253)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.apache.struts.action.ActionServlet.processActionForward(ActionServlet.java:1833)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1670)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:514)
javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
I have tested the condition , it does enter the if part but causes exception when going through the form tag . I don't get it. Some help please.
please, terminate tags porperly,
</html:form> into your jsp file after <html:form> uses.
Your Error clearly says , Unterminated (see it)
means, you need to close every tag(here <html:form>) after it's uses.

java.lang.IllegalStateException: CDI API is not available in this environment. at org.omnifaces.config.BeanManager

I m trying to display an image from a managed bean method which returns byte[] with <o:graphicImage> using omnifaces i deployed omnifaces 2.1 in WEB_INF/lib
here is my code
Register.java
public byte[] getImage() throws IOException{
return Utils.toByteArray(Faces.getResourceAsStream("/resources/images/loader.gif"));
}
Home.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:o="http://omnifaces.org/ui">
<h:head>
<h:outputStylesheet library="css" name="style.css"></h:outputStylesheet>
<style>
.center {
align: center;
}
</style>
</h:head>
<h:body>
<h:form>
<table style="float:right;margin-top:5px;">
<tr><td><h:commandLink action="/user/second.xhtml" style="float:right;">Second page</h:commandLink></td>
<td><h:commandLink action="#{login.logout}" style="float:right;">Logout</h:commandLink></td></tr>
</table>
</h:form>
<h:form>
<div style="margin: auto;">
<h:messages globalOnly="true" infoStyle="display:none" styleClass="error" style="list-style-type:none;"/>
<h:messages globalOnly="true" errorStyle="display:none" styleClass="green" style="list-style-type:none;"/>
<h4 align="center">Welcome to JSF #{login.username} Dude</h4>
<h3 align="center">User Profile</h3>
<h:dataTable value="#{register.getUser(login.username)}" var="info"
styleClass="center" style="margin:auto;" border="1">
<h:column>
<f:facet name="header">Image</f:facet>
<o:graphicImage value="#{register.getImage()}" type="jpg" dataURI="true"/>
</h:column>
<h:column>
<!-- column header -->
<f:facet name="header">Email</f:facet>
<!-- row record -->
<h:outputText value="#{info.email}"
rendered="#{not register.edit}" />
<!-- <h:inputText value="#{info.email}" rendered="#{not register.edit}" /> -->
</h:column>
<h:column>
<!-- column header -->
<f:facet name="header">First Name</f:facet>
<!-- row record -->
<h:outputText value="#{info.first}" rendered="#{not register.edit}" />
<!-- <h:inputText value="#{info.first}" rendered="#{not register.edit}" /> -->
</h:column>
<h:column>
<!-- column header -->
<f:facet name="header">Last name</f:facet>
<!-- row record -->
<h:outputText value="#{info.last}" rendered="#{not register.edit}" />
<!-- <h:inputText value="#{info.last}" rendered="#{not register.edit}" /> -->
</h:column>
<h:column>
<!-- column header -->
<f:facet name="header">Phone</f:facet>
<!-- row record -->
<h:outputText value="#{info.phone}"
rendered="#{not register.edit}" />
<!-- <h:inputText value="#{info.phone}" rendered="#{not register.edit}" /> -->
</h:column>
<h:column>
<h:commandButton value="Update" action="/user/Updateprofile.xhtml">
</h:commandButton>
</h:column>
</h:dataTable>
</div>
</h:form>
</h:body>
</html>
At first tomcat got started shown this error <o:graphicImage> Tag Library supports namespace: http://omnifaces.org/ui, but no tag was defined for name: graphicImage] when i have added the omnifaces.jar into my build path in eclipse. After the omnifaces.jar in WEB-INF/lib
it is not letting the tomcat to start and showing the following error
java.lang.ExceptionInInitializerError
at org.omnifaces.application.OmniApplication.<init>(OmniApplication.java:69)
at org.omnifaces.application.OmniApplicationFactory.createOmniApplication(OmniApplicationFactory.java:89)
at org.omnifaces.application.OmniApplicationFactory.getApplication(OmniApplicationFactory.java:54)
at org.apache.myfaces.config.FacesConfigurator.configureApplication(FacesConfigurator.java:1926)
at org.apache.myfaces.config.FacesConfigurator.configure(FacesConfigurator.java:529)
at org.apache.myfaces.webapp.AbstractFacesInitializer.buildConfiguration(AbstractFacesInitializer.java:296)
at org.apache.myfaces.webapp.Jsp21FacesInitializer.initContainerIntegration(Jsp21FacesInitializer.java:73)
at org.apache.myfaces.webapp.AbstractFacesInitializer.initFaces(AbstractFacesInitializer.java:118)
at org.apache.myfaces.webapp.StartupServletContextListener.contextInitialized(StartupServletContextListener.java:111)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: CDI API is not available in this environment.
at org.omnifaces.config.BeanManager.<init>(BeanManager.java:88)
at org.omnifaces.config.BeanManager.<clinit>(BeanManager.java:49)
... 18 more
Caused by: java.lang.ClassNotFoundException: javax.enterprise.inject.spi.BeanManager
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.omnifaces.config.BeanManager.<init>(BeanManager.java:82)
... 19 more
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.omnifaces.ApplicationListener
java.lang.NoClassDefFoundError: Could not initialize class org.omnifaces.config.BeanManager
at org.omnifaces.ApplicationListener.checkCDIAvailable(ApplicationListener.java:63)
at org.omnifaces.ApplicationListener.contextInitialized(ApplicationListener.java:55)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: One or more listeners failed to start. Full details will be found in the appropriate container log file
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/First] startup failed due to previous errors
Jul 23, 2015 5:16:06 PM org.apache.catalina.core.StandardContext listenerStop
SEVERE: Exception sending context destroyed event to listener instance of class org.apache.myfaces.webapp.StartupServletContextListener
java.lang.NoClassDefFoundError: Could not initialize class org.omnifaces.config.BeanManager
at org.omnifaces.application.OmniApplication.<init>(OmniApplication.java:69)
at org.omnifaces.application.OmniApplicationFactory.createOmniApplication(OmniApplicationFactory.java:89)
at org.omnifaces.application.OmniApplicationFactory.getApplication(OmniApplicationFactory.java:54)
at org.apache.myfaces.context.servlet.FacesContextImplBase.getApplication(FacesContextImplBase.java:131)
at org.apache.myfaces.webapp.AbstractFacesInitializer._dispatchApplicationEvent(AbstractFacesInitializer.java:239)
at org.apache.myfaces.webapp.AbstractFacesInitializer.destroyFaces(AbstractFacesInitializer.java:273)
at org.apache.myfaces.webapp.StartupServletContextListener.contextDestroyed(StartupServletContextListener.java:153)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:5063)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5723)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I am using JSF 2.0(Apache Myfaces), and i also have tomahawk 1.1.14 used for uploading the file.
OmniFaces 2.x requires CDI. See also Download section of OmniFaces homepage:
OmniFaces 2.x
Required: Java 1.7, JSF 2.2, EL 2.2, Servlet 3.0 and CDI 1.1
Optional: BV 1.0
Tomcat as being a barebones JSP/Servlet container doesn't ship with CDI out the box. It's only available in full fledged Java EE containers like WildFly and TomEE. The same applies to JSF (and JSTL), by the way, you'd have to manually install it in Tomcat.
You've 2 options:
Replace Tomcat by TomEE or any other Java EE container.
Manually install CDI in Tomcat.
See also:
How to install and use CDI on Tomcat? - contains detailed installation instructions

Spring security can't initialize context

I am going through the official spring documentation examples:
adding spring security to application without it: http://docs.spring.io/spring-security/site/docs/3.2.x/guides/helloworld.html
creating custom login
http://docs.spring.io/spring-security/site/docs/3.2.x/guides/form.html
and have successfully added security to my application. However, when I go to add a custom login the documentation talks about a dependency "since our project adds the messages-jc project as a dependency..." so I added this to my pom. Since I have added this it throws an error as shown below.
I can remove this error if I exclude my SecurityWebApplicationInitializer and then run the app, however the system does not correctly redirect to the login page, and the login page does not work correctly (I use th:if and none of the if statements work it acts as if all of the if statements are true even if I say th:if="false"). If I remove the jc-messages dependency then it redirects correctly however the login page still does not work and trying to login throws a invalid CSRF Token 'null' was found on the request parameter '_csrf' or header 'X-CSRF-TOKEN error.
It seems as if the security class is being registered more than once triggering the error, but when I take away the SecurityWebApplicationInitializer nothing is registering the class. I feel like there is something basic I am missing since I am following a simple tutorial however I can't figure out what it is. The only other thing I could think of besides being a spring problem is that there is something wrong with Thymeleaf which is keeping the login page from not working.
Any help is appreciated!
SecurityWebApplicationInitializer.java
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(SecurityConfig.class);
}
}
pom.xml Commented out other dependencies to isolate the springframework dependency problem
<dependencies>
<!-- ... other dependency elements ... -->
<!-- <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency> -->
<!-- <dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.4.RELEASE</version>
</dependency> -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-samples-messages-jc</artifactId>
<version>3.2.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
SecurityConfig.java
#Configuration
#EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
}
#Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/res/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/loginSpring.html")
.permitAll();
}
}
loginSpring.html
<html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head>
<title tiles:fragment="title">Messages : Create</title>
</head>
<body>
<div tiles:fragment="content">
<form name="f" th:action="#{/login}" method="post">
<fieldset>
<legend>Please Login</legend>
<div th:if="${param.error}" class="alert alert-error">
Invalid username and password.
</div>
<div th:if="${param.logout}" class="alert alert-success">
You have been logged out.
</div>
<label for="username">Username</label>
<input type="text" id="username" name="username"/>
<label for="password">Password</label>
<input type="password" id="password" name="password"/>
<div class="form-actions">
<button type="submit" class="btn">Log in</button>
</div>
</fieldset>
</form>
</div>
</body>
</html>
Stack trace
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:277)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4701)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5204)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5199)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Jul 23, 2014 5:47:10 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Jul 23, 2014 5:47:10 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/geocounts] startup failed due to previous errors
Jul 23, 2014 5:47:10 PM org.apache.catalina.core.ApplicationContext log
SEVERE: Servlet dispatcher threw unload() exception
javax.servlet.ServletException: Servlet.destroy() for servlet dispatcher threw exception
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1397)
at org.apache.catalina.core.StandardWrapper.stopInternal(StandardWrapper.java:1723)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:225)
at org.apache.catalina.core.StandardContext$4.run(StandardContext.java:5427)
at java.lang.Thread.run(Unknown Source)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5459)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:225)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.startup.Catalina.start(Catalina.java:620)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431)
Caused by: java.lang.NullPointerException
at org.springframework.web.servlet.FrameworkServlet.destroy(FrameworkServlet.java:809)
at org.apache.catalina.core.StandardWrapper.unload(StandardWrapper.java:1377)
... 24 more
Broken login page
When using Java based configuration for Spring Security, CSRF protection is enabled by default. You can switch it off as http.csrf().disable().

Call java method in JSP file

I want to call my method getSelectedLayouts in my jsp page, where the method is
public Iterable<Layouts> getSelectedLayouts(String Subject){
Session sess=getCurrentSession();
return sess.createCriteria(Layouts.class, Subject).list();
}
Inside class LayoutManager. I passed LayoutManager into my jsp page using a Spring Bean
<custom:useSpringBean var="layoutManager" bean="LayoutManager">
the jsp page asks for the subject
<form method="post">
<label for="subjectName">SubjectName:</label>
<input type="text" name="subjectName" size="50" id="subjectName">
<input class="button" type="submit" value="Search Layout" name="submit">
</form>
Which I then pass to
<jsp:useBean id="subjectName" class="LayoutManager">
<c:if test="${param.submit!=null}">
(here's where I want to call my getSelectedLayouts method)
</c:if>
I've been trying with scriplets, including variations of
<jsp:setProperty name="layout" property="*"/>
((LayoutManager)pageContext.getAttribute("layoutManager")).getSelectedLayout((La‌​youts)pageContext.getAttribute("layout"));
or just
<jsp:setProperty name="layout" property="*"/>
list<Layouts> = LayoutManager.getSelectedLayouts(layout);
Where Layouts is my object class
Please tell me if I need to give any other information
Edit: When I try
LayoutManager layoutManager = new LayoutManager();
String subjectNa = request.getParameter("subjectName");
Iterable<Layouts> bla = layoutManager.getSelectedLayouts(subjectNa);
I get the error list
org.apache.jasper.JasperException: An exception occurred processing JSP page /search.jsp at line 72
Iterable<Layouts> waters = layoutManager.getSelectedLayouts(subjectNa);
java.lang.NullPointerException
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getCurrentSession(LayoutManager.java:37)
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getSelectedLayouts(LayoutManager.java:50)
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:502)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
root cause
java.lang.NullPointerException com.amazon.basalt.examples.octane.tomcat.LayoutManager.getCurrentSession(LayoutManager.java:37)
com.amazon.basalt.examples.octane.tomcat.LayoutManager.getAllLayouts(LayoutManager.java:68)
org.apache.jsp.search_jsp._jspService(search_jsp.java:221)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
One possible solution. (If using scriplets is not an issue.)
<%
LayoutManager layoutManager = new LayoutManager();
String subjectName = request.getParameter("subjectName");
layoutManager.getSelectedLayouts(subjectName);
%>
Do remember to import the class in the JSP
I recommend you to use ajax to get the the list of layouts.

Categories