如何将表单数据导入角度控制器以及如何将其传递给 serlvet?

我有一个包含两个<select>标签的表单。一个是年份列表,另一个是剂量列表。


当我选择选项时selectedYear,我可以使用 ng-model 在我的控制器中获取,但无法获取selectedDosage. 当我试图将此数据发送到我的 servlet 时,它返回 null 作为值。


我感谢 JSON 的任何建议


Dosages:[

    {"name":"Tablet"},

    {"name":"Syrups and Suspension"},

    {"name":"Non-Antibiotic Capsules"},

    {"name":"Antibiotic Capsules"},

    {"name":"Dry Powder for Suspension"},

    {"name":"ORS"},

    {"name":"Antiseptics"}

]

Years:{"json1":"[

    {\"years\":2018},

    {\"years\":2017},

    {\"years\":2016},

    {\"years\":2015}]"

HTML


<form  ng-submit="Display()">


    <select class="form-control" ng-model="selectedYear">

        <option value="" selected disabled>Select Year</option>

        <option   ng-repeat="option in years" ng-value="{{option.years}}" >

          {{option.years}}

        </option>

    </select>

    <br>

    <select class="form-control" ng-model="selectedDo">

        <option value="" selected disabled>Select Dosage</option>

        <option  ng-repeat="option in section" ng-value="{{option.name}}"  >

          {{option.name}}

        </option>

    </select>


    <input type="submit" value="Submit"/>       

</form>

控制器


$scope.selectedDo;

$scope.selectedYear;

$scope.Display=function(){


    $window.alert($scope.selectedDo+"..."+ $scope.selectedYear);

    $http({

        method:"post",

        url:"/com.az.BusinessRatioAnalysis/SectionServlet",

        data:{

           year:$scope.selectedYear,

           dosage:$scope.selectedDo,

        }

    }).then(function(result){

        $window.alert("Ajex call failed" + result);                             

    });

};


小服务程序


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String year=request.getParameter("year");

        String dosage=request.getParameter("dosage");

                System.out.println(year+".."+dosage);

    }


拉风的咖菲猫
浏览 64回答 1
1回答

小唯快跑啊

请尝试以下操作。负载 Bean 类:class Bean {&nbsp; &nbsp; Year year;&nbsp; &nbsp; Dosage dosage;&nbsp; &nbsp; //getters and setters}服务类:public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {&nbsp; &nbsp; StringBuilder json = new StringBuilder();&nbsp; &nbsp; String str;&nbsp; &nbsp; while ((str = request.getReader().readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; json.append(str);&nbsp; &nbsp; }&nbsp; &nbsp; // Using Jackson&nbsp; &nbsp; ObjectMapper objectMapper = new ObjectMapper();&nbsp; &nbsp; Bean bean = objectMapper.readValue(json.toString(), Bean.class);&nbsp; &nbsp; System.out.println("Year ::" + bean.getYear());&nbsp; &nbsp; System.out.println("Dosage ::" + bean.getDosage());}如果是 Maven 项目,则为 pom.xml 文件,否则相应地添加 jars:<dependency>&nbsp; &nbsp; <groupId>com.fasterxml.jackson.core</groupId>&nbsp; &nbsp; <artifactId>jackson-databind</artifactId>&nbsp; &nbsp; <version>2.9.8</version></dependency>PS:忽略上面代码中的异常处理
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java