• Struts2 Hello World


    案例:

      用户登录

     login.jsp代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>登录页面</title>
      </head>
      
      <body>
            <form action="${ pageContext.request.contextPath }/login.action" method="post">
                <table align="center">
                      <caption><h3>用户登录</h3></caption>
                    <tr>
                        <td>用户名:</td>
                        <td><input type="text" name="username" /></td>
                    </tr>
                    <tr>
                        <td>密&nbsp;&nbsp;&nbsp;码:</td>
                        <td><input type="password" name="password" /></td>
                    </tr>
                    <tr align="center">
                        <td colspan="2"><input type="submit" value="登录" />&nbsp;&nbsp;<input type="reset" value="重置" /></td>
                    </tr>
                </table>
            </form>
      </body>
    </html>
    View Code

    LoginAction代码:

    package com.itheima.action;
    
    import com.itheima.domain.User;
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    public class LoginAction extends ActionSupport implements ModelDriven<User> {
    
        private static final long serialVersionUID = 6114481653813951691L;
    
        private User user = new User();
    
        @Override
        public User getModel() {
            return user;
        }
    
        public String login() {
            if ("tom".equals(user.getUsername()) && "123".equals(user.getPassword())) {
                return "success";
            } else {
                return "fail";
            }
    
        }
    
    }
    View Code

    Welcome.jsp代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>登录成功页面</title>
      </head>
      
      <body>
              <p>Welcome!</p>
      </body>
    </html>
    View Code

    error.jsp代码:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>登录失败页面</title>
      </head>
      
      <body>
              <p>Fail!!!</p>
      </body>
    </html>
    View Code

    struts.xml配置:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- 指定Struts2配置文件的DTD信息 -->
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
    <!-- struts是Struts2配置文件的根元素 -->
    <struts>
    
        <constant name="struts.devMode" value="true" />
        <!-- Struts2的Action必须在指定的包空间下定义 -->
        <package name="default" namespace="/" extends="struts-default">
            <!-- 定义login的Action,该Action的实现类为 com.itheima.action.LoginAction 类 -->
            <action name="login" class="com.itheima.action.LoginAction"
                method="login">
                <!-- 定义处理结果与资源之间映射关系 -->
                <result name="success">/welcome.jsp</result>
                <result name="fail">/error.jsp</result>
            </action>
    
        </package>
    
    </struts>
    View Code

    web.xml配置:

      <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>

  • 相关阅读:
    混沌的艺术--- YChaos通过数学公式生成混沌图像
    相声段子:How Are You
    太阳崇拜---64幅由算法生成的八芒星图像
    自然的密码---36幅由算法生成的六芒星图像
    雪花六出---几幅算法生成的雪花图像,并祝大家平安夜和圣诞节快乐
    18个分形图形的GIF动画演示
    python的with用法(参考)
    彻底解决django 2.2以上版本与mysql兼容性问题(不用改源码)
    python操作MySQL数据库的三个模块
    MySql 外键约束 之CASCADE、SET NULL、RESTRICT、NO ACTION分析和作用
  • 原文地址:https://www.cnblogs.com/datapool/p/6986711.html
Copyright © 2020-2023  润新知