seasar2의 학습4 (SAstruts에서 세션 사용)

5675 단어 Seasar2
이전과 비슷한 느낌이지만 이번에는 회화를 사용해야 한다.
등록 화면 input.jsp
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    <html:errors/>
    <s:form method="post">
        名前 <html:text property="name" />
        所属 <html:text property="belonging" />
        その人の特徴 <html:text property="feature" />
        <s:submit property="regist" value="送信" />
    </s:form>
</body>
</html>
화면 표시
<%@ page pageEncoding="UTF-8"%>
<%@ page import="org.seasar.sastruts.example.form.PersonForm" %>
<%@ page import="org.seasar.sastruts.example.dto.PersonListDto" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    以下の内容で登録しました。
    名前:<bean:write name="personListDto" property="personForm.name"/>  <br>
    所属:<bean:write name="personListDto" property="personForm.belonging"/> <br>
    特徴:<bean:write name="personListDto" property="personForm.feature"/><br>
</body>
</html>
bean: 뭐 해요?
<c:out value="${personListDto.personForm.name}"/>
할 수 있어.
동작
package org.seasar.sastruts.example.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.seasar.sastruts.example.dto.PersonListDto;
import org.seasar.sastruts.example.form.PersonForm;
import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;

public class PersonAction {

    @Resource
    @ActionForm
    protected PersonForm personForm;

    @Resource
    protected HttpSession session;

    @Execute(validator=false)
    /** 登録画面表示処理 */
    public String index() {
        return "input.jsp";
    }



    @Execute(validator=true, input="input.jsp")
    /** 登録処理 */
    public String regist() {
        PersonListDto pld = new PersonListDto();
        pld.personForm = personForm;
        session.setAttribute("personListDto", pld);
        return "complete?redirect=true";
    }

    /** 登録完了画面に遷移する */
    @Execute(validator=false)
    public String complete() {
        return "complete.jsp";
    }
}
빈들아.이름은 Person List Doto이지만 Person Form은 하나밖에 없어요.
단지 빈을 끼워 넣은 상태에서의 처리 방법을 확인하고 싶었을 뿐이다.
package org.seasar.sastruts.example.dto;

import java.io.Serializable;

import org.seasar.framework.container.annotation.tiger.Component;
import org.seasar.framework.container.annotation.tiger.InstanceType;
import org.seasar.sastruts.example.form.PersonForm;

@Component(instance = InstanceType.SESSION)
public class PersonListDto implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public PersonForm personForm;

    public PersonForm getPersonForm() {
        return personForm;
    }

    public void setPersonForm(PersonForm personForm) {
        this.personForm = personForm;
    }


}
package org.seasar.sastruts.example.form;

import java.io.Serializable;

import org.seasar.struts.annotation.Required;

public class PersonForm implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    @Required
    /** 名前 */
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBelonging() {
        return belonging;
    }

    public void setBelonging(String belonging) {
        this.belonging = belonging;
    }

    public String getFeature() {
        return feature;
    }

    public void setFeature(String feature) {
        this.feature = feature;
    }

    @Required
    /** 所属*/
    public String belonging;

    @Required
    /** 特徴*/
    public String feature;


}
input에서 적당히 입력한 결과는 다음과 같습니다.

테이블처럼 목록을 보여주고 싶다면 이런 동작으로 jsp와
    /** リストに登録処理 */
    @Execute(validator=true, input="input.jsp")
    @SuppressWarnings({ "unchecked", "rawtypes"})
    public String registList() {
        System.out.println("registList呼ばれました");
        List plist = (List<PersonForm>) session.getAttribute("plist");
        if( plist == null) {
            plist = new ArrayList<PersonForm>();
        }

        plist.add(personForm);
        session.setAttribute("plist", plist);
        return "completeList?redirect=true";
    }
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
    以下の内容が登録済みです。<br/>
    <c:forEach items="${plist}" var="person">
        <c:out value="${person.name}" default="取れなかった"/>
        <c:out value="${person.belonging}" default="取れなかった"/>
        <c:out value="${person.feature}" default="取れなかった"/><br/>
    </c:forEach>
</body>
</html>
최근에 jsp와struts를 전혀 만져본 적이 없어요. 사struts보다 회상하기가 더 힘들어요.

좋은 웹페이지 즐겨찾기