2014-10-14 인터셉터

jsp2.0 2014. 10. 14. 09:34

사용자 요청이 Action에 도달하기 전에 가로 채 Action 실행 전 후에 다른 실행 코드를 넣을 수 있는 객체


controller에


<action name="hello" class="struts.hello.HelloAction">

<result name="success">/1013/hello.jsp</result>

</action>

action result 사이에

<interceptor-ref name="params" />

<interceptor-ref name="workflw" />생략 되어 있어서 안써도 됨(이미 정의 되어 있음)


직접 만든 인터셉트를 사용할 때는 인터셉터를 모두 넣어 주어야 한다(생략된 인터셉터는 동작 안함) 액션마다 해주어야 함




public String intercept(ActionInvocation arg0) throws Exception{

System.out.println("전 인터셉터");

String str = arg0.invoke();

// 목적지 Action의 execute 메서드 호출 

//invoke 기준으로 전 인터셉터 후 인터셉터 나눠짐

System.out.println("후 인터셉터");

return str;

}




<interceptors> 

<interceptor name="test" class="struts.test.TestInterceptor"></interceptor>

</interceptors>

인터셉터는 패키지 바로 밑에 있어야 하고 action위에 있어야 됨

'jsp2.0' 카테고리의 다른 글

2014-10-15 checkbox  (0) 2014.10.15
2014-10-15 인터셉터  (0) 2014.10.15
2014-10-13 Struts2  (0) 2014.10.13
2014-10-08  (0) 2014.10.08
2014-10-07  (0) 2014.10.07
Posted by 5DMK2]발자국
,

2014-10-13 Struts2

jsp2.0 2014. 10. 13. 09:52





Java Resources -> src -> struts.xml생성


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_2_5.xsd" id="WebApp_ID" version="2.5">

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>                    //              /* <==모든 주소 받음

</filter-mapping>

</web-app>


상수

public static final String SUCCESS = "success";  기본값

public static final String NONE = "none";     정상처리 되었지만 view 필요 없을 때

public static final String ERROR = "error";

public static final String INPUT = "input";     파라미터가 못 왔을 때 

public static final String LOGIN = "login";    로그인 실패 했을 때 




struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="" extends="">
<action name="" class="">
<result></result>
</action>
</package>

</struts>
추가

    


실행방법

http://localhost:8000/struts/hello.aciton

프로젝트 이름/ 액션 테그 이름


pojo방식

aaa.java 

execute가 기본 메서드

메서드 이름 변경 후 

struts.xml파일에 

<action name="test" class="struts.hello.TestAction"> 에 메서드 이름 추가하면 메서드 사용 가능

method="abcd"





Validateable 유효성 검사




1.set 먼저 동작

2.Validateable - set메서드에 들어온 값을 확인

3. execute



Struts =>기본 설정이 되어있다.


request.getParamater ==> setXXXX

request.setAttribute ==> getXXX


'jsp2.0' 카테고리의 다른 글

2014-10-15 인터셉터  (0) 2014.10.15
2014-10-14 인터셉터  (0) 2014.10.14
2014-10-08  (0) 2014.10.08
2014-10-07  (0) 2014.10.07
10-01 슈퍼인터페이스  (0) 2014.10.01
Posted by 5DMK2]발자국
,

2014-10-08

jsp2.0 2014. 10. 8. 09:26

1. 로그인 후 글쓰기

jsp파일에서<c:if test="${memId == null}"> 로 로그인 설정 하여 로그인 되었을 때

글쓰기 버튼 활성화


2. 작성자는 로그인된 ID

작성자 : ${memID} 


3. 나의 작성 글목록

자바파일에서 String id = (String)session.getAttribute("memID");


id값으로 쿼리문 날려서 결과 값을 받은 후

list= select * from board where id=? 

글 리스트 자바파일에서  

request.setAttribute("list", list);

값을 넣고 리스트 jsp파일에서

<c:forEach var="list" items="${list}">

와 같이 넣는다


4. 비밀번호 확인 후 글 읽기 + 관리자는 모두 읽기 가능

클릭한 글 번호

int num = Integer.parseInt(req.getParameter("num"));

와 해당 글의 비밀번호

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

를 받고 해당 글의 번호와 비밀번호를 

int check = selct passwd from board where num=?

하여 쿼리문을 날려서 받아옴 

리턴 받은 값을

자바 파일에서 if문을 사용하여 비밀 번호가 같으면 글 내용을 

if(check ==1){ 글 내용 페이지}

}else{

경고 창 띄움

}


5. 관리자만 답변가능

관리자 ID(adminID)설정 된 상태에서

String id = (String)session.getAttribute("memId");

로그인된 ID와 관리자와 같으면

if(id.equals("adminID)){

String adminID = "adminID";

req.setAttribute("adminID", adminID);

}

JSP 파일에서 로그인 된 ID와 관리자 ID가 같으면

<c:if test="${memId == adminID}"> 

답변 버튼 기능 활성화


6. 관리자만 글쓰기 가능

관리자 ID(adminID)설정 된 상태에서

String id = (String)session.getAttribute("memId");

로그인된 ID와 관리자와 같으면

if(id.equals("adminID)){

String adminID = "adminID";

req.setAttribute("adminID", adminID);

}

JSP 파일에서 로그인 된 ID와 관리자 ID가 같으면

<c:if test="${memId == adminID}"> 

글쓰기 버튼 기능 활성화


7. 쿠키를 활용한 자동 로그인


쿠키 생성

Cookie coo1 = new Cookie("memId", "id");

Cookie coo2 = new Cookie("passwd", "passwd");

coo1.setPath("/");

coo2.setPath("/");

coo1.setMaxAge(3000); //쿠키 시간 설정

coo2.setMaxAge(3000); 

response.addCookie(coo1);

response.addCookie(coo2);

키 불러오기

Cookie[] cookies = request.getCookies();

if(cookies  != null){

for(int i = 0; i < cookies.length; i++){

Cookie thisCookie = cookies[i];

HttpSession session = reqeust.getSession();

session.setAttribute("memId", id);

}

}

'jsp2.0' 카테고리의 다른 글

2014-10-14 인터셉터  (0) 2014.10.14
2014-10-13 Struts2  (0) 2014.10.13
2014-10-07  (0) 2014.10.07
10-01 슈퍼인터페이스  (0) 2014.10.01
모델2기반의 MVC패턴  (0) 2014.09.30
Posted by 5DMK2]발자국
,