步骤 2 : 模仿和排错 步骤 3 : 基于前面的教程 步骤 4 : jar 步骤 5 : 注释掉struts.xml 步骤 6 : 添加注解 步骤 7 : 运行测试 步骤 8 : 其他常用注解 步骤 9 : 总结
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
访问页面: http://127.0.0.1:8080/struts/showProduct
在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。 推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。 这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来 这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
这个知识,基于显示数据到JSP 这个知识点,把其由xml配置方式,改造为注解方式
1. 为了使struts支持注解,需要用到struts2-convention-plugin-x.x.x.jar 这个jar包,在前面的教程中是没有使用的,所以这里需要从右侧下载
2. 下载好了之后,放在WEB-INF/lib 下 3. 不仅如此,还要在项目导入jar,以使得eclipse能够编译通过
接着就把struts.xml中的配置信息注释掉,以确保最后生效的是注解方式
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="basicstruts" extends="struts-default">
<!-- <action name="showProduct" class="com.how2java.action.ProductAction" method="show"> -->
<!-- <result name="show">show.jsp</result> -->
<!-- </action> -->
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="basicstruts" extends="struts-default"> <!-- <action name="showProduct" class="com.how2java.action.ProductAction" method="show"> --> <!-- <result name="show">show.jsp</result> --> <!-- </action> --> </package> </struts>
然后就是在ProductAction类上面添加注解
1. 在类前面添加3个注解 @Namespace("/") 表示访问路径,如果是@Namespace("/test"),那么访问的时候,就需要写成http://127.0.0.1:8080/struts/test/showProduct @ParentPackage("struts-default") 与配置文件中的struts-default相同,表示使用默认的一套拦截器 @Results({@Result(name="show", location="/show.jsp"), @Result(name="home", location="/index.jsp")}) 预先定义多个results, "show" 返回"/show.jsp" , "home" 返回 "/index.jsp". 注: 这里并没有用到"home",写出来的目的是为了演示这种定义多个result 的代码风格。 2. 在show方法前加上注解: @Action("showProduct") 表示当访问路径是showProduct的时候,就会调用show方法 package com.how2java.action;
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 Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
Namespace:指定命名空间。
ParentPackage:指定父包。 Result:提供了Action结果的映射。(一个结果的映射) Results:“Result”注解列表 ResultPath:指定结果页面的基路径。 Action:指定Action的访问URL。 Actions:“Action”注解列表。 ExceptionMapping:指定异常映射。(映射一个声明异常) ExceptionMappings:一级声明异常的数组。 InterceptorRef:拦截器引用。 InterceptorRefs:拦截器引用组。 一般说来,不是所有的注解都会用到,真正用到哪个的时候再来查一下就知道怎么回事了。
可以看出来,注解方式,就是把本来做在struts.xml里的事情,搬到了Action类里面来做。
那么到底应该用注解还是配置呢?从个人经验来讲,小项目适合用注解,大项目因为其复杂性,采用注解会导致配置信息难以维护和查询,更适合采用xml配置方式。 关于注解是如何生效的,请查看自定义注解相关知识。
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|