• [JavaWeb基础] 018.Struts2 Action通配符使用


    Struts2中有一个很牛逼的action通配符,可以用来简化action配置,以我们将要讲解的案例来说,如果我们要对一个学生信息进行增加,删除,修改,那么按照原来的做法,我们需要写3个Action来配置:student_add,student_update,student_del。那么我们容易发现它们可以提取共同的前缀student_,这样我们就可以配置下通配符,把后面的东西弄成变量,那么我们就只要配置一个action就可以了。

    1.首先编写我们的Action类

    package com.babybus.sdteam.action;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import com.babybus.sdteam.dao.StudentHibernateDao;
    import com.babybus.sdteam.vo.Student;
    import com.opensymphony.xwork2.Action;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class ManageStudentAction extends ActionSupport implements Action {
    
    	private static final long serialVersionUID = 1L;
    	private String studentname;
    	private Integer age;
    	private String classname;
    	private Integer userid;
    	private String username; 
        private List<Student> resultList;
    	
    	public String getStudentname() {
    		return studentname;
    	}
    
    	public void setStudentname(String studentname) {
    		this.studentname = studentname;
    	}
    
    	public Integer getAge() {
    		return age;
    	}
    
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    
    	public String getClassname() {
    		return classname;
    	}
    
    	public void setClassname(String classname) {
    		this.classname = classname;
    	}
    
    	public Integer getUserid() {
    		return userid;
    	}
    
    	public void setUserid(Integer userid) {
    		this.userid = userid;
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public List<Student> getResultList() {
    		return resultList;
    	}
    
    	public void setResultList(List<Student> resultList) {
    		this.resultList = resultList;
    	}
    
    	/**
    	 * 获取条件
    	 * @param method
    	 * @return
    	 */
    	private Student getCondition(String method)
    	{
    		Student result = new Student();
            if(!"del".equals(method))
            {
                result.setStudentname(this.studentname);
                result.setAge(this.age);
                result.setClassname(this.classname);
            }
            
            if("update".equals(method)) 
            {
            	result.setId(this.userid);
            }
            
    		return result;
    	}
    	
    	/**
    	 * 增加信息
    	 * @throws SQLException 
    	 */
    	public String studentadd() throws SQLException
    	{
    		Student condition = getCondition("add");
    		StudentHibernateDao dao = new StudentHibernateDao();
    		dao.insertStudent(condition);
    		
    		// 查询结果集合
    		resultList = dao.queryStudent(null);
    		return SUCCESS;
    	}
    	
    	/**
    	 * 修改信息
    	 * @throws SQLException 
    	 */
    	public String studentupdate() throws SQLException
    	{
    		Student condition = getCondition("update");
    		StudentHibernateDao dao = new StudentHibernateDao();
    		dao.updateStudent(condition);
    		
    		// 查询结果集合
    		resultList = dao.queryStudent(null);
    		return SUCCESS;
    	}
    
    	/**
    	 * 删除信息
    	 * @throws SQLException 
    	 */
    	public String studentdel() throws SQLException
    	{
    		StudentHibernateDao dao = new StudentHibernateDao();
    		dao.deleteStudent(this.userid);
    		
    		// 查询结果集合
    		resultList = dao.queryStudent(null);
    		return SUCCESS;
    	}
    }
    

    2.配置我们的struts.xml

    <action name="student_*" class="com.babybus.sdteam.action.ManageStudentAction" method="student{1}">  
        <result name="success">/UserList.jsp</result>  
    </action> 
    

    3.前端调用

    <form action="student_add.action" method="post" >
    <form action="student_update.action" method="post" >
    <a href="student_del.action?userid=${student.id}" onclick="return confirm('确定要删除么?')" }>删除</a></td>
    

    4.这样我们就可以用通配符节省很多action的配置了

     

    本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 

    转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4808043.html

  • 相关阅读:
    蓝色、绿色、红色、紫色四种颜色的水平CSS导航
    【Java/MD5】MD5摘要算法
    【Java/MD2】MD2摘要算法
    SpringBoot的yml配置文件里一处自定义属性出现的错误提示:“Expecting a 'string' but got a 'Mapping' node”
    人的希望就像一颗永恒的星,每到深夜又会重现光芒
    【SpringBoot】给SpringBoot程序配置个简单的logback(附简易版logback.xml下载)
    SpringBoot程序连到本地Oracle,隔三岔五地启动时,在Hiraki连接池之后爆Connection refused错
    【Thymeleaf】ModelAndView中设置一个值,页面上显示此值。
    【Thymeleaf】让页面上显示session中的值
    【Oracle/错误码】select max(id) from emp for update 是锁行还是锁表?结果爆ORA01786错误: 此查询表达式不允许 FOR UPDATE
  • 原文地址:https://www.cnblogs.com/superdo/p/4808043.html
Copyright © 2020-2023  润新知