程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> struts2的全局結果處理和依賴注入案例

struts2的全局結果處理和依賴注入案例

編輯:關於JSP

web.xml     [html]   <?xml version="1.0" encoding="UTF-8"?>   <web-app version="2.5"        xmlns="http://java.sun.com/xml/ns/javaee"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">                     <!-- 添加啟動struts2MVC框架的過濾器 -->       <filter>         <filter-name>struts2</filter-name>         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>       </filter>              <filter-mapping>           <filter-name>struts2</filter-name>           <url-pattern>/*</url-pattern>       </filter-mapping>            <welcome-file-list>       <welcome-file>index.jsp</welcome-file>     </welcome-file-list>   </web-app>     <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">     <!-- 添加啟動struts2MVC框架的過濾器 --> <filter>  <filter-name>struts2</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter>   <filter-mapping>    <filter-name>struts2</filter-name>    <url-pattern>/*</url-pattern> </filter-mapping>     <welcome-file-list>     <welcome-file>index.jsp</welcome-file>   </welcome-file-list> </web-app>   index.jsp      [html]   <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   <%   String path = request.getContextPath();   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   %>      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   <html>       <head>           <base href="<%=basePath%>">              <title>My JSP 'index.jsp' starting page</title>           <meta http-equiv="pragma" content="no-cache">           <meta http-equiv="cache-control" content="no-cache">           <meta http-equiv="expires" content="0">           <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">           <meta http-equiv="description" content="This is my page">           <!--      <link rel="stylesheet" type="text/css" href="styles.css">      -->       </head>          <body>           <div align="center">               <h3>                   登陸操作               </h3>               <form action="${pageContext.request.contextPath }/csdn/login.action" method="post">                   用戶名:                   <input type="text" name="name" />                   <br />                   密碼:                   <input type="password" name="pass" />                   <br />                   <input type="submit" value="登陸">               </form>           </div>       </body>   </html>     <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>">   <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head>   <body> <div align="center"> <h3> 登陸操作 </h3> <form action="${pageContext.request.contextPath }/csdn/login.action" method="post"> 用戶名: <input type="text" name="name" /> <br /> 密碼: <input type="password" name="pass" /> <br /> <input type="submit" value="登陸"> </form> </div> </body> </html>   sc.jsp      [html]   <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>   <%   String path = request.getContextPath();   String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   %>      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   <html>     <head>       <base href="<%=basePath%>">              <title>My JSP 'sc.jsp' starting page</title>              <meta http-equiv="pragma" content="no-cache">       <meta http-equiv="cache-control" content="no-cache">       <meta http-equiv="expires" content="0">           <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">       <meta http-equiv="description" content="This is my page">       <!--      <link rel="stylesheet" type="text/css" href="styles.css">      -->        </head>          <body>            <div align="center">                <h3>登陸成功!</h3>            </div>     </body>   </html>     <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>   <head>     <base href="<%=basePath%>">          <title>My JSP 'sc.jsp' starting page</title>      <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0">     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->     </head>      <body>          <div align="center">              <h3>登陸成功!</h3>          </div>   </body> </html>   LoginAction.java      [java]   package www.csdn.struts_action.action;      import com.opensymphony.xwork2.ActionSupport;      public class LoginAction extends ActionSupport {          /**       *        */       private static final long serialVersionUID = 1L;          // 接受客戶端的值           private String name;       private String pass;              private String upload;          public String getName() {           return name;       }          // 注入        public void setName(String name) {           this.name = name;       }          public String getPass() {           return pass;       }          public void setPass(String pass) {           this.pass = pass;       }                        public String getUpload() {           return upload;       }          public void setUpload(String upload) {           this.upload = upload;       }          public String login() {              System.out.println("處理業務....."+name+"--"+pass+"---"+upload);           return SUCCESS;       }      }     package www.csdn.struts_action.action;   import com.opensymphony.xwork2.ActionSupport;   public class LoginAction extends ActionSupport {   /** *  */ private static final long serialVersionUID = 1L;   // 接受客戶端的值   private String name; private String pass;   private String upload;   public String getName() { return name; }   // 注入 public void setName(String name) { this.name = name; }   public String getPass() { return pass; }   public void setPass(String pass) { this.pass = pass; }       public String getUpload() { return upload; }   public void setUpload(String upload) { this.upload = upload; }   public String login() {   System.out.println("處理業務....."+name+"--"+pass+"---"+upload); return SUCCESS; }   }   struts.xml      [html]   <?xml version="1.0" encoding="UTF-8"?>   <!DOCTYPE struts PUBLIC       "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"       "http://struts.apache.org/dtds/struts-2.3.dtd">   <struts>          <!--           ${pageContext.request.contextPath }/csdn/login.action              url:http://localhost:8080/struts_action/csdn/login.action           struts-default 包名       -->             <package name="user" namespace="/csdn" extends="struts-default">                 <!-- 全局的結果集 -->           <global-results>               <result name="success" type="dispatcher">/sc.jsp</result>           </global-results>              <!--               <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />               Method=execute           -->           <action name="login" class="www.csdn.struts_action.action.LoginAction"               method="login">               <param name="upload">/images</param>               <!--                   name="success" <result-type name="dispatcher"                   class="org.apache.struts2.dispatcher.ServletDispatcherResult"                   default="true"/>               -->               <result name="success" type="dispatcher">/index.jsp</result>           </action>          </package>         </struts>     <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>   <!-- ${pageContext.request.contextPath }/csdn/login.action   url:http://localhost:8080/struts_action/csdn/login.action struts-default 包名 -->     <package name="user" namespace="/csdn" extends="struts-default">     <!-- 全局的結果集 --> <global-results> <result name="success" type="dispatcher">/sc.jsp</result> </global-results>   <!-- <default-class-ref class="com.opensymphony.xwork2.ActionSupport" /> Method=execute --> <action name="login" class="www.csdn.struts_action.action.LoginAction" method="login"> <param name="upload">/images</param> <!-- name="success" <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> --> <result name="success" type="dispatcher">/index.jsp</result> </action>   </package>     </struts>[html] view plaincopyprint?        當沒有     [html]  www.2cto.com <result name="success" type="dispatcher">/index.jsp</result>     <result name="success" type="dispatcher">/index.jsp</result>跳轉到sc頁面,    添加上     [html]   <result name="success" type="dispatcher">/index.jsp</result>     <result name="success" type="dispatcher">/index.jsp</result>則跳轉到index頁面

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved