webwork学习笔记(二)之简单登录实现

Wednesday June 6, 2007

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.
//DTD Web Application 2.3//EN" 
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
  <servlet>
  	<servlet-name>webwork</servlet-name>
  	<servlet-class>
com.opensymphony.webwork.dispatcher.ServletDispatcher
</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>webwork</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  <taglib>
  	<taglib-uri>webwork</taglib-uri>
  	<taglib-location>/WEB-INF/lib/webwork-2.1.7.jar</taglib-location>
  </taglib>
  <resource-ref>
	<res-ref-name>jdb/sqlserver</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
</resource-ref>
</web-app>


配置webwork.properties略,参考 此篇
jsp文件之 index.jsp 实现登录界面

<%@ page contentType="text/html; charset=gbk"%>
<%@ taglib prefix="ww" uri="webwork"%>
<html>
	<head>
		<title>login</title>
	</head>
	<body>
		<form action="login.action" method="post">
			<input type="textfield" name="userName" /><br />
			<input type="password" name="password" /><br/>
			<input type="submit" value="submit" /><br/>
			<ww:property value="message" />
		</form>
	</body>
</html>

登录成功页面 loginsuccess.jsp

<%@ page contentType="text/html; charset=gbk"%>
<%@ taglib prefix="ww" uri="webwork"%>
<html>
	<head>
		<title>success</title>
	</head>
	<body>
		<ww:property value="#session['userName']" />
		<ww:property value="message" />
	</body>
</html>

action,只列出execute方法,其它方法略

   private String userName;
	private String password;
	private String message;
   .......
   public String execute() {
		boolean isSucced = false;
		if (userName != null) {
			System.out.println("start to logining");
			userName = userName.trim();
			password = password.trim();
			if (userName.equals("") && password.equals("")) {
				isSucced = false;
				message = "用户名或密码不能为空";
			} else {
				isSucced = true;
			}
		if (isSucced) {
			if (userName.equals("zbsdg") && password.equals("123")) {
				isSucced = true;
                     ActionContext.getContext().getSession()
                  .put("userName", userName);
					message = "登录成功";
				} else {
					isSucced = false;
					message = "用户名或密码错误";
				}
			}
		} else {
			isSucced = false;
			message = "请输入用户名或密码";
		}
		if (isSucced) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}

配置xwork.xml

<?xml version="1.0" encoding="UTF-8"?>
<!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">
		<default-interceptor-ref name="completeStack" />
		<action name="login" class="com.webwork.action.Login">
			<result name="error">index.jsp</result>
			<result name="success">loginsuccess.jsp</result>
		</action>
	</package>
</xwork>

2 Comments

  1. 呵呵,开始学java了?是在freebsd上的java开发环境?速度怎么样?据说freebsd对java的支持不怎么样,就一直没有安装哦。

  2. 只有猪头才会给自己找麻烦,没有任何理由可以让我跑去fb下开发java, 我在xp下开发惬意的很…

Leave a reply