Struts2通配符CRUD和注解CRUD

来源:互联网 发布:ubuntu dash to dock 编辑:程序博客网 时间:2024/05/19 18:15

通配符CRUD

在struts.xml文件中使用通配符完成Action及Result简化配置

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><constant name="struts.devMode" value="true"></constant><constant name="struts.i18n.reload" value="true"></constant><package name="Struts_12" extends="struts-default"><!-- 下面的*号代表任意字符,{1}和{2}代表第一个*号和第二个*号 ,method={2}是代表进入Action调用{2}这个方法; 如 : *_*在访问页面路径时,访问页面路径为:http://localhost:8080/Struts_tpf/emp_list; 是emp_list,那么emp_list代替了*_*,所以{1}是emp,{2}是list,那么将访问Action里面的list方法 当向访问toadd时,则将list改为toadd--><action name="*_*" class="com.mingde.web.action.EmployeeAction" method="{2}"><result name="{2}">WEB-INF/{1}/{2}.jsp</result><result name="list">WEB-INF/{1}/list.jsp</result><result name="input">WEB-INF/emp/to{2}.jsp</result></action></package></struts>

注解CRUD

第一步:要先导入3个注解架包
asm-3.3.jar
asm-commons-3.3.jar
struts2-convention-plugin-2.3.32.jar
第二步:在struts.xml文件中定义开发中需要用到的常量配置,这个不能在注解中定义
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant><constant name="struts.devMode" value="true"></constant></struts>
第三步:在Action类中使用注解
package com.mingde.web.action;import java.util.List;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.ResultPath;import org.apache.struts2.convention.annotation.Results;import com.mingde.dao.EmployeeDao;import com.mingde.dao.impl.EmployeeDaoImpl;import com.mingde.po.Department;import com.mingde.po.Employee;import com.opensymphony.xwork2.ActionSupport;/** @Results为所有的结果返回级 */@Results({ @Result(name="success",location="list.jsp"), //返回值为success,则跳往list.jsp界面   @Result(name="toadd",location="add.jsp"), //返回值为toadd,则跳往add.jsp界面   @Result(name="to_list",location="employye!list",type="redirect")})//将重定向到employyeAction类,并调用list方法 @ResultPath("/WEB-INF/emp/")//在要跳往的路径前加上该路径;如上面的list.jsp,则变为/WEB-INF/emp/list.jsp@SuppressWarnings("serial")public class EmployeeAction extends ActionSupport{private EmployeeDao ed=new EmployeeDaoImpl();List<Employee> list;Employee e=new Employee();List<Department> d;@Overridepublic String execute() throws Exception {list=ed.seeAll();return SUCCESS;}public String add() throws Exception {ed.add(e);return execute();}public String toadd() throws Exception {d=ed.seeDpt();System.out.println(d);return "toadd";}//下面的get和set方法省略
第四步:在访问页面路径时要注意:
Action名的注解格式为:去掉类名后的Action,将剩余的字母该为小写,如:EmpAction,那么它的名称应改为emp
如果去掉后面的Action,再将全部字母小写也无用时,应将Action类名只保留开头字母大写和Action的开头字母大写,
如:EmpInfoAction 访问页面是写成empinfo ,但仍然无效后,将EmpInfoAction类名改为EmpinfoAction ,在访问页面empinfo 即可

例子:Action类名:EmployeeAction.java
访问页面:http://localhost:8080/Struts_zhujie/employee





原创粉丝点击