WebWork2.2.4シミュレーションwebwork+hibernateのテンプレート例


パッケージ構造:
com.huanglq.action    --------LoginAction.java 
com.huanglq.dao          --------LoginDao..java,LoginDaoImpl.java
com.huanglq.vo            --------User.java
----------------------------------------------------
vo:(model)
User.java
package com.huanglq.vo;

public class User {

	private String username;

	private String password;

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

}

-----------------
dao:(データベース・アクセス・レイヤ)
UserDao.java

package com.huanglq.dao;

import com.huanglq.vo.User;

public interface UserDao {

	public User login(String username, String password);
}
-----------------
UserDaoImpl.java

package com.huanglq.dao;

import com.huanglq.vo.User;

public class UserDaoImpl implements UserDao {

	//         ,     null
	//       Hibernate   ,       Spring
	public User login(String username, String password) {
		User user = null;
		//    Hibernate  ,        User    
		//        
		if ("kyo".equalsIgnoreCase(username)&& "123".equalsIgnoreCase(password)) {
			user = new User();
			user.setUsername(username);
			user.setPassword(password);

		}
		return user;
	}

}
--------------------
アクション
LoginAction.java
package com.huanglq.action;

import com.huanglq.dao.UserDao;
import com.huanglq.dao.UserDaoImpl;
import com.huanglq.vo.User;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

public class LoginAction implements Action, ModelDriven {

	private User user = new User();

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	//      Action    ,        , form  *.action (* action )
	public String execute() throws Exception {
		UserDao userDao = new UserDaoImpl();
		try {
			user = userDao.login(user.getUsername(), user.getPassword());
		} catch (Exception e) {
			user = null;
		}
		if (user != null) {
			return "success";
		} else {
			return "error";
		}
	}

	public Object getModel() {
		//        model  
		return user;
	}

	//      , form  *!login.action (* action )
	public String login() {
		UserDao userDao = new UserDaoImpl();
		try {
			user = userDao.login(user.getUsername(), user.getPassword());
		} catch (Exception e) {
			user = null;
		}
		if (user != null) {
			return "success";
		} else {
			return "error";
		}
	}

}
------------------------------
xwork.xmlコンテンツ:

<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">

<xwork>

	<include file="webwork-default.xml" />

	<package name="default" extends="webwork-default">
		<action name="login" class="com.huanglq.action.LoginAction">
			<result name="success" type="dispatcher">
				/login/success.jsp
			</result>
			<result name="error" type="dispatcher">
				/login/login.jsp
			</result>
			<interceptor-ref name="params" />
			<interceptor-ref name="model-driven" />
		</action>
	</package>

</xwork>
------------------------
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name>webwork-simple-hello</display-name>

    <filter>
        <filter-name>webwork</filter-name>
        <filter-class>com.opensymphony.webwork.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>webwork</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
	
	<welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

-----------------------------------
index.jsp:(login.jspとindex.jspの差は多くない)
<%@ page pageEncoding="gb2312" contentType="text/html;charset=gb2312"%>
<html>
<body>
<!--     action  login      -->
<form action="login!login.action">
<p align="center">
  <br>
</p>
   :
<input type="text" name="user.username" />
<br>
  
<input type="password" name="user.password"/>
<br>
<input type="submit" value="  "/>
<input type="reset" value="  "/>
</form>
</body>
</html>

-------------------------------------------
success.jsp:
<%@ taglib prefix="ww" uri="/webwork"%>
<html>
<head>
<title>SUCCESS</title>
</head>
<body>
Welcome to WebWork <ww:property value="user.username"/>
</body>
</html>