无法使用Struts 2重定向jsp文件并显示值 [英] Cannot redirect a jsp file and display values using Struts 2

查看:40
本文介绍了无法使用Struts 2重定向jsp文件并显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的程序,该程序使用文本字段获取用户的名字和姓氏.但是问题在于,当我单击提交"按钮时,无法将其重定向到另一个显示用户名和姓氏的jsp文件.

I have created a simple program that gets the firstname and lastname of a user using a textfield. But the problem is that when I click the submit button I cannot redirect it to another jsp file which shows the firstname and lastname of the user.

这是我的HelloAction类:

package com.novamsc.training.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {

    private String firstName;
    private String lastName;

    public String user(){
        return "hello";
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String display(){
        return "hi";
    }
}

这是我的userInput.jsp文件

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Form</title>
</head>

<body>
    <s:form name="inputData">
    <s:textfield key="firstName"/>
    <s:textfield key="lastName"/>
    <s:submit/>
    </s:form>

</body>

</html>

这是我的resultInput.js文件

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Get Result</title>
</head>

<body>
    <p>
    <s:property value="firstName" />
    </p>
    <p>
        <s:property value="lastName" />
    </p>
</body>

</html>

这是我的struts-traning.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="hello" namespace="/exercise" extends="training-default">

        <action name="userInput" class="com.novamsc.training.struts.action.HelloAction"
            method="user">
            <result name="hello">/jsp/userInput.jsp</result>
        </action>

        <action name="inputData" class="com.novamsc.training.struts.action.HelloAction"
            method="display">
            <result name="hi">/jsp/resultInput.jsp</result>
        </action>
    </package>

</struts>

推荐答案

您应该使用带有附加属性的表单标签,这些附加属性会将表单映射到其他操作.

You should use form tag with additional attributes that map the form to another action.

<s:form name="inputData" namespace="/exercise" action="submitData">
    <s:textfield key="firstName"/>
    <s:textfield key="lastName"/>
    <s:submit/>
</s:form>

然后要执行其他操作,您需要编写操作配置

then for additional action you need to write action config

<action name="submitData" class="com.novamsc.training.struts.action.HelloAction"
            method="save">
            <result type="redirectAction">inputData</result>
</action>

添加其他方法来映射新动作

add additional method to map the new action

public String save(){
        return ActionSupport.SUCCESS;
}


要遵循 PRG(重定向后获取)一个>模式.


This additional action is required to follow PRG (Post-Redirect-Get) pattern.

这篇关于无法使用Struts 2重定向jsp文件并显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆