首页 > 技术文章 > Struct2的简单的CRUD配置和使用

wys-373 2019-05-19 22:10 原文

1.

首先是Struct2使用的jar包,可以在官网下载https://struts.apache.org/   ,其中包只要下面这些就够用了。

或者点击下面链接下载

链接:https://pan.baidu.com/s/19vH_6FElQSzbLu3VqcGmSA
提取码:jybj

2.配置文件,在WEB-INF文件下的web.xml文件中添加内容

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

3.添加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.3.dtd">
<struts>

<package name="default" namespace="/" extends="struts-default">
        <action name="delete"  class="action.Deleteaction">
            <result>/index.jsp</result>
        </action>
        <action name="add"  class="action.Addaction">
            <result>/index.jsp</result>
        </action>
        <action name="updata"  class="action.Updataction">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

其中的action的作用:

  1.name属性为请求地址,即当请求地址为name的属性时会到这里。

  2.class 为请求的方法所在的类,该类需要继承 ActionSupport,后面还会说。

  3.method 属性(可选),当请求时,默认执行的是class中 execute 方法,如果添加了method属性,将访问method属性下的方法。

  4.result属性,即为当方法返回成功之后,要返回的界面

4.以增加为例

Addaction,其中主要的业务代码都需要写在execute中,如果在xml文件中action没有配置method属性即执行该方法,配置其他方法就会执行对应的方法。返回为SUCCESS,就会跳转到对应的result页面。

package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import Bean.StudentDao;
import Model.Student;

public class Addaction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    private HttpServletRequest request = ServletActionContext.getRequest();  
    private HttpServletResponse response = ServletActionContext.getResponse(); 

    @Override
    public String execute() throws Exception {
        System.out.println("add");
        request.setCharacterEncoding("UTF-8");
        String xuehao=request.getParameter("xuehao");
        String name=request.getParameter("name");
        String sex=request.getParameter("sex");
        String shengri=request.getParameter("shengri");
        String zhuzhi=request.getParameter("zhuzhi");
        Student student=new Student();
        student.setName(name);
        student.setSex(sex);
        student.setShengri(shengri);
        student.setXuehao(xuehao);
        student.setZhuzhi(zhuzhi);
        StudentDao studentDao=new StudentDao();
        try{
            studentDao.add(student);
            request.setAttribute("success", "添加成功");
        }catch(Exception e){
            request.setAttribute("error123", "用户名存在");
        }
        //request.getRequestDispatcher("addinput.jsp").forward(request, response);
        
        return SUCCESS;
    }

}

 

5.在请求地址上,为add.action

6.在运行时可能会报错在我操作过程中主要遇到的有两种问题

  1.web.xml没有配置

  2.缺少jar包

  3.structs.xml配置错误,如果class下的文件不存在启动会报错,classNotfound,运行的页面都为404

  

程序运行界面:

 

注意请求地址

 

推荐阅读