首页 > 解决方案 > 从 JSP 页面获取值到 Java 类

问题描述

我在 JSP 页面中有一个下拉菜单,我正在从此页面中选择国家代码的值。我想在其他类中获取这个国家代码,我可以使用它来获取与所选国家相对应的状态列表。

<html>
    <head>
        <title>Home Page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> 
    </head>
<body>

<button id="mybtn">Get Country</button>

<div>
<select id="output">

<script type="text/javascript">

                                         $('#mybtn').click(function () {

                                            $.getJSON('countryDetail', function (data) {
                                                $("select#output > option").remove();
                                                 $.each(data, function (key, value) {
                                                    $("#output").append('<option>' + value['countryCode'] + " " + value['countryName']   + '</option>');

                                                    $("select").change(function(){
                                                        var selectedOption = $(this).find(":selected").val();
                                                        document.getElementById("Country").innerHTML = selectedOption;
                                                       
                                                                                                             
                                                            var countryDetails= document.getElementById("myText").innerHTML;
                                                           
                                                        
                                                    });
                                                  
                                                    
                                                });
                                            });
                                        }); 

                                         
</script>


</select>


<h1>"The value for Country is: " <span id="Country"></span></h1>



</body>
</html>

我要获取国家代码并在查询中使用值的 Java 类是:

@Repository
public class StateDetailDAOImpl implements StateDetailDAO {
    
    @Autowired
    private SessionFactory sessionFactory;
    
    


    @Override
    public List<StateDetail> getStateDetail() {
        
        
        
        Session currentSession = sessionFactory.getCurrentSession();
        
        Query<StateDetail> theQuery = currentSession.createQuery("from StateDetail", StateDetail.class);

        List<StateDetail> StateDetails = theQuery.getResultList();


        return stateDetails;
    }

}

标签: jqueryspringhibernatejsp

解决方案


推荐阅读