欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

struts2的全局结果处理和依赖注入案例

程序员文章站 2022-03-24 23:53:10
web.xml     [html]  
web.xml

 

 

[html]  

<?xml version="1.0" encoding="utf-8"?>  

<web-app version="2.5"   

    xmlns="https://java.sun.com/xml/ns/javaee"   

    xmlns:xsi="https://www.w3.org/2001/xmlschema-instance"   

    xsi:schemalocation="https://java.sun.com/xml/ns/javaee   

    https://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.</welcome-file>  

  </welcome-file-list>  

</web-app>  

 

<?xml version="1.0" encoding="utf-8"?>

<web-app version="2.5" 

xmlns="https://java.sun.com/xml/ns/javaee" 

xmlns:xsi="https://www.w3.org/2001/xmlschema-instance" 

xsi:schemalocation="https://java.sun.com/xml/ns/javaee 

https://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>  

        <p 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>  

        </p>  

    </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>

<p 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>

</p>

</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>  

         <p align="center">  

             <h3>登陆成功!</h3>  

         </p>  

  </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>

         <p align="center">

             <h3>登陆成功!</h3>

         </p>

  </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"  

    "https://struts.apache.org/dtds/struts-2.3.dtd">  

<struts>  

  

    <!--  

        ${pagecontext.request.contextpath }/csdn/login.action  

  

        url:https://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"

"https://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

 

<!--

${pagecontext.request.contextpath }/csdn/login.action

 

url:https://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页面