0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

seasar2のお勉強4(SAstrutsでセッション使う)

Posted at

前と似た感じだけど今度はセッション使います。

登録用画面 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:writeってしてるところは

<c:out value="${personListDto.personForm.name}"/>

とかでもいけます。
で、Action

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";
	}
}

Bean達。PersonListDto なんて名前をしているのにpersonForm一個しか持って無いのは
単にBeanがネストしている場合のとり方を確認しておきたかったからです。

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から入力して結果表示すると以下のようになります。
Clipboard01.png

テーブルみたいにリスト表示したい場合はこんな感じのActionと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とか全然最近触ってなかったからsastrutsよりこっち思い出すほうが大変だった気がするね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?