zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    nginx实现请求的负载均衡 + Keep Alive实现nginx的高可用
    理解什么是JWT(Json web token)及Python实现
    TCP/UDP协议到底是什么
    Redis实现分布式单点登录
    Python面试题---给定一个字符串 {xxx[xxx{xxx}]xx{x[xxx]xxx{xxx}xx}x} 判断其中的 {}[]() 是否成对出现
    Typora里面如何快捷改变字体颜色?
    基于Docker安装关系型数据库PostgrelSQL替代Mysql
    PEP8-Python编码规范
    欢迎来到我的友链小屋
    windows下lib和dll区别
  • 原文地址:https://www.cnblogs.com/datapool/p/6986711.html
Copyright © 2011-2022 走看看