如何从表单操作调用自定义URL操作? [英] How to call custome URL action from Form action?

查看:124
本文介绍了如何从表单操作调用自定义URL操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这篇文章并创建了一个自定义URL应用程序。该操作被调用,但网址显示会话ID,如

I followed this post and created a custom URL application. The action is getting called but the url shows with session id like

http:// localhost:8080 / CustomURL%7Busername%7D.action; jsessionid = 9C1FB3EB633209C18625BBB40EA61000

我想要像 http:// localhost:8080 / CustomURL / rajesh

查看我的Struts.xml

See my Struts.xml

<struts>
<constant name="struts.mapper.alwaysSelectFullNamespace"
    value="false" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.patternMatcher" value="namedVariable" />
<package name="default" namespace="/" extends="struts-default">
    <action name="">
        <result name="success">home.jsp</result>
    </action>

    <action name="{username}" class="com.rajesh.struts2.CustomURL"
        method="customUrl">
        <result name="success">welcome.jsp</result>
    </action>

</package>

查看我的jsp页面

<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 Custom URL</title>
</head>
<body>
    <h1>Struts 2 Custom URL</h1>
    <h3>Enter your name below</h3>
    <s:form action="{username}">
        <s:textfield name="username" />
        <s:submit />
    </s:form>
</body>
</html>

请参阅下面的java文件。

See java file below.

public class CustomURL extends ActionSupport {

    private String username;

    public String getUsername() {
        System.out.println("Getter");
        return username;
    }

    public void setUsername(String username) {
        System.out.println("Setter");
        this.username = username;
    }

    private static final long serialVersionUID = -4337790298641431230L;

    public String customUrl() {
        return SUCCESS;
    }
}

请提出任何建议。

推荐答案

首先,如果您不希望用户认为他们的名字有扩展名,您应该摆脱行动扩展。

First of all you should get rid of action extension, if you don't want user to think their name has an extension.

<constant name="struts.action.extension" value=",,action"/> 

接下来模式匹配器应为 regex

Next the pattern matcher should be regex.

<constant name="struts.patternMatcher" value="regex"/>

行动映射

<action name="/CustomURL/{username}" class="com.rajesh.struts2.CustomURL" method="customUrl">
    <result name="success">welcome.jsp</result>
</action>

在JSP中,您不需要使用表单标签,但锚标签。并使用已知名称。

In the JSP you don't need to use form tag, but anchor tag. And use known names.

<a href="http://localhost:8080/CustomURL/rajesh">Click my name</a>

这篇关于如何从表单操作调用自定义URL操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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