I want to include js and css files in my jsp, but I'm not able to do so. I'm new to the concept of spring MVC. For a long time, I've been working on this same topic. My index Page is like this
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/> <script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js"> </script> <style type="text/css"> body { background-image: url("LoginPageBackgroundImage.jpg"); } </style> </head> <body > <h6>Please login in google Chrome</h6> <h1 align="center">Welcome to my Twitter Clone</h1> <div class="m" style="margin-left: 401px; margin-top: 70px;"> <form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);"> <fieldset> <legend align="center">Login</legend> <div class="a"> <div class="l">User Name</div> <div class="r"> <INPUT type="text" name="userName"> </div> </div> <div class="a"> <div class="l">Password</div> <div class="r"> <INPUT type="password" name="password"> </div> </div> <div class="a"> <div class="r"> <INPUT class="button" type="submit" name="submit" value="Login"> </div> </div> <i align="center" style="margin-left: 183px;">New User? <a href="signup.html"><u>Signup</u></a></i> </fieldset> </form> </div> </body> </html>
And my spring-dispatcher-servlet.xml is like this.
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.csc.student" /> <mvc:annotation-driven/> <!--<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />--> <!-- <bean name="/welcome.html" class ="csc.csc.helloController.HelloController" /> --> <bean id="viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix"> <value>/WEB-INF/</value> </property> <property name ="suffix"> <value>.jsp</value> </property> </bean> </beans>
My controller is like this.
package com.csc.student; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class StudentInfoController { @RequestMapping(value = "/indexPage.html", method = RequestMethod.GET) public ModelAndView getAdmissionFrom() { ModelAndView model = new ModelAndView("indexPage"); return model; } }
Can some one help me in this? I'm struggling very hard but I'm not getting any solution. I have kept my js and css file in WEB-INF folder.
Put your css/js files in folder src/main/webapp/resources . Don't put them in WEB-INF or src/main/resources . Show activity on this post. Show activity on this post.
You can not call JavaScript function in if statement of JSP, because JSP is executed at the server side and JavaScript is executed at client side. You have to trigger event when the one of the radio button is clicked, using onclick event you can call function corc() .
If you open the browser console and go to the 'networks' tab, you will see 404 (resource not found) against it. You cannot specify a relative URL to a JavaScript in a JSP file that way. And then keep the 'js' folder containing 'index. js' at the same level as 'WEB-INF'.
Well yes definitely you can. Consider JSP page as advanced HTML along with features of java. So surely you can use CSS in three ways: Inline CSS <h1 style="color:blue;"><%= someVariable %></h1>
First you need to declare your resources in dispatcher-servlet file like this :
<mvc:resources mapping="/resources/**" location="/resources/folder/" />
Any request with url mapping /resources/** will directly look for /resources/folder/.
Now in jsp file you need to include your css file like this :
<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
Similarly you can include js files.
Hope this solves your problem.
Put your style.css
directly into the webapp/css
folder, not into the WEB-INF
folder.
Then add the following code into your spring-dispatcher-servlet.xml
<mvc:resources mapping="/css/**" location="/css/" />
and then add following code into your jsp page
<link rel="stylesheet" type="text/css" href="css/style.css"/>
I hope it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With