java中struts設置裝備擺設。本站提示廣大學習愛好者:(java中struts設置裝備擺設)文章只能為提供參考,不一定能成為您想要的結果。以下是java中struts設置裝備擺設正文
1.懂得struts
Struts2框架中焦點組件就是Action、攔阻器等,Struts2框架應用包來治理Action和攔阻器等。每一個包就是多個Action、多個攔阻器、多個攔阻器援用的聚集。
在struts.xml文件中package元素用於界說包設置裝備擺設,每一個package元素界說了一個包設置裝備擺設。它的經常使用屬性有:
l name:必填屬性,用來指定包的名字。
l extends:可選屬性,用來指定該包繼續其他包。繼續其它包,可以繼續其它包中的Action界說、攔阻器界說等。
l namespace:可選屬性,用來指定該包的定名空間。
2.設置裝備擺設struts
起首新建一個web項目,在右擊一個項目,選擇myeclipse下add struts
在選擇 struts2.1 單擊下一步在選擇本身所須要的包 在保留
3.修正用戶登錄驗證示例,多增長一個注冊用戶功效。
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; //成果信息屬性
/**
*@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;
}
/**
*處置用戶要求的login()辦法
*@return成果導航字符串
*@throwsException
*/
public String login() throws Exception{
if("test".equals(123) && "test".equals(123)){
msg = "登錄勝利,迎接" + 123;
//獲得ActionContext實例,經由過程它來拜訪Servlet API
ActionContext context = ActionContext.getContext();
//看session中能否曾經寄存了用戶名,假如寄存了:解釋曾經登錄了;
//不然解釋是第一次登錄勝利
if(null != context.getSession().get("uName")){
msg = this.userName + ":你曾經登錄過了!!!";
}else{
context.getSession().put("uName", this.userName);
}
returnthis.SUCCESS;
}else{
msg = "登錄掉敗,用戶名或暗碼錯";
returnthis.ERROR;
}
}
public String regist() throws Exception{
//將用戶名,暗碼添加到數據庫中
//...
msg = "注冊勝利。";
returnthis.SUCCESS;
}
}
2. struts.xml文件:沒有甚麼變更,跟之前一樣設置裝備擺設
<!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">
<!-- 界說處置要求URL為login.action的Action -->
<action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 界說處置成果字符串和資本之間的映照關系 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
3. 頁面:
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>用戶登錄頁面</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>用戶注冊頁面</title>
</head>
<body>
<h2>用戶注冊</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=" 注冊 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
如今便可以應用sturts。
以上所述就是本文的全體內容了,願望年夜家可以或許愛好。