java中struts配置
1.了解struts
Struts2框架中核心組件就是Action、攔截器等,Struts2框架使用包來(lái)管理Action和攔截器等。每個(gè)包就是多個(gè)Action、多個(gè)攔截器、多個(gè)攔截器引用的集合。
在struts.xml文件中package元素用于定義包配置,每個(gè)package元素定義了一個(gè)包配置。它的常用屬性有:
l name:必填屬性,用來(lái)指定包的名字。
l extends:可選屬性,用來(lái)指定該包繼承其他包。繼承其它包,可以繼承其它包中的Action定義、攔截器定義等。
l namespace:可選屬性,用來(lái)指定該包的命名空間。
2.配置struts
首先新建一個(gè)web項(xiàng)目,在右擊一個(gè)項(xiàng)目,選擇myeclipse下add struts
在選擇 struts2.1 單擊下一步在選擇自己所需要的包 在保存
3.修改用戶登錄驗(yàn)證示例,多增加一個(gè)注冊(cè)用戶功能。
1. 修改Action類:
package org.qiujy.web.struts2.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; /** *@authorqiujy *@version1.0 */ publicclass LoginAction extends ActionSupport{ private String userName; private String password; private String msg; //結(jié)果信息屬性 /** *@returnthemsg */ public String getMsg() { returnmsg; } /** *@parammsgthemsgtoset */ publicvoid setMsg(String msg) { this.msg = msg; } /** *@returntheuserName */ public String getUserName() { returnuserName; } /** *@paramuserNametheuserNametoset */ publicvoid setUserName(String userName) { this.userName = userName; } /** *@returnthepassword */ public String getPassword() { returnpassword; } /** *@parampasswordthepasswordtoset */ publicvoid setPassword(String password) { this.password = password; } /** *處理用戶請(qǐng)求的login()方法 *@return結(jié)果導(dǎo)航字符串 *@throwsException */ public String login() throws Exception{ if("test".equals(123) && "test".equals(123)){ msg = "登錄成功,歡迎" + 123; //獲取ActionContext實(shí)例,通過(guò)它來(lái)訪問(wèn)Servlet API ActionContext context = ActionContext.getContext(); //看session中是否已經(jīng)存放了用戶名,如果存放了:說(shuō)明已經(jīng)登錄了; //否則說(shuō)明是第一次登錄成功 if(null != context.getSession().get("uName")){ msg = this.userName + ":你已經(jīng)登錄過(guò)了!!!"; }else{ context.getSession().put("uName", this.userName); } returnthis.SUCCESS; }else{ msg = "登錄失敗,用戶名或密碼錯(cuò)"; returnthis.ERROR; } } public String regist() throws Exception{ //將用戶名,密碼添加到數(shù)據(jù)庫(kù)中 //... msg = "注冊(cè)成功。"; returnthis.SUCCESS; } }
2. struts.xml文件:沒(méi)有什么變化,跟以前一樣配置
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="my" extends="struts-default" namespace="/manage"> <!-- 定義處理請(qǐng)求URL為login.action的Action --> <action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction"> <!-- 定義處理結(jié)果字符串和資源之間的映射關(guān)系 --> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
3. 頁(yè)面:
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用戶登錄頁(yè)面</title> </head> <body> <h2>用戶入口</h2> <hr> <form action="manage/userOpt!login.action" method="post"> <table border="1"> <tr> <td>用戶名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 確定 "/> </td> </tr> </table> </form> </body> </html>
regist.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <html> <head> <title>用戶注冊(cè)頁(yè)面</title> </head> <body> <h2>用戶注冊(cè)</h2> <hr> <form action="manage/userOpt!regist.action" method="post"> <table border="1"> <tr> <td>用戶名:</td> <td><input type="text" name="userName"/></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value=" 注冊(cè) "/> </td> </tr> </table> </form> </body> </html>
現(xiàn)在就可以使用sturts。
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- Struts2的s:radio標(biāo)簽使用及用jquery添加change事件
- Struts之logic標(biāo)簽庫(kù)詳解
- jsp struts1 標(biāo)簽實(shí)例詳解
- java中struts 框架的實(shí)現(xiàn)
- java中struts2實(shí)現(xiàn)文件上傳下載功能實(shí)例解析
- struts2+jquery實(shí)現(xiàn)ajax登陸實(shí)例詳解
- struts2中一個(gè)表單中提交多個(gè)請(qǐng)求的例子(多個(gè)提交按鈕)
- struts2單個(gè)文件上傳的兩種實(shí)現(xiàn)方式
- Java(基于Struts2) 分頁(yè)實(shí)現(xiàn)代碼
- struts2的select標(biāo)簽用法實(shí)例分析
相關(guān)文章
新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目(圖文教程)
這篇文章主要介紹了新版本IntelliJ IDEA 構(gòu)建maven,并用Maven創(chuàng)建一個(gè)web項(xiàng)目的圖文教程,需要的朋友可以參考下2018-01-01Spring boot如何集成kaptcha并生成驗(yàn)證碼
這篇文章主要介紹了Spring boot如何集成kaptcha并生成驗(yàn)證碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Springboot解決ajax+自定義headers的跨域請(qǐng)求問(wèn)題
由于瀏覽器同源策略(同源策略,它是由Netscape提出的一個(gè)著名的安全策略,現(xiàn)在所有支持JavaScript 的瀏覽器都會(huì)使用這個(gè)策略。接下來(lái)通過(guò)本文給大家介紹Springboot如何優(yōu)雅的解決ajax+自定義headers的跨域請(qǐng)求 ,需要的朋友可以參考下2019-05-05spring redis 如何實(shí)現(xiàn)模糊查找key
這篇文章主要介紹了spring redis 如何實(shí)現(xiàn)模糊查找key的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)通用模塊統(tǒng)一返回結(jié)果異常日志處理
這篇文章主要為大家介紹了實(shí)戰(zhàn)分布式醫(yī)療掛號(hào)系統(tǒng)之統(tǒng)一返回結(jié)果統(tǒng)一異常處理,統(tǒng)一日志處理到通用模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04