一,引入支持Struts2支持註解開發jar包:java
struts2-convention-plugin-2.1.8.1.jar(支持Struts2框架註解開發的jar包)數組
二,Struts2使用註解開發須要遵循一些規範:session
1,Action要必須繼承ActionSupport父類;app
2,Action所在的包名必須以 .action 結尾。框架
三,action中經常使用的註解:jsp
1,@ParentPackage:對應xml配置文件中的package的父包,通常須要繼承struts-default。學習
2,@Namespace:對應配置文件中的nameSpace,命名空間。優化
3,寫在方法前邊的註解:spa
4,看一下action中最經常使用的results中單個result註解的配置吧:prototype
1 <span style="font-size:18px;">@Controller//控制層的Spring註解 2 @Scope("prototype")//支持多例 3 @ParentPackage("struts-default") //表示繼承的父包 4 @Namespace(value="/user") //表示當前Action所在命名空間 5 public class LoginAction extends ActionSupport{ 6 7 @Resource 8 private User user; //使用域驅動模式接收表單參數 9 10 @Action( //表示請求的Action及處理方法 11 value="login", //表示action的請求名稱 12 results={ //表示結果跳轉 13 @Result(name="success",location="/success.jsp",type="redirect"), 14 @Result(name="login",location="/login.jsp",type="redirect"), 15 @Result(name="error",location="/error.jsp",type="redirect") 16 }, 17 interceptorRefs={ //表示攔截器引用 18 @InterceptorRef("defaultStack"), 19 @InterceptorRef("timer") 20 }, 21 exceptionMappings={ //映射映射聲明 22 @ExceptionMapping(exception="java.lang.Exception",result="error") 23 } 24 ) 25 public String login() throws Exception { 26 27 int i = 1/0 ; 28 29 if ("admin".equals(user.getUsercode()) && "admin".equals(user.getUserpswd())) { 30 31 Map session = ActionContext.getContext().getSession(); 32 session.put("session_user", user); 33 34 return "success"; 35 } else { 36 return "login"; 37 } 38 39 } 40 } 41 </span>
綜上,爲Struts2框架中註解的開發。三大框架都利用註解開發,和配置文件開發,效率會大大提高的。各類框架,jar包等新的版本如今都是支持註解開發的,不斷的學習,不斷的優化,不斷提升效率,註解開發利弊並存着,咱們要懂的揚長避短,讓每一個框架,每種思想的優勢都來爲咱們開發便可。