一、调试1
1、修改addProduct.jsp
与jstl标准标签库类似的,struts有专属标签库
form标签用于提交数据
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<s:form action="addProduct">
<s:textfield name="product.name" label="product name" />
<s:submit value="Submit" />
</s:form>
</body>
</html>
struts把表单里的内容自动转换成了table
二、调试2
<s:debug/>
三、使用拦截器
1、ProductAction增加Date属性
2、创建拦截器
public class DateInterceptor extends AbstractInterceptor{
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
ProductAction action = (ProductAction) actionInvocation.getAction();
action.setDate(new Date());
return actionInvocation.invoke();
}
}
3、struts.xml中声明拦截器,并指定categories使用
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts" extends="struts-default">
<interceptors>
<interceptor name="dateIntercepter" class="com.web.interceptor.DateInterceptor"/>
</interceptors>
<action name="categories" class="com.web.action.ProductAction" method="categoryList">
<interceptor-ref name="dateIntercepter"/>
<interceptor-ref name="defaultStack"/>
<result name="categories">categories.jsp</result>
</action>
</package>
四、客户端跳转(重定向)
struts.xml配置
action name="categories" class="com.web.action.ProductAction" method="categoryList">
<interceptor-ref name="dateIntercepter"/>
<interceptor-ref name="defaultStack"/>
<result name="categories" type="redirect">categories.jsp</result>
</action>
五、客户端跳转传参
1、ProductAction中增加name属性
2、addProduct.jsp配置
<body>
<from action="addPage">
<input type="text" name="test" value="${param.name}">
</from>
</body>
六、表单验证
1、addProduct.jsp中增加错误内容显示
<s:head/>
2、ProductAction中增加validate方法进行验证
public void validate(){
if(product.getName().length()==0){
addFieldError("product.name","name can't be empty!");
}
}
3、struts.xml中增加错误视图
<action name="addProduct" class="com.web.action.ProductAction" method="add">
<result name="show">show.jsp</result>
<result name="input">addProduct.jsp</result>
</action>
七、注解方式
ProductAction中,增加注解
@Namespace("/")
@ParentPackage("struts-default")
@Results({@Result(name="show", location="/show.jsp"),
@Result(name="home", location="/index.jsp")})
public class ProductAction extends ActionSupport{
private Product product;
private ArrayList<Product> products;
private ArrayList<Product> products1;
private ArrayList<Product> products2;
private ArrayList<Integer> selectedID;
private ArrayList<Category> Categories;
private Category Category1;
private Category Category2;
private String name;
private Date date;
@Action("showProduct")
public String show(){
product = new Product();
product.setName("iphone7");
return "show";
}