struts2入门实例

  在MyEclipse下创建一个Web Project,名为Struts2_1。在项目名称上右键MyEclipse->Project Facets->Install Apache Facets(Struts2) ,则可添加struts2,会在src目录下生成 struts.xml文件。
  在src目录下创建class类LoginAction.java,包路径为com.study.struts.action,代码如下:

package com.study.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String account;
    private String password;

    public String execute() {
        if ("abc".equals(account) && "123".equals(password)) {
            return SUCCESS;
        }
        return ERROR;
    }

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

}

  在WebRoot下创建login.jsp,success.jsp和error.jsp

login.jsp文件如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="struts" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'login.jsp' starting page</title>

    <%--  <struts:head theme="ajax" /> --%>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <struts:form action="loginPerson">
        <struts:label value="登录系统"/>
        <struts:textfield name="account" label="账号"/>
        <struts:password name="password" label="密码"/>
        <struts:submit value="登陆"/>
    </struts:form>
  </body>
</html>

  success.jsp 和error.jsp分别显示登陆成功和登陆失败

struts.xml文件如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="main" extends="struts-default">
        <global-results>
            <result name="login">/login.jsp</result>
        </global-results>

        <action name="loginPerson" class="com.study.struts.action.LoginAction">
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>    

web.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Struts_1</display-name>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  <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>  <!-- 截获所有的url -->
  </filter-mapping>
</web-app>

  启动tomcat服务器,并部署struts2_1应用,就可以在浏览器输入http://localhost:8080/Struts_1访问,首先会进入登录页面,登陆成功则跳转到success.jsp页面显示欢迎,失败则跳转到error.jsp页面,显示用户名或密码错误。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值