eclipse 编写struct2的 demo

1、eclipse,mysql,apache tomcat的安装和配置见前面几篇文章

建一个Dynamic Web Project,取名叫OATest

2、struts2配置

下载struts2需要的的完整包,将其中的以下文件复制到eclipse的对应web工程的WebContent/WEB-INF/lib的lib目录下:

            1)xwork-core-
            2)struts2-core-
            3)ognl-
            4)freemarker-
            5)commons-io-
            6)commons-fileupload-
            7)javassist-
            8)commons-lang3-
3、struts2修改web.xml

     修改web.xml增加过滤器filter,最终配置文件如下所示

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<constant name="struts.i18n.encoding" value="GBK"></constant>
	<package name="default" extends="struts-default">
		<action name="loginAction" class="com.oa.action.LoginAction">
			<result name="success">welcome.jsp</result>
			<result name="error">err.jsp</result>
		</action>
	</package>
</struts>


 
4、定义表单的JSP页面

<%@ page language="java" contentType="text/html; charset=GBK"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<c:form action="loginAction">
		<c:textarea name="username" key="username"></c:textarea>
		<c:textarea name="password" key="password"></c:textarea>
		<input type="submit" value="提交"/>
	</c:form>
</body>
</html>

5、定义拦截类com.oa.action.LoginAction

注意,包为com.oa.action

package com.oa.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;
	private String password;

	public String execute() {
		if (username.equals("a") && password.equals("a"))
			return SUCCESS;
		return ERROR;
	}

	public String getUsername() {
		return username;
	}

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

	public String getPassword() {
		return password;
	}

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

}











                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值