Struts2 Demo

下面两张图,用 viso 画了1个小时~ viso 很强大啊!

工作原理

Struts2的工作原理(图解)详解
Struts2基本原理

Struts2原理.png-163.3kB

工作流程

Struts2步骤.png-284.9kB

Hello World Demo

Struts 2 hello world (XML版本)

1.png-14.5kB

web.xml

配置 Struts 2

<?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">
    <display-name></display-name>

    <welcome-file-list>
        <welcome-file>HelloWorld.jsp</welcome-file>
    </welcome-file-list>

    <!-- Struts2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

struts.xml

配置每个Action。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="struts2" extends="struts-default">
        <global-results>
            <result name="login">/HelloWorld.jsp</result>
        </global-results>
        <action name="loginPerson" class="action.LoginAction">
            <result name="success">/welcome.jsp</result>
        </action>
    </package>
</struts>

HelloWorld.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>登录界面</title>
</head>
<body>
    <form action="loginPerson">
        <table>
            <tr>
                <td>账号</td>
                <td><input type="text" name="account"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"></td>
            </tr>
            <tr>
                <td><input type="submit" value="登录"></td>
            </tr>
        </table>
    </form>
</body>
</html>

welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>欢迎界面</title>
</head>
<body>
    welcome!
    <%=request.getAttribute("account")%>
</body>
</html>

LoginAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

    private String account;
    private String password;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getPassword() {
        return password;
    }

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

    @Override
    public String execute() throws Exception {

        if ("yano".equals(account) && "123456".equals(password)) {
            return SUCCESS;
        }

        return LOGIN;
    }

}

下载链接

http://pan.baidu.com/s/1pKNZix9

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值