一个简单的Struts2.0 的登录的例子

一、下载struts2.0

http://struts.apache.org/download

二、编辑web.xml

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"> 
    <display-name>HelloWorld</display-name> 
    <!-- 定义Struts 2 的FilterDispatcher 的Filter --> 
    <filter> 
        <!-- 定义核心Filter的名字 --> 
        <filter-name>struts2</filter-name> 
        <!-- 定义核心Filter实现类 --> 
        <filter-class> 
            org.apache.struts2.dispatcher.FilterDispatcher  
        </filter-class> 
    </filter> 
    <!-- FilterDispatcher 用来初始化 Struts2 并且处理所有的Web请求 --> 
    <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> 

<?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">
<display-name>HelloWorld</display-name>
<!-- 定义Struts 2 的FilterDispatcher 的Filter -->
<filter>
<!-- 定义核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心Filter实现类 -->
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<!-- FilterDispatcher 用来初始化 Struts2 并且处理所有的Web请求 -->
<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>


三、创建一个简单的用户登录界面

Html代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<html> 
    <head> 
        <title>简单的用户登录</title> 
    </head> 
    <body> 
        <form action="Login.action" methos="post"> 
            用户名:<input type="text" name="name" /> 
            <br> 
            密  码:<input type="password" name="password" /> 
            <br> 
            <input type="submit" name="ok" value="提交" /> 
        </form> 
    </body> 
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>简单的用户登录</title>
</head>
<body>
<form action="Login.action" methos="post">
用户名:<input type="text" name="name" />
<br>
密  码:<input type="password" name="password" />
<br>
<input type="submit" name="ok" value="提交" />
</form>
</body>
</html>


四、创建实现控制类

Java代码
package tutorial;  
 
public class Login {  
    private String name;  
 
    private String password;  
 
    public String getPassword() {  
        return password;  
    }  
 
    public void setPassword(String password) {  
        this.password = password;  
    }  
 
    public String getName() {  
        return name;  
    }  
 
    public void setName(String name) {  
        this.name = name;  
    }  
 
    public String execute() {  
        if ("abc".equals(this.name) && "123".equals(this.password))  
            return "success";  
        else 
            return "false";  
    }  


package tutorial;

public class Login {
private String name;

private String password;

public String getPassword() {
return password;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String execute() {
if ("admin".equals(this.name) && "admin".equals(this.password))
return "success";
else
return "false";
}
}


五、配置struts.xml

Xml代码
<!DOCTYPE struts PUBLIC  
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
        "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <include file="struts-default.xml"></include> 
    <package name="strutsqs" extends="struts-default">   
        <action name="Login" class="tutorial.Login"> 
            <result name="success" type="redirect">success.jsp</result> 
            <result name="false" type="redirect">faile.jsp</result> 
        </action> 
    </package> 
</struts> 

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"></include>
<package name="strutsqs" extends="struts-default">
<action name="Login" class="tutorial.Login">
<result name="success" type="redirect">success.jsp</result>
<result name="false" type="redirect">faile.jsp</result>
</action>
</package>
</struts>


六、创建两个结果页面

(1)success.jsp页面

Html代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<html> 
    <head> 
        <title>简单的用户登录</title> 
    </head> 
    <body> 
        登录成功  
    </body> 
</html> 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>简单的用户登录</title>
</head>
<body>
登录成功
</body>
</html>

(2)faile.jsp页面

Html代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 
<html> 
    <head> 
        <title>简单的用户登录</title> 
    </head> 
    <body> 
        <form action="Login.action" methos="post"> 
            用户名:<input type="text" name="name" /> 
            <br> 
            密  码:<input type="password" name="password" /> 
            <br> 
            <input type="submit" name="ok" value="提交" /> 
        </form> 
    </body> 
</html> 

你可能感兴趣的:(java,apache,jsp,xml,struts)