redirect-action

原文链接: http://www.cnblogs.com/c0liu/p/5493283.html

功能:

点击login , redirect 到hello.jsp 显示 "hello"

点击redirect, redirect 到 error.jsp 显示 "error"

1.创建Struts2 工程

2.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <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>

3.index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <s:a action="login">login</s:a><br/>
    <s:a action="login!redirect.action">redirect</s:a> <!--此处有bug-->
</body>
</html>

4.LoginAction.java

package com.action;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private static final long serialVersionUID=1L;
    public String execute() throws Exception{
        return SUCCESS;
    }
    public String redirect() throws Exception{
        return ERROR;
    }
}

5.UserAction.java

package com.action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
    private static final long serialVersionUID=1L;
    public String execute() throws Exception{
        return SUCCESS;
    }
}

6.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="default" extends="struts-default">
        <action name="login" class="com.action.LoginAction">
            <result type="redirectAction">
                <param name="actionName">userLogin</param>
                <param name="namespace">/user</param>
            </result>
            <result name="error" type="redirectAction">error</result>  <!--此处的name为LoginAction.redirect传过来的值ERROR-->
        </action>
        <action name="error">
            <result>/error.jsp</result>
        </action>
    </package>
    <package name="user" extends="struts-default" namespace="/user">
        <action name="userLogin" class="com.action.UserAction">
            <result>
                /hello.jsp
            </result>
        </action>
    </package>
</struts>

7.error.jsp & hello.jsp

<body>
    <center>
        <h2>error</h2>
    </center>
</body>


<body>
    <center>
        <h2>hello</h2>
    </center>
</body>

转载于:https://www.cnblogs.com/c0liu/p/5493283.html

猜你喜欢

转载自blog.csdn.net/weixin_30236595/article/details/94783722
今日推荐