struts框架實現web登陸介面?

Tags: 介面, 框架,

struts2框架實現了MVC模式,簡化了顯示資料介面的編輯,方便開發人員開發。

工具/原料

mysql5.0

eclipse

方法/步驟

在eclipse中建立一個新的Bynamic WEB PROJECT專案。並建立兩個包:com.action\ com.db.

com.action包中存放所有的Action類

com.db包存放與資料庫操作相關的類

在src節點上新建兩個檔案struts.xml,和struts.properties。

在struts.properties中寫入下面內容:

struts.locale=zh_CN

struts.i18n.encoding=GBK

在struts.xml中編寫如下內容:

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

";>

/error.jsp

/index.jsp

login.jsp

在web.xml中如下內容:

strutsstudentmanager

struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

struts2

/*

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

編寫login.jsp頁面:

<%@ page language="java" contentType="text/html; charset=gb2312"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

struts使用者登入




使用者登入

請輸入使用者名稱:

請輸入密碼:
&nbsp;&nbsp;

在com.db包中新增新類:DBConn.java

package com.db;

import java.sql.Connection;

import java.sql.DriverManager;

public class DBConn {

//得到資料庫連線

public static Connection createConnection(){

try {

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

Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/xsglxt","xsglxt","xsglxt");

return connection;

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

return null;

}

}

//關閉資料庫連線

public static void close(Connection connection){

try {

connection.close();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

在com.action中新增新類:LoginAction.java

package com.action;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import com.db.DBConn;

import com.opensymphony.xwork2.ActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

/**

*

*/

private static final long serialVersionUID = 1L;

public String adminusername;

public String adminuserpassword;

public String action;//操作型別

public String errormsg;//錯誤訊息

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

if("login".equals(action)){

try {Connection conn=DBConn.createConnection();

String sqlString="select * from adminuser where Adminusername=? "+

"and Adminuserpassword=?";

PreparedStatement statement=conn.prepareStatement(sqlString);

statement.setString(1,adminusername);

statement.setString(2, adminuserpassword);

ResultSet rSet=statement.executeQuery();

if(rSet.next()){//如果使用者名稱和密碼正確

ActionContext.getContext().getSession().put("adminusername", adminusername);

ActionContext.getContext().getSession().put("adminuserpassword", adminuserpassword);

ActionContext.getContext().getSession().put("adminuserrole", rSet.getString("Adminuserrole"));

DBConn.close(conn);

return SUCCESS;

}

else {

errormsg=new String("使用者名稱或者密碼錯誤");

}

DBConn.close(conn);

} catch (Exception e) {

// TODO: handle exception

errormsg=new String("資料庫連線錯誤");

}

}

return INPUT;

}

}

效果圖:

struts框架實現web登陸介面

struts框架實現web登陸介面

struts框架實現web登陸介面

struts框架實現web登陸介面

注意事項

如果親愛的讀者喜歡本經驗,請點個贊吧

如果你是javaweb開發學習者,那麼請訂閱本經驗吧!會讓你少走彎路!!!!!

介面, 框架,
相關問題答案