commons-fileupload- 1.2.1.jar。
S2SH框架配置步骤(4)
时间:2010-12-22 BlogJava 扬帆出海
3.2 配置一个Action
下面开始配置一个Action吧,以用户登录为例:
1)首先新建一个登陆页面login.jsp,代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<s:form name="form1" action="login" >
<s:textfield name="username" label="username" ></s:textfield>
<s:password name="password" label="password" ></s:password>
<s:submit label="submit"></s:submit>
</s:form>
<s:actionerror/>
</body>
</html>
2)在我们已经建好的struts.xml中来配置登录的action。这里定义登录 action的名字为login,配置代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts2" extends="struts-default">
<action name="login" class="test.LoginAction">
<result name="success" type="redirect">index.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
</struts>
3)下面就来编写具体的action类了。代码如下:
1package test;
2import com.opensymphony.xwork2.ActionSupport;
3public class LoginAction extends ActionSupport {
4 public String username;
5 public String password;
6 public String execute() {
7 if(!username.equals("admin") ) {
8 super.addFieldError("username", "用户名 错误!") ;
9 return ERROR;
10 }
11 if(!password.equals("001") ) {
12 super.addFieldError("password", "密码错误 !") ;
13 return ERROR;
14 }
15 return SUCCESS;
16 }
17
|