JSP 如何在Struts 2中创建动态URL [replicate]

iqxoj9l9  于 2023-02-06  发布在  其他
关注(0)|答案(2)|浏览(135)
    • 此问题在此处已有答案**:

How to create custom URL in Struts 2? Like www.twitter.com/goodyzain(2个答案)
5年前关闭。
我已经创建了表单login.jsp,当用户输入用户名和密码,然后我想创建像这样的URL:/companyName/username.
示例:如果公司名称为abc,用户名为parth1,则URL为
"http://localhost:8080/Temp_Central_Emp/abc/parth1/afterLogin.jsp"
我根据用户名从数据库中获取companyName

    • 文件:login.jsp:**
<s:form action="afterLogin" method="post">
    <s:textfield label="Enter username" key="userId" maxlength="5" size="30" id="userId"/>
    <s:password label="Enter password" key="userPsw" size="30" />
    <tr>
        <td></td>
        <td>
            <img src="<c:url value='simple-captcha.png' />" />
            <br />
            <p>Press F5 for refresh.</p>
        </td>
    </tr>
    <s:textfield label="Enter code" key="captchaResponse" size="30" />
    <s:submit value="Login" />
</s:form>
    • 文件:struts.xml:**
<struts>
<constant name="struts.enable.SlashesInActionNames" value="true"/> 
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.devMode" value="true" />
<package name="a" extends="struts-default">
 
    <action name="afterLogin" class="com.parth.LoginAction">
        <result name="success">/afterLogin.jsp</result>
    </action>

</package>
    • 文件:LoginAction.java:**
public class LoginAction extends ActionSupport{

private Map<String, Object> session;
private String userId;
private String userPsw;
private String url;

private static final long serialVersionUID = 1L;

@Override
@Action(value="{url}/afterLogin")
public String execute() throws Exception {      
    // business logic to insert user into database
    url = "/companyName/" + userId;
    return SUCCESS;
}

 /* Getters and Setters */
}

我指的是这个链接,但我的需要是不同的。Creating dynamic URL with 2 action parameters in Struts 2 using OGNL
我也尝试从这个链接的代码,但我仍然没有解决它。

aiqt4smr

aiqt4smr1#

尝试将url变量放入struts.xml中:

<action name="afterLogin" class="com.parth.LoginAction">
    <result name="success">${url}/afterLogin.jsp</result>
</action>

并在此处删除注解Action

@Override
//@Action(value="{url}/afterLogin") // remove this
public String execute() throws Exception {      
    // business logic to insert user into database
    url = "/companyName/" + userId;
    return SUCCESS;
}
xzv2uavs

xzv2uavs2#

如果要更改URL并遵循PRG模式,则结果应为重定向操作类型。请参见Parameters in configuration results

<result name="success" type="redirectAction">afterLogin?companyName=${companyName}&userId=${userId}</result>

<action name="afterLogin">
   <result>/afterLogin.jsp</result>
</action>

相关问题