How to call custome URL action from Form action?(如何从表单操作中调用客户 URL 操作?)
问题描述
我关注了 这篇文章 并创建了一个自定义 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
:
<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 文件:
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 中你不需要使用form
标签,而是使用anchor 标签.并使用已知名称.
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 操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从表单操作中调用客户 URL 操作?


基础教程推荐
- Java Swing计时器未清除 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01