小毅也玩struts2之helloWorld程序改進版(增加了驗證功能和struts標簽使用)
Posted on 2008-10-09 20:30 H2O 閱讀(397) 評論(0) 編輯 收藏 所屬分類: strutslogin.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>struts標簽的登陸頁面</title>
</head>
<body>
<s:form name="loginForm" method="post" action="login" >
<s:textfield name="username" label="用戶名"></s:textfield>
<s:password name="pwd" label="密 碼"></s:password>
<s:submit label=" 登 陸 "></s:submit>
<s:reset label=" 重 置 "></s:reset>
</s:form>
</body>
</html>
show.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>顯示結果</title>
</head>
<body>
<!-- 因為struts2都是用的dispatcher即轉發,都是同一次請求,所以可以到request作用于中取值 -->
<h3>用戶名--->${requestScope.username}<br>
密碼--->${requestScope.pwd}<br>
</body>
</html>
loginAction
package com.yz.struts2.actions;

import com.opensymphony.xwork2.ActionSupport;

public class loginAction extends ActionSupport{
private String username;
private String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String helloWorld() {
System.out.println("用戶名--->"+username);
System.out.println("密碼--->"+pwd);
if("小毅".equals(username) && "xiaoyi".equals(pwd)){
return "ok";//查找名字為ok的result,相當于struts1.*的foward名
}else{
this.addFieldError("username","用戶名或密碼錯誤");
return "failer";
}
}
//驗證表單輸入
@Override
public void validate() {
if("".equals(this.getUsername().trim()) || null==this.getUsername()){
//第一個參數為: index.jsp表單中的文本框的name(名字)第二個為錯誤消息
this.addFieldError("username", "用戶名不能為空");
}
if("".equals(this.getPwd().trim()) || null==this.getPwd()){
this.addFieldError("pwd", "密碼不能為空");
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- struts2采用過濾器過濾客戶端發送給服務器的所有請求 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml
<?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>
<!-- struts2會自動到classes下找struts.xml,
直接放在src下工具會自動把src下的文件編譯到classes下
dtd頭信息表示 sturs為根標簽
-->
<!-- struts2這個包繼承sturs2內置的包struts-default -->
<package name="struts2" extends="struts-default">
<!-- 配置action
name: 對應index.jsp頁面中form的action= login.action 的login
struts默認所有以點action結尾的請求交給struts處理,因為繼承自webwork的特性,習慣于這樣。。
class: action對應的類,包名點類名全路徑(com.yz.struts2.loginAction)
method:請求該action時自動執行的方法,如果沒有配置默認執行execute方法
-->
<action
name="login"
class="com.yz.struts2.actions.loginAction"
method="helloWorld"
>
<!-- result默認的name為success -->
<result name="ok">/show.jsp</result>
<!-- 在繼承ActionSuport的action中的validate方法來驗證表單輸入信息,驗證中添加filedError后返回
name為input的result對應的頁面,必須name為input,類似于struts1.*中action配置的input
-->
<result name="input">/login2.jsp</result>
<result name="failer">/login2.jsp</result>
</action>
</package>
</struts>

















show.jsp











loginAction













































web.xml





















struts.xml

































