Struts2中不同包中存在相同action配置方式

1.在src中不同package中定义两个类LoginAction

jk包中:

public class LoginAction extends ActionSupport{
	
	private final String success = "success";
	private final String error = "error";
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String execute()
	{
		if(getUsername().equals("jk") &&
			getPassword().endsWith("123"))
		{
			ActionContext.getContext().getSession().put("user", getUsername());
			return success;
		}
		else
		{
			return error;
		}
	}
}

 org.apache.struts.actionresult包:

public class LoginAction {

	private String username;
	private String password;
	private String tip;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getTip() {
		return tip;
	}
	public void setTip(String tip) {
		this.tip = tip;
	}
	public String execute() throws Exception
	{
		if(getUsername().equalsIgnoreCase("my"))
		{
			throw new MyException("自定义异常");
		}
		if(getUsername().equalsIgnoreCase("lang"))
		{
			throw new java.lang.Exception("用户名不能为lang");
		}
		if(getUsername().equals("jk"))
		{
			setTip("登录成功");
			return "success";
		}
		else{
			return "error";
		}
	}
}

MyExeption

public class MyException extends Exception {

	public MyException()
	{}
	
	public MyException(String msg)
	{
		super(msg);
	}
} 

第一个类是普通登录类,第二个用来测试struts2的异常处理!

2.struts.xml中配置方式:

<package name="jk" extends="struts-default">
		<action name="login" class="jk.LoginAction">
			<result type="redirect">/success.jsp</result>
		</action>
</package>
<package name="org.apache.struts.actionresult" extends="struts-default" namespace="/exception">
	<global-results>
		<result name="lang">/lang.jsp</result>
	</global-results>
	<global-exception-mappings>
		<exception-mapping result="lang" exception="java.lang.Exception">
		</exception-mapping>
	</global-exception-mappings>
	<action name="exception/login"  class="org.apache.struts.actionresult.LoginAction">
		<exception-mapping result="my" exception="org.apache.struts.actionresult.MyException" />
		<result name="my">/my.jsp</result>
	</action>
</package>

3.jsp中代码:

<s:form action="login">
	<s:textfield name="username" key="user"/>
	<s:textfield name="password" key="pass"/>
	<s:submit key="login"/>
</s:form>
<s:form action="login" namespace="/exception">
	<s:textfield name="username" key="user"/>
	<s:textfield name="password" key="pass"/>
	<s:submit key="login"/>
</s:form>

即可用使用命名空间来区别两个action中的login。

但是在测试中发现一个问题:

No configuration found for the specified action: 'exception/login' in namespace....

也就是说在命名空间中找不到exception/login这个action

只需要把JSP中写成这样就可以了:<s:form action="login" namespace="/exception">!

猜你喜欢

转载自jkhhuse.iteye.com/blog/1597595
今日推荐