theRoot
theRoot

Reputation: 569

Can't get jQuery datepicker in Struts 2

I want to use jQuery datepicker in my Struts app

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My First JQuery Page</title>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="css/mytestcss.css">
<script src="/js/jquery-1.11.1.js"></script>
<script src="/js/jquery-ui.js"></script>
<script>
    $(function() {
     $( "#datepicker" ).datepicker();
    });
</script>
</head>
<body>
  <div id="formdiv">
    <s:form>
        <s:textfield label="username" name="username" ></s:textfield>
        <s:password label="password" name="password" ></s:password>
        <s:textfield label="date" id="datepicker" name="date"></s:textfield>
        
    </s:form>
  </div>
  <input type="text" id="datepicker">
</body>
</html>

Did I miss anything? Why datepicker is not loading?

Upvotes: -1

Views: 645

Answers (3)

theRoot
theRoot

Reputation: 569

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>My First JQuery Page</title>
<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">
<link rel="stylesheet" type="text/css" href="css/mytestcss.css">
<script src="js/jquery-1.11.1.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
    $(function() {
     $( "#datepicker" ).datepicker();
    });
</script>
</head>
<body>
  <div id="formdiv">
    <s:form>
        <s:textfield label="username" name="username" ></s:textfield>
        <s:password label="password" name="password" ></s:password>
        <s:textfield label="date" id="datepicker" name="date"></s:textfield>

    </s:form>
  </div>

</body>
</html>

The .js file was not loaded ,because of /js/jquery-1.11.1.js i changed it to js/jquery-1.11.1.js (removed '/')

Upvotes: 1

Roman C
Roman C

Reputation: 1

You can use s:url tag to build correct path

<script src="<s:url value='/js/jquery-1.11.1.js'/>"></script>

JSTL c:url tag works similar

<script src="<c:url value='/js/jquery-1.11.1.js'/>"></script>

or use JSP EL

<script src="${pageContext.request.contextPath}/js/jquery-1.11.1.js"></script>

Upvotes: 1

Georgi Bilyukov
Georgi Bilyukov

Reputation: 627

You should use document ready event to be sure your element is rendered

Try using this:

$(document).ready(function() {
    $( "#datepicker" ).datepicker();
});

Upvotes: 0

Related Questions