我已经创建了表单login.jsp,并且当用户输入用户名&密码时,我想创建url:/companyName/username
。如何在struts2中创建动态url?
例子:如果公司名称是“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
我也尝试从这个链接代码,但仍然没有解决它。
检查了这一点http://stackoverflow.com/questions/23780282/how-to-create-custom-urls-with-struts2like-www-twitter-com-goodyzain – goodyzain
@goodyzain当我添加** <常数名称=“struts.patternMatcher”值=“正则表达式”/> **我收到错误。 而且我还根据您的问题的答案更改了我的代码,但仍无法正常工作。 如果您知道如何生成动态网址,请根据上述问题向我提供代码。 –
当你添加Advanced wildCard <常量名=“struts.patternMatcher”value =“regex”/>?时,你会得到什么错误? – goodyzain