Struts2 Hello World

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

技术分享

 

以上是关于Struts2 Hello World的主要内容,如果未能解决你的问题,请参考以下文章

springMvc的Hello world(xml配置)

Struts2 Hello,Wold

c语言入门到入门,hello world篇

6个变态的C语言Hello World程序 之 雷人的程序语言

Struts2开发基础

Hello World代码