Struts 登陆

来源:互联网 发布:ea自动交易软件 编辑:程序博客网 时间:2024/05/16 01:06

1、代码

1.1、web.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  11.     </filter>  
  12.     <filter-mapping>  
  13.         <filter-name>struts2</filter-name>  
  14.         <url-pattern>/*</url-pattern>  
  15.     </filter-mapping>  
  16.    
  17.   <welcome-file-list>  
  18.     <welcome-file>index.jsp</welcome-file>  
  19.   </welcome-file-list>  
  20.   
  21. </web-app>  

1.2、Struts.xml

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.3.dtd">  
  5.   
  6. <struts>  
  7.   
  8.       
  9.       
  10.       
  11.     <!--  原始的,可以用来参考  
  12.       
  13.     <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
  14.     <constant name="struts.devMode" value="true" />  
  15.     <package name="default" namespace="/" extends="struts-default">  
  16.   
  17.         <default-action-ref name="index" />  
  18.   
  19.         <global-results>  
  20.             <result name="error">/error.jsp</result>  
  21.         </global-results>  
  22.   
  23.         <global-exception-mappings>  
  24.             <exception-mapping exception="java.lang.Exception" result="error"/>  
  25.         </global-exception-mappings>  
  26.   
  27.         <action name="index">  
  28.             <result type="redirectAction">  
  29.                 <param name="actionName">HelloWorld</param>  
  30.                 <param name="namespace">/example</param>  
  31.             </result>  
  32.         </action>  
  33.     </package>  
  34.      -->  
  35.        
  36.        
  37.        
  38.      <constant name="struts.devMode" value="true" />  
  39.     <!-- 如果value=true 开启开发模式,可以改完xml里面的东西就能自动刷新,不需要重新部署服务器 -->  
  40.           
  41.     <package name="default"  extends="struts-default">  
  42.         <action name="LoginAction_2014_4_28" class="com.god.action.LoginAction_2014_4_28">  
  43.             <result name="success">/2014_4_28_welcome_lx_01.jsp</result>  
  44.             <result name="input">/2014_4_28_login_lx_01.jsp</result>  
  45.         </action>  
  46.     </package>  
  47.       
  48.     <include file="example.xml"/>  
  49.     <!-- Add packages here -->  
  50. </struts>  

1.3、两个页面代码

1.3.1、2014_4_28_login_lx_01.jsp

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP '2014_4_28_login_lx_01.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <center>  
  27.         <div>  
  28.               
  29.         </div>  
  30.         <h3>这是一个简单的Struts 2应用</h3>  
  31.         <br/><hr/>  
  32.         <form  action="LoginAction_2014_4_28.action" method="post">  
  33.             用户名:<input name="uname" type="text"/><br/>  
  34.             密码:<input name="upasswd" type="text"/><br/>  
  35.             <input type="submit" value="提交"/>   
  36.         </form>  
  37.     </center>  
  38.   </body>  
  39. </html>  

1.3.1、2014_4_28_welcome_lx_01.jsp

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  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 '2014_4_28_welcome_lx_01.jsp' starting page</title>  
  14.       
  15. <span style="white-space:pre">  </span><meta http-equiv="pragma" content="no-cache">  
  16. <span style="white-space:pre">  </span><meta http-equiv="cache-control" content="no-cache">  
  17. <span style="white-space:pre">  </span><meta http-equiv="expires" content="0">      
  18. <span style="white-space:pre">  </span><meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  19. <span style="white-space:pre">  </span><meta http-equiv="description" content="This is my page">  
  20. <span style="white-space:pre">  </span><!--  
  21. <span style="white-space:pre">  </span><link rel="stylesheet" type="text/css" href="styles.css">  
  22. <span style="white-space:pre">  </span>-->  
  23.   
  24.   
  25.   </head>  
  26.     
  27.   <body><center>  
  28.   <span style="white-space:pre">   </span><div>  
  29.   <span style="white-space:pre">       </span>  
  30.   <span style="white-space:pre">   </span></div>  
  31.   <span style="white-space:pre">   </span><h3>一个简单的Struts 2应用</h3>  
  32.   <span style="white-space:pre">   </span><br/><hr/>  
  33.   <span style="white-space:pre">   </span>${uname}欢迎您!  
  34.   </center>  
  35.     
  36.     
  37.   </body>  
  38. </html>  

1.4action代码

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package com.god.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class LoginAction_2014_4_28 extends ActionSupport {  
  6.     org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter d;  
  7.     private String uname;  
  8.     private String upasswd;  
  9.     public String getUname() {  
  10.         return uname;  
  11.     }  
  12.     public void setUname(String uname) {  
  13.         this.uname = uname;  
  14.     }  
  15.     public String getUpasswd() {  
  16.         return upasswd;  
  17.     }  
  18.     public void setUpasswd(String upasswd) {  
  19.         this.upasswd = upasswd;  
  20.     }  
  21.       
  22.       
  23.     public String execute(){// 类似于servlet的doGet和doPost方法  
  24.         if(uname.equals("fang")&&upasswd.equals("123")){  
  25.             return SUCCESS; //这两个return的东西要继承ActionSupport之后才能使用。  
  26.         }else{  
  27.             return INPUT;  
  28.         }  
  29.     }  
  30. }  

2、效果截图

2.1、2014_4_28_login_lx_01.jsp


2.2、2014_4_28_welcome_lx_01.jsp





0 0
原创粉丝点击