0

I'm having an application which takes input from a popup dialog.When a user enters a text in the text field of a popup dialog.I need to pass that text to another method ina different class.How can I achieve this. My code is shown below.

viewRoles.jsp

    <%@page contentType="text/html"  import="com.model.Data" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
       <link href="bootstrap.css" type="text/css" rel="stylesheet">
       <script>
          window.onload=function()
          {
                var el=document.getElementById('btnAddNewRole');
                el.onclick=function()
                {
                var my_text=prompt('Enter text here');
                if(my_text){
                    $.ajax({
                    url: '/com.model/Data/addRole('+my_text+')',
                    success: function(my_text){
                    alert("Inserted")
                    }, error: function(){
                    // when got error
                    }
                    });
                }
                }
          }
        </script>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>View Roles</title>
    </head>
    <body><br><br>
           <button id="btnAddNewRole" class="btn btn-info">Add New Role</button>
    </body>
</html>

Data.java in com.model package

package com.model;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.hibernate.Session;

@ManagedBean
@SessionScoped
public class Data {
    private Roles r;
    private HibernateUtil helper;
    private Session session;

    public void addRole(String roleTitle){
        r=new Roles(roleTitle);
        session = helper.getSessionFactory().openSession();
        session.beginTransaction();
        session.save(r);
        session.getTransaction().commit();
        session.close();
    }
6
  • I don't think you can ! ajax map to an URL who your js function will get into your addRole methode ? you need to use a mapping in your server side. Commented Oct 15, 2017 at 9:25
  • @AtimeneNazim How can I achieve this task can you please guide me, Your answer is not quite understandable to me Commented Oct 15, 2017 at 9:28
  • you need to put an url into your server side. this way your client (ajax call) can reach the method. Commented Oct 15, 2017 at 9:34
  • Do you have controllers ? Commented Oct 15, 2017 at 9:41
  • @AtimeneNazim No actually I'm sending data to db using Data.java class,Here You have said 'put an url into your server side' can you please guide me what it really means I didn't get it yet.I'm very beginner to programming.Thanks in advance Commented Oct 15, 2017 at 9:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.