欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

J2EE学习笔记——Struts2多方法实现(1)(2)

来源: javaer 分享于  点击 4090 次 点评:174

Struts.xml

[html] view plaincopy
  1. <?xmlversionxmlversion="1.0"encoding="UTF-8"?> 
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 
  3. <struts> 
  4.    
  5.    
  6.      <packagenamepackagename="default"extends="struts-default"> 
  7.          
  8.         <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  9.             <resultnameresultname="success">/Success.jsp</result> 
  10.              <resultnameresultname="error">/Login.jsp</result> 
  11.                   
  12.         </action> 
  13.            
  14.            
  15.         <actionnameactionname="regAction"class="xuyan.com.action.LoginAction"method="Login"> 
  16.             <resultnameresultname="success">/RegSuccess.jsp</result> 
  17.              <resultnameresultname="error">/reg.jsp</result> 
  18.                   
  19.         </action> 
  20.            
  21.     </package> 
  22.    
  23.    
  24.    
  25.    
  26. </struts> 

Reg.jsp:

[html] view plaincopy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
  2. <%@ taglib uri="/struts-tags" prefix="s" %> 
  3. <
  4. String path = request.getContextPath(); 
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
  6. %> 
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
  9. <html> 
  10.   <head> 
  11.     <base href="<%=basePath%>"> 
  12.      
  13.     <title>My JSP 'reg.jsp' starting page</title> 
  14.      
  15.     <meta http-equiv="pragma" content="no-cache"> 
  16.     <meta http-equiv="cache-control" content="no-cache"> 
  17.     <meta http-equiv="expires" content="0">     
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 
  19.     <meta http-equiv="description" content="This is my page"> 
  20.      
  21.  
  22.   </head> 
  23.    
  24.   <body> 
  25.       <s:form  action="regAction" method="post"> 
  26.       
  27.         <s:textfield  name="username" label="用户名" /> <br> 
  28.          
  29.          <s:password name="password"  label="密码"/> <br> 
  30.           
  31.            <input type="submit" value="注册"> <br> 
  32.       
  33.      </s:form> 
  34.   </body> 
  35. </html> 

注意:  

[html] view plaincopy
  1.  <actionnameactionname="loginAction"class="xuyan.com.action.LoginAction"> 
  2.  
  3.  loginAction与login. JSP页面中的<s:form  action="loginAction" method="post">对应 
  4.  
  5.  
  6. <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  7.  
  8.    regAction与reg.JSP 页面中的 <s:form  action="regAction" method="post">对应 
  9.  
  10.  
  11.    loginAction  类中public String Login()方法必须在Struts.xml中声明 
  12.  
  13.  <action name="regAction" class="xuyan.com.action.LoginAction" method="Login"> 
  14.          <result name="success">/RegSuccess.jsp</result> 
  15.           <result name="error">/reg.jsp</result> 
  16.              
  17.      </action> 


相关栏目:

用户点评