(十五)struts2之注解

一、作用

  • 以用来替换struts.xml配置文件
  •  使用前提 :必须引入struts2-convention-plugin-2.3.14.jar 这个jar包

二、参数

  • @Action来代替<action>元素!

      String value():指定访问路径;

      Result[] results():指定局部结果。

  • @Result来代替<result>元素!

      String name():指定结果名称;

     String location():指定结果路径。

  • @Namespace代替<package>的namespace属性:

      String value():指定名称空间。

  • @ParentPackage代替<package>的extends属性:

     String value():指定父包名称

 三、基本使用

  • index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%String path=request.getContextPath(); %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <a href="<%=path%>/action/exam">用注解替代xml文件</a>
</body>
</html>

ExampleAction.java

 1 package action;
 2 
 3 import org.apache.struts2.convention.annotation.Action;
 4 import org.apache.struts2.convention.annotation.Namespace;
 5 import org.apache.struts2.convention.annotation.ParentPackage;
 6 import org.apache.struts2.convention.annotation.Result;
 7 
 8 import actionUtil.BaseAction;
 9 
10 @ParentPackage(value="struts-default")
11 @Namespace(value="/action")
12 @Action(value="exam" ,results={
13     @Result(name="succ",location="/succ.jsp")
14         
15 })
16 
17 public class ExampleAction  extends BaseAction {
18     public String execute(){
19         
20         return "succ";
21     }
22 }
  •  注意:1.必须引入struts2-convention-plugin-2.3.14.jar 这个jar包

      2.需要将action类 放在 名字为 action 的package下。

原文地址:https://www.cnblogs.com/shyroke/p/6657421.html