struts入门案例 登录

来源:互联网 发布:php 替换字符串 某一段 编辑:程序博客网 时间:2024/06/06 03:57

struts入门案例

1、首先导入需要的jar包

commons-fileupload-1.2.2.jar      【文件上传相关包】

commons-io-2.0.1.jar

struts2-core-2.3.4.1.jar           【struts2核心功能包】

xwork-core-2.3.4.1.jar           【Xwork核心包】

ognl-3.0.5.jar                         【Ognl表达式功能支持表】

commons-lang3-3.1.jar         【struts对java.lang包的扩展】

freemarker-2.3.19.jar           【struts的标签模板库jar文件】

javassist-3.11.0.GA.jar          【struts对字节码的处理相关jar】

2、配置web.xml文件,添加struts核心过滤器

<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-core-2.3.4.1.jar

StrutsPrepareAndExecuteFilter  即为核心过滤器

注意:

 使用的struts的版本不同,核心过滤器类是不一样的!

3、开始开发

首先创建3个展示页面 jsp

--------  fail.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.jsp' starting page</title>
    <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>
    登录失败
  </body>
</html>

---------  login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.jsp' starting page</title>
    <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>
    <s:form action="login">
        <s:textfield name="nameString" key="user"> </s:textfield>
        <s:password name="passString" key="pass"></s:password>
        <s:submit key="login"></s:submit>
    </s:form>
  </body>
</html>

---------  loginSuccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%
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 'index.jsp' starting page</title>
    <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>
    <s:text name="">${sessionScope.successTip } 登陆成功</s:text>
  </body>
</html>


4、创建action类

HelloAction.java

package test;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class helloAction extends ActionSupport {//继承 ActionSupport

   private String nameString;//用户名,此处要与login.jsp中的name属性一致
    private String passString;//密码,此处要与login.jsp中的name属性一致

// 属性的setter getter 方法
    public String getNameString() {
        return nameString;
    }
    public void setNameString(String nameString) {
        this.nameString = nameString;
    }
    public String getPassString() {
        return passString;
    }
    public void setPassString(String passString) {
        this.passString = passString;
    }

//自定义的登录方法,注意此处必须返回字符串,作为跳转结果的name属性值

    public String loginMethod() throws Exception {
        if (getNameString().equals("xiaoming") && getPassString().equals("123")) {
            ActionContext.getContext().getSession().put("successTip", getNameString());
            return"success";
        }else {
            return "fail" ;
        }
    }

    
}

5、配置struts.xml文件

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

    <package name="fdssfd" extends="struts-default">// name属性随便写,但是为了便于区分,最好写当前action所在的package包名  ,extends继承apach的struts XML文件
        <action name="login" class="test.helloAction" method="loginMethod">//name为form表单中的action名字,class为创建的action类(全名:包名+类名),loginMethod为要调用的方法名
            <result name="success">/loginSuccess.jsp</result>//success为action类返回的String  ,   /loginSuccess.jsp为跳转的页面
            <result name="fail">/fail.jsp</result>
        </action>
    </package>
</struts>

6、运行结果

运行Tomcat ,输入http://localhost:8000/strust/login.jsp,然后输入username-->xiaoming,password --> 123,显示xiaoming登陆成功  ,否则登录失败