struts框架编写的用户登陆代码

来源:互联网 发布:js 二维数组定义 编辑:程序博客网 时间:2024/05/21 22:13

1、index.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="x"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <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>
   <form action="login.action" method="post">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    <input type="submit" value="login">
    </form>
  </body>
</html>

2、web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.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>
 </filter-mapping>
</web-app>

3、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>
 <constant name="struts.i18n.encoding" value="UTF-8" />
 <constant name="struts.ui.theme" value="simple"></constant>

 <package name="login" extends="struts-default" namespace="/">

  <action name="login" class="com.why.actions.LoginAction" method="f1">
   <result name="success" type="dispatcher">
    /wel.jsp
   </result>
   <result name="error" type="dispatcher">
    /err.jsp
   </result>
  </action>
 </package>
</struts>

 

 

4、LoginAction.java文件

package com.why.actions;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
 private static final long serialVersionUID = 5362742299006067572L;
 private String username;
 private String password;
 public String f1()
 {
  if("why".equals(username)&&"123".equals(password)){
   return "success";
  }else{
   return "error";
  }
 
 }
 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 
}

 

0 0