struts學習筆記(二)

使用struts2建一個簡單的登錄頁面。

   首先配置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>

    <constant name="struts.devMode" value="false" />

    <!-- Add packages here -->

 

    <package name="user" extends="struts-default" >

       <action name="login" class="com.login.action.LoginAction" method="login">

           <result name="success">/loginsuccess.jsp</result>

           <result name="input">/loginfail.jsp</result>

       </action>

    </package>

</struts>

 

   寫對應的action,在src目錄下建一個包,名爲com.login.action,在裏面建立一個類

public class LoginAction extends ActionSupport{

    User u;

    UserService us = new UserService();

   

    public User getU() {

       return u;

    }

 

    public void setU(User u) {

       this.u = u;

    }

 

    public UserService getUs() {

       return us;

    }

 

    public void setUs(UserService us) {

       this.us = us;

    }

 

    public String login(){

       if(us.login(u))

           return SUCCESS;

       else

           return INPUT;

    }

   

}

 

Com.login.db下的類如下:

public class DB {

    public static Connection getCon(){

       Connection con=null;

       try {

           Class.forName("com.mysql.jdbc.Driver");

           String url="jdbc:mysql://localhost/login?user=root&password=root";

           con=DriverManager.getConnection(url);

       } catch (ClassNotFoundException e) {

           e.printStackTrace();

       } catch (SQLException e) {

           e.printStackTrace();

       }

       return con;

    }

   

    public static PreparedStatement prepare(Connection con,String sql){

       PreparedStatement ps=null;

       try {

           ps=con.prepareStatement(sql);

       } catch (SQLException e) {

           e.printStackTrace();

       }

       return ps;

    }

   

    public static void close(Connection con){

       try {

           if(con != null) {

              con.close();

              con = null;

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }

    }

   

    public static void close(Statement stmt){

       try {

           if(stmt != null) {

              stmt.close();

              stmt = null;

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }

    }

 

    public static void close(ResultSet rs){

       try {

           if(rs != null) {

              rs.close();

              rs = null;

           }

       } catch (SQLException e) {

           e.printStackTrace();

       }

    }

}

 

Com.login.model下的類如下:

 

public class User {

    private String username;

    private String password;

    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;

    }

   

}

com.login.service下的類如下:

public class UserService {

    public boolean login(User u){

       Connection con = DB.getCon();

       String sql = "select * from login where username='+u.getUsername()+' and password='+u.getPassword()+'";

       PreparedStatement ps = DB.prepare(con, sql);

       try {

           ResultSet rs = ps.executeQuery();

           if(rs.next())

              return true;

       } catch (SQLException e) {

           e.printStackTrace();

       }

       return false;

    }

}

 

相應的index.jsp文件如下:

 

<!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>

    <form action="login.action">

    username:<input name="u.username" type="text" size=15>

    password:<input name="u.password" type="password" size=15>

   

    <input type="submit" value="login">

    <input type="reset" value="reset">

    </form>

  </body>

</html>

loginfail.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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>

    login fail! <br>

  </body>

</html>

loginsuccess.jsp如下:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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>

    login success! <br>

  </body>

</html>

如將以上文件拷貝到你的tomcatwebapps目錄下面,部署好,如果不會配置,請看我的筆記一,然後啓動你的tomcat

如果登錄不上,請讀者自己修改db類中的數據庫連接的語句,與你自己所在的機子的mysql密碼,以及數據庫想匹配上才能夠正確登錄。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章