最最最简单的框架基础,,,struts2框架解决简单的用户登陆……

来源:互联网 发布:高分二号数据预处理 编辑:程序博客网 时间:2024/06/05 00:36

最近在学习struts框架,,所以只会基础性的问题解决,,现在就带大家来看看基础性的登陆功能(用户名和密码是订死的),只是通过这个来了解简单的struts框架的原理:
首先就是登陆界面,,同样就是简单的jsp页面:
啥也不说了,,上代码:
login.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 'login.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>      //简单的form表单,将登陆信息传到后台action处理    <form method="post" action="login.action">        username:<input name="username" /><br />        userpass:<input name="userpass" /><br />        <input type="submit" value="登陆"/><br />    </form>  </body></html>

后台action接收前台传过来的数据,LoginActon.java
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

package com.ruide.action;public class LoginAction {    //前台的数据    //用户名    private String username;    //用户密码    private String userpass;    //提供对应的get和set方法    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getUserpass() {        return userpass;    }    public void setUserpass(String userpass) {        this.userpass = userpass;    }    //action的核心方法,,调用action方法处理前台的数据    public String execute(){        System.out.println("调用了execute方法。。。");        System.out.println(username);        System.out.println(userpass);        //在这里我们是把用户名和密码写死的,只是见的的代码实现而已。        //规定用户名和密码分别为aaa和123        if (username.equals("aaa")&&userpass.equals("123")){            //正确的话直接返回"success"            return "success";        }else {            //验证失败的话直接就返回"fail"            return "fail";        }    }}

配置struts.xml实现jsp和action之间的链接
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.3.dtd"><struts>    <package name="jjj" namespace="/" extends="struts-default">        <action name="login" class="com.ruide.action.LoginAction">            <result name="success">/savedept.jsp</result>            <result name="fail">/index.jsp</result>        </action>       </package></struts>

至于struts框架的一些配置web.xml还有一些jar包在这里就不一一赘述 了。

原创粉丝点击