gpt4 book ai didi

javascript - 将 Angular JS 与 JSP 和 Servlet 集成

转载 作者:行者123 更新时间:2023-11-30 16:34:33 25 4
gpt4 key购买 nike

我是 Angular Js 的新手,我正在尝试将 Angular JS 与 JSP 和 Servelet 集成。我想编写一个简单的代码,它只是从 Action 类中检索数据并显示在 Index.jsp 页面中。这是我的代码:-

索引.jsp -

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Fetch Data</title>
<script type="text/javascript" src="js/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope , $http ) {


$scope.getDataFromServer = function() {
alert("Check Function");
$http({
method : 'GET',
url : '/AngularJsServlet'
}).success(function(data, status, headers, config) {
alert('data----'+data);
$scope.person = data;
}).error(function(data, status, headers, config) {
alert("failure");
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
});
</script>
</head>
<body>
<div data-ng-app="myApp" data-ng-controller="myCtrl">

<button data-ng-click="getDataFromServer()">Fetch data from server
</button>
<p>First Name : {{person.firstName}}</p>
<p>Last Name : {{person.lastName}}</p>
</div>
</body>
</html>

PersonData.Java(模型类)-

package com.model;

public class PersonData {

private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}

AngulaJsServelet.Java( Action 类)-

     package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;


import com.google.gson.Gson;
import com.model.PersonData;

public class AngularJsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public AngularJsServlet() {
super();
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PersonData personData = new PersonData();
personData.setFirstName("Mohaideen");
personData.setLastName("Jamil");

String json = new Gson().toJson(personData);
System.out.println("json---" + json);
response.setContentType("application/json");
response.getWriter().write(json);
}
}

web.xml -

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Practice</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>AngularJsServlet</servlet-name>
<servlet-class>com.controller.AngularJsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AngularJsServlet</servlet-name>
<url-pattern>/AngularJsServlet</url-pattern>
</servlet-mapping>

</web-app>

错误-

 GET http://localhost:8080/AngularJsServlet 404 (Not Found)

请给我建议解决这个问题的方法。

谢谢。

最佳答案

错误很明显。您收到 404 是因为该页面丢失了。你应该检查你的网址。它应该是这样的形式:

协议(protocol)://host:port/appName/page

但是您显然缺少 appName。假设您的 web.xml 中的显示名称与您的项目名称相同,您应该能够像这样修复它:

GET http://localhost:8080/Practice/AngularJsServlet

关于javascript - 将 Angular JS 与 JSP 和 Servlet 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com