Struts2 【第三章 其他】

一、调试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";
    }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值