loginaction
package com.god.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction_2014_5_5 extends ActionSupport{
private String uname;
private String upasswd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpasswd() {
return upasswd;
}
public void setUpasswd(String upasswd) {
this.upasswd = upasswd;
}
public void validateExecute(){
System.out.println("--validate--");
if(uname==null||uname.equals("")){
super.addActionError("用户名不能为空");
}
if(upasswd==null||upasswd.equals("")){
super.addActionError("密码不能为空");
}
}
public void validate(){
System.out.println("--validate2--");
if(uname==null||uname.equals("")){
super.addActionError("用户名不能为空");
}
if(upasswd==null||upasswd.equals("")){
super.addActionError("密码不能为空");
}
}
public String execute(){
System.out.println("--执行--");
if(uname.equals("zhu")&&upasswd.equals("123")){
return "ok";
}else{
return "no";
}
}
}
struts.xml
<package name="default" namespace="/2014_strut2_lx" extends="struts-default" >
<action name="loginAction" class="com.god.action.LoginAction_2014_5_5" >
<result name="ok">/2014_5_5_welcome_lx_01.jsp</result>
<result name="no">/2014_5_5_welcome_lx_02.jsp</result>
<result name="input">/2014_5_5_login_lx_bqk_01.jsp</result><!-- addActionError,指明返回到哪个网址 -->
</action>
</package>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %><!-- 引用标签 -->
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP '2014_4_29_login_lx_01.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<h3>这是一个简单的Struts UI标签库的例子</h3>
<br/><hr/>
<div>
<font color='red'><s:actionerror/></font>
</div>
<s:form action="loginAction" namespace="/2014_strut2_lx">
<s:textfield name="uname" label="请输入用户名" />
<s:password name="upasswd" label="请输入密码" />
<s:submit value="登录" />
</s:form>
</center>
</body>
</html>