ubuntu服务器 注解方式运行struts

2024-10-30 08:00:57

1、添加包struts2-convention-plugin-2.1.7。注释掉xml文件里的action对应关系。

ubuntu服务器 注解方式运行struts

2、在mvc的control层添加注释代码,即struts.xml注释的部分。package com.how2java.action;import javax.persistence.SqlResultSetMapping;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.how2java.bean.Product;@Namespace("/")@ParentPackage("struts-default")@Results({@Result(name="show", location="/show.jsp"), @Result(name="home", location="/index.jsp")})public class ProductAction { private Product product; @Action("showProduct") public String show(){ product = new Product(); product.setName("iphone7"); return "show"; } public String add(){ System.out.println("product.name:"+product.getName()); return "show"; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; }}

ubuntu服务器 注解方式运行struts

3、注释解释@Namespace("/") :表示注解访问根目录,即本文/z2_struts/。@ParentPackage("struts-default"):xml文件的默认拦截器。 @Result:对应xml文件的返回页面。show对应的是show.jsp. 运行代码。

ubuntu服务器 注解方式运行struts
猜你喜欢