Struts 1.3.8学習ノート(六)

2772 ワード

6つ目のバージョンでは、ActionでSessionオブジェクトを取得し、オブジェクトをSessionに保存し、jspページで取得することが重要です.
 
更新後のコード
LoginAction.java
package com.coderdream.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hp.gddc.ad.db.UserDao;
import com.hp.gddc.ad.form.LoginForm;
import com.hp.gddc.ad.vo.UserView;

public class LoginAction extends Action {

	/**
	 *        
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		//           
		LoginForm lf = (LoginForm) form;
		UserDao userDao = new UserDao();
		String userName = lf.getUserName();
		String password = lf.getPassword();
		UserView userView = new UserView();
		userView.setUserName(userName);
		userView.setPassword(password);
		int result = userDao.queryUser(userName, password);
		//     
		if (1 <= result) {
			HttpSession session = request.getSession();
			session.setAttribute("userView", userView);

			//          ,       
			return mapping.findForward("success");
		} else {
			//        ,       
			return mapping.findForward("failing");
		}
	}

}

 
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>    </title>
</head>
<body>
	      !
	<br>      Session :
	<logic:present name="userView" scope="session">
		<bean:write name="userView" property="userName" />
	</logic:present>
</body>
</html:html>