我有一个从弹出对话框中获取输入的应用程序。当用户在弹出对话框的文本字段中输入文本时。我需要将该文本传递给另一个类中的另一个方法。如何实现这一点。我的代码如下所示。
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>
com.model包中的Data.java
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();
}