简体   繁体   中英

how to dynamically change the status message on the webpage for various results using angular js

I am trying to develop a web application in which there were options of adding actor,modifying,searching,removing. my requirement is to have message of actor added sucesfully for add actor and actor modified sucessfully,these messages are sent by their respective servlets to statusMessage.html.i want to have only one statusMessage.html for various messages.

my servlet is AddActor.java

package com.flp.fms.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.flp.fms.service.ActorServiceImpl;
import com.flp.fms.service.IActorService;

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try
        {
            response.setContentType("text/html");  
            IActorService actorService=new ActorServiceImpl();
            Map<String,String> actorDetails=new HashMap<String,String>();
            actorDetails.put("firstName", request.getParameter("firstName"));
            actorDetails.put("lastName", request.getParameter("lastName"));

            if(actorService.AddActor(actorDetails))
            {
                response.sendRedirect(request.getContextPath() +"/htmlTemplates/statusMsg.html");
            }
        }
        catch (Exception e) {
            // TODO: handle exception
        }
    }
}

my statusMsg.html is

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Status</title>
        <link rel="stylesheet" href="style.css">
        <script type="text/javascript" src="../script/angular.min.js"></script>
        <script type="text/javascript" src="../script/angular-route.min.js">></script>

        <script type="text/javascript" src="../script/appConfig.js"></script>
        <script type="text/javascript" src="../script/appCtrl.js"></script>
</head>
<body ng-app="myApplication">
<img src='../static/bg.png' width="1280" height="222"/>
<a href="Welcome.html" class="show_buttton button1">Home</a>
<div ng-controller="status">
    <h1 align=center>{{message}}</h1>
</div>
</body>
</html>

My Controller is

app.controller('status',function($scope,$http){
    var url='http://localhost:8018/angularjs_webapp/statusMsg';

    $http.get(url)
        .success(function(response){
            $scope.message=response;
        });
});

my statusMsg.java is

package com.flp.fms.controller;

import java.io.IOException;
import java.io.PrintWriter;

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

import com.google.gson.Gson;

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

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message=(String) request.getAttribute("message");
        PrintWriter out=response.getWriter();
        response.setContentType("application/json");

        Gson gson=new Gson();
        String myGsonMsg=gson.toJson(message);
        out.println(myGsonMsg);
    }
}

Using sendRedirect method in your AddActor.java servlet redirects request coming in this service to the statusMsg.java servlet.

Your can use getRequestDispathcer(URL).forward(request, response) in your AddActor.java servlet where you can push response as a request to the statusMsg.java servlet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM