2013/11/14 - [프로그램 자료/ASP.NET] - ASP.NET 코드 비하인드에서 자바스크립트 실행 Code-behind JavaScript Run


자바로 개발하다보니 권한이나 정책을 체크할 때 사용하려고 만들었다.

기본적인 권한이 현재는 사용자, 관리자 등의 role만 체크하고 있는데 해당 role 보다 더 작은 단위의 기능별 권한이 필요해졌다.



import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.http.HttpServletResponse;

 

public class ControllerAccessHelper {

 

    public static void accessDeniedAndBack(HttpServletResponse resp) throws IOException {

        String script = "<script>alert('권한이 없어 이전페이지로 돌아갑니다.'); history.back();</script>";

        callJavaScript(resp, script);

    }

 

    public static void callJavaScriptWithAlertAndHistoryBack(HttpServletResponse resp, String msg) throws IOException {

        String script = String.format("<script>alert('%s'); history.back();</script>", msg);

        callJavaScript(resp, script);

    }

 

    public static void callJavaScriptWithAlertAndRedirect(HttpServletResponse resp, String msg, String location) throws IOException {

        String script = String.format("<script>alert('%s'); location.replace('%s');</script>", msg, location);

        callJavaScript(resp, script);

    }

 

    public static void callJavaScript(HttpServletResponse resp, String script) throws IOException {

        resp.setContentType("text/html; charset=UTF-8");

        PrintWriter writer = resp.getWriter();

        writer.println(script);

        writer.flush();

    }

 

}

 


Posted by motolies
,