struts2配置参考

1:login.JSP
  <form action="login.action"  method="post">
   username:<input type="text" name="username"> <br/>
   password:<input type="text" name="password"> <br/>
   <input type="submit" name="submit"/>
   </form>

<s:form action="login">
    <s:textfield name="username" label="username"></s:textfield>
    <s:password name="password" label="password"> </s:password>
    <s:submit name="submit"></s:submit>
    </s:form>

2:action
package com.test.action;
public class LoginAction {
    private String username;
    private String password;
    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;
    }
    public String execute() throws Exception {
        return "success";
    }
}



package com.test.action;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String username;
    private String password;
    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;
    }
    public String execute() throws Exception {
        if("smallfa".equals(this.getUsername().trim())&&"smallfa".equals(this.getPassword().trim()))
        {
            return "success";
        }
        else
        {
            this.addFieldError(username, "username or password errors");
            return "failer";
        }
    }
    @Override
    public void validate() {
        if(null==this.getUsername()||"".equals(this.getUsername().trim()))
        {
            this.addFieldError(username, "username required");
        }
        if(null==this.getPassword()||"".equals(this.getPassword().trim()))
        {
            this.addFieldError(username, "username required");
        }
    }
}

3:struts-xml
<?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>
 <package name="struts2" namespace="/" extends="struts-default">
        <action name="login" class="com.test.action.LoginAction">
            <result name="success">/result.jsp</result>
        </action>
    </package>
</struts>
   
4:web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <filter>
        <!-- 定义核心Filter的名字 -->
        <filter-name>struts2</filter-name>
        <!-- 定义核心Filter的实现类 -->
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>    
  </web-app>

5:result.jsp
  username:${requestScope.username}<br/>
  password:${requestScope.password}<br/>


6:参考视频

视频: Struts2入门与配置  浪曦 J2EE Struts 2应用开发详解系列视频

http://v.youku.com/v_show/id_XNTY4MDUwMzY=.html

posted on 2010-03-30 01:22 smallfa 阅读(338) 评论(0)  编辑  收藏 所属分类: SSH


只有注册用户登录后才能发表评论。


网站导航:
 
<2010年3月>
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

公告

smallfa
博客园
C++博客
博客生活
Blogjava
足球博客
微博
Redsaga

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

Ajax

Blogs

DB

java

Open source

ORM

Tools/Help

vedio Tech

搜索

最新评论

阅读排行榜

评论排行榜