1.首先是ActionMapping的unknown属性的测试
在struts-config.xml中的<action-mapping>添加如下代码:
<
action
path
="/unknown"
type
="com.ycringfinger.struts.UnknownTestAction"
scope
="request"
unknown
="true"
>
< forward name ="success" path ="/unknown.jsp" ></ forward >
</ action >
< forward name ="success" path ="/unknown.jsp" ></ forward >
</ action >
在UnknownTestAction中让其跳转到unknown.jsp
这样,当链接到一个未知的.do时,例如xxx.do,服务器就会自动跳转到unknown.jsp
2.使用Action中的forward属性,这样可以省去编写一个action来控制跳转,直接forward到要跳转的界面
例:
<
action
path
="/adduser"
forward
="/adduser.jsp"
>
</
action
>
这样,在jsp中采用以下代码
<
a
href
="adduser.do"
>
使用ActionMapping.forward转向添加用户
</
a
><
br
>
即可访问adduser.jsp
3.在注册时出现错误返回时保留表单数据
首先编写一个UserActionForm.java
package
com.ycringfinger.struts;
import org.apache.struts.action.ActionForm;
public class UserActionForm extends ActionForm ... {
private String name;
private int age;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
import org.apache.struts.action.ActionForm;
public class UserActionForm extends ActionForm ... {
private String name;
private int age;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public int getAge() ...{
return age;
}
public void setAge(int age) ...{
this.age = age;
}
}
再上struts-config.xml:
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<! DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd" >
< struts-config >
< form-beans >
< form-bean name ="userActionForm" type ="com.ycringfinger.struts.UserActionForm" >
</ form-bean >
</ form-beans >
< action-mappings >
< action path ="/adduser" forward ="/adduser.jsp" >
</ action >
< action path ="/unknown" type ="com.ycringfinger.struts.UnknownTestAction" scope ="request" unknown ="true" >
< forward name ="success" path ="/unknown.jsp" ></ forward >
</ action >
< action path ="/saveuser" type ="com.ycringfinger.struts.SaveUserAction" name ="userActionForm" scope ="request" >
< forward name ="adduser" path ="/adduser.do" ></ forward >
< forward name ="success" path ="/addusersuccess.jsp" ></ forward >
</ action >
</ action-mappings >
</ struts-config >
<! DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd" >
< struts-config >
< form-beans >
< form-bean name ="userActionForm" type ="com.ycringfinger.struts.UserActionForm" >
</ form-bean >
</ form-beans >
< action-mappings >
< action path ="/adduser" forward ="/adduser.jsp" >
</ action >
< action path ="/unknown" type ="com.ycringfinger.struts.UnknownTestAction" scope ="request" unknown ="true" >
< forward name ="success" path ="/unknown.jsp" ></ forward >
</ action >
< action path ="/saveuser" type ="com.ycringfinger.struts.SaveUserAction" name ="userActionForm" scope ="request" >
< forward name ="adduser" path ="/adduser.do" ></ forward >
< forward name ="success" path ="/addusersuccess.jsp" ></ forward >
</ action >
</ action-mappings >
</ struts-config >
由于数据都是提交到了这个UserActionForm中,所以要想保存数据,在该编写如下代码(使用EL表达式):
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
< form action ="saveuser.do" >
Name : < input type ="text" name ="name" value ="${userActionForm.name}" >< br >
Age : < input type ="text" name ="age" value ="${userActionForm.age}" >< br >
< input type ="submit" value ="save" >< br >
</ form >
</ body >
</ html >
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
< form action ="saveuser.do" >
Name : < input type ="text" name ="name" value ="${userActionForm.name}" >< br >
Age : < input type ="text" name ="age" value ="${userActionForm.age}" >< br >
< input type ="submit" value ="save" >< br >
</ form >
</ body >
</ html >
如果使用HTML Tag,则可这样编写:
<%
...
@ page language="java" import="java.util.*" pageEncoding="GB18030"
%>
<% ... @ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
< h1 > 添加用户 </ h1 >
< hr >
< html:form action ="saveuser.do" method ="post" >
用户: < html:text property ="name" ></ html:text >< br >
年龄: < html:text property ="age" ></ html:text >< br >
< input type ="submit" value ="add" >
</ html:form >
</ body >
</ html >
<% ... @ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
< head >
</ head >
< body >
< h1 > 添加用户 </ h1 >
< hr >
< html:form action ="saveuser.do" method ="post" >
用户: < html:text property ="name" ></ html:text >< br >
年龄: < html:text property ="age" ></ html:text >< br >
< input type ="submit" value ="add" >
</ html:form >
</ body >
</ html >