码迷,mamicode.com
首页 > 其他好文 > 详细

redirect-action

时间:2016-05-14 21:27:45      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

功能:

 

技术分享

点击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>

 

redirect-action

标签:

原文地址:http://www.cnblogs.com/c0liu/p/5493283.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!