XML配置归纳大法

Servlet配置
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
        id="WebApp_ID" version="3.0">
        <display-name>o</display-name>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
        </welcome-file-list>
        <servlet>
            <servlet-name>OA</servlet-name>
            <servlet-class>OA.servlet.OAService</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>OA</servlet-name>
            <url-pattern>/OA</url-pattern>
        </servlet-mapping>  
    </web-app>
    //servlet-name相同
    //servlet-class是servlet继承Httpservlet类的全部路径
    //url-pattern是from表单的提交路径
    //通过url-pattern找到servlet-name相同的servlet-class,然后运行配置的class文件,doget/doPost.

Servlet/应用
    /*结构
    建立实体
    通过数据访问层Dao层用实体接受
    写服务层对实体进行操作
    然后from表单通过Servlet用服务层进行实现,*/
    package OA.servlet;
    import java.io.IOException;
    import java.io.UnsupportedEncodingException;
    import java.util.Collection;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import OA.Connection.Generic;
    import OA.mode.Dept;
    import OA.service.DeptService;
    import net.sf.json.JSONArray;
    import oracle.net.aso.s;
    public class DeptServlet<T> extends HttpServlet {
        private T T;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            System.out.println(delete(req));
            resp.sendRedirect(req.getContextPath() + "/Dept.jsp");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html; charset=UTF-8");
            DeptService DeptService = new DeptService();
            Dept dept = new Dept();
            switch (req.getParameter("Type")) {
            case "Add":
                //通过ajax实现
                Collection<Dept> list = DeptService.Fid(0);
                resp.getWriter().print(JSONArray.fromObject(list));
                resp.getWriter().flush();
                resp.getWriter().close();
                break;
            case "AddDept":
                int id = Integer.parseInt(req.getParameter("Dept_id"));
                String name = req.getParameter("Dept_name");
                int fid = Integer.parseInt(req.getParameter("Dept_fid"));
                String dec = req.getParameter("Dept_description");
                dept.setDept_id(id);
                dept.setDept_name(name);
                dept.setDept_fig(fid);
                dept.setDept_description(dec);
                boolean end = DeptService.add(dept);
                System.out.println(end);
                resp.sendRedirect(req.getContextPath() + "/Dept.jsp");
                break;
            default:
                break;
            }
        }
        @Override
        protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
            // TODO Auto-generated method stub
            super.service(arg0, arg1);
        }
        public boolean delete(HttpServletRequest req) {
            try {
                req.setCharacterEncoding("UTF-8");
                switch (req.getParameter("Type")) {
                case "Dept":
                    System.out.println("Dept");
                    Dept dept=new Dept();
                    Generic<Dept> Generic=new Generic<Dept>();
                    Generic.delete(dept, Integer.parseInt(req.getParameter("id")));
                    break;
                default:
                    break;
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
    }

ServletAjax的/应用
    <script type="text/javascript">
        $(function(){
             var sopt="<option value='0'>顶级部门</option>";
            $.ajax({
                   url:'<%=request.getContextPath()%>/Dept',
                   type:'post',
                   data: {Type:'Add'},
                   dataType:'JSON',
                   success: function(data){
                       $.each(data,function(i,n){                    
                           sopt+="<option value='"+n.dept_id+"'>"+n.dept_name+"</option>";
                       })
                       $("#fid").append(sopt);
                   }
                 });
            $("#Add").click(function() {
            });
        })
    </script>

Struts2框架的Xml配置/搭建/应用
    //(1)解压struts-2.3.4.1-all
    //(2)然后解压struts-2.3.4.1/apps/struts2-blank.war
    //(3)Struts\WEB-INF\Web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
        <display-name>Struts Blank</display-name>
        <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>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
        </welcome-file-list>
    </web-app>
    //复制内容到项目里的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>
    //(4)Struts\WEB-INF\src\java\struts.xml(该xml文件放在项目src下面)
    //(5)把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>
        <package name="default" namespace="/" extends="struts-default">
            <action name="*_*" class="OA.service.{1}Over" method="{2}">
                <result>/{1}{2}.jsp</result>
                <!--<result>/OA.jsp</result>-->
            </action>
        </package>
    </struts>
    //<action>的name是web:url路径,通过class(要执行的class文件全路径),method是执行的方法;
    //<result>是通过<action>name执行<action>calss配置的java.class文件,运行完后打开的网页,现在是DeptAdd.jsp;
    package OA.service;
    import java.util.ArrayList;
    import java.util.Collection;
    import org.apache.struts2.components.ActionComponent;
    import com.opensymphony.xwork2.ActionContext;
    import OA.Connection.Connection;
    import OA.Uilt.GetConnectionValues;
    import OA.mode.Dept;
    import OA.mode.Primarykey;
    public class DeptOver extends GetConnectionValues {
        private int Dept_id;
        private String Dept_name;
        private int Dept_fig;
        private String Dept_description;
        public int getDept_id() {
            return Dept_id;
        }
        public void setDept_id(int dept_id) {
            Dept_id = dept_id;
        }
        public String getDept_name() {
            return Dept_name;
        }
        public void setDept_name(String dept_name) {
            Dept_name = dept_name;
        }
        public int getDept_fig() {
            return Dept_fig;
        }
        public void setDept_fig(int dept_fig) {
            Dept_fig = dept_fig;
        }
        public String getDept_description() {
            return Dept_description;
        }
        public void setDept_description(String dept_description) {
            Dept_description = dept_description;
        }
        public DeptOver() {

        }
        public String Add() {
            System.out.println("*****");
            Dept Dept = new Dept();
            // Dept.setDept_id(Dept_id);
            // Dept.setDept_name(Dept_name);
            // Dept.setDept_fig(Dept_fig);
            // Dept.setDept_description(Dept_description);
            // Connection.Call().Add(Dept);
            Collection<Dept> list = Connection.Call().Select();
    //      ArrayList<Dept> list=new ArrayList<Dept>();
    //      list.add(new Dept(1, "888", 2, "fsufk"));

            ActionContext.getContext().put("list", list);
            System.out.println(Dept_id + Dept_name + Dept_fig + Dept_description);
            ActionContext.getContext().put("Dept_id", "52");
            return "success";
        }
    }
    //<action>的配置的class,它的字段属性必须封装,属性名必须和from表单里面input的name相同;

Struts2框架的/应用
    package OA.service;
    import java.util.Collection;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ModelDriven;
    import OA.Connection.Connection;
    import OA.mode.Point;
    import OA.mode.Dept;
    public class DeptOver implements ModelDriven<Dept> {
        private Dept entity;
        private Point point;
        public Point getPoint() {
            return point;
        }
        public void setPoint(Point point) {
            this.point = point;
        }
        public Dept getEntity() {
            return entity;
        }
        public void setEntity(Dept entity) {
            this.entity = entity;
        }

        public String OA() {
            System.out.println(point);
            return "OA";
        }
        /**
         * 添加
         * 
         * @return
         */
        public String Add() {
            int id = entity.getDept_id();
            entity.setDept_id(id);
            entity.setDept_name(entity.getDept_name());
            entity.setDept_fig(entity.getDept_fig());
            entity.setDept_description(entity.getDept_description());
            if (id == 0) {
                Connection.Call().Add(entity);
            }
            Connection.Call().Modify(entity);
            return "success";
        }
        /**
         * 查询
         * 
         * @return
         */
        public String Select() {
            Collection<Dept> list = Connection.Call().Select();
            ActionContext.getContext().put("list", list);
            return "success";
        }
        /**
         * 删除
         * 
         * @return
         */
        public String Delete() {
            Connection.Call().delete(entity.getDept_id());
            return "delete";
        }
        @Override
        public Dept getModel() {
            if (entity == null) {
                entity = new Dept();
            }
            return entity;
        }
    }
    //该类通过implements于ModelDriven<T>,对那个实体进行操作就给<Dept>填写里面,还必须重写getModel方法,getModel优先运行,Dept的封装类的字段属性必须和页面的name相同.

Struts2框架向页面发送信息
    //添加到Context()
    ActionContext.getContext().put("list", list);
    //拿到值
    <S:iterator value="#list" var="a">
            <S:property value="#a.dept_name" />
            <S:property value="#a.dept_id" />
    </S:iterator>
    //该类型主要用于判断用户是否登录
    ActionContext.getContext().getSession().put("list", list);
    //后续补充
    ActionContext.getContext().getValueStack().push(list);
    //后续补充
    ServletActionContext.getRequest().setAttribute("list", list);

Struts2全局转换器
    //创建一个类
    package OA.mode;
    import java.util.Date;
    public class Point {
        private int x;
        private int y;
        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        public int getY() {
            return y;
        }
        public void setY(int y) {
            this.y = y;
        }
    }
    //然后再Action里面进行封装
    package OA.service;
    import java.util.Collection;
    import java.util.Date;
    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ModelDriven;
    import OA.Connection.Connection;
    import OA.mode.Point;
    import OA.mode.Dept;
    public class DeptOver implements ModelDriven<Dept> {
        private Dept entity;
        private Point point;
        private Date date;
        public Date getDate() {
            return date;
        }
        public void setDate(Date date) {
            this.date = date;
        }
        public Point getPoint() {
            return point;
        }
        public void setPoint(Point point) {
            this.point = point;
        }
        public Dept getEntity() {
            return entity;
        }
        public void setEntity(Dept entity) {
            this.entity = entity;
        }
        public String OA() {
            //System.out.println(point.getDate());
            Collection<Dept> list = Connection.Call().Select();
    //      ActionContext.getContext().put("list", list);
            ActionContext.getContext().getSession().put("list", list);
    //      ActionContext.getContext().getValueStack().push(list);
    //      ServletActionContext.getRequest().setAttribute("list", list);
            return "OA";
        }
        /**
         * 添加
         * 
         * @return
         */
        public String Add() {
            int id = entity.getDept_id();
            entity.setDept_id(id);
            entity.setDept_name(entity.getDept_name());
            entity.setDept_fig(entity.getDept_fig());
            entity.setDept_description(entity.getDept_description());
            if (id == 0) {
                Connection.Call().Add(entity);
            }
            Connection.Call().Modify(entity);
            return "success";
        }
        /**
         * 查询
         * 
         * @return
         */
        public String Select() {
            Collection<Dept> list = Connection.Call().Select();
            ActionContext.getContext().put("list", list);
            ActionContext.getContext().getSession().put("list", list);
            ActionContext.getContext().getValueStack().push(list);
            ServletActionContext.getRequest().setAttribute("list", list);
            return "success";
        }
        /**
         * 删除
         * 
         * @return
         */
        public String Delete() {
            Connection.Call().delete(entity.getDept_id());
            return "delete";
        }
        @Override
        public Dept getModel() {
            if (entity == null) {
                entity = new Dept();
            }
            return entity;
        }
    }
    //转换器
    package OA.service;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Map;
    import org.apache.struts2.util.StrutsTypeConverter;
    import OA.mode.Dept;
    public class DateConverter extends StrutsTypeConverter {
        @Override
        public Object convertFromString(Map arg0, String[] val, Class arg2){
            //val就是接受的值
            Point Point=new Point();
            //接受12,88
            String value=val[0];
            //把12,88用逗号隔开生成数组
            String[] Field=value.split(",");
            Point.setX(Integer.parseInt(Field[0]));
            Point.setY(Integer.parseInt(Field[1]));
            return Point;
        }
        @Override
        public String convertToString(Map arg0, Object arg1) {
            //arg1接受的值转换
            Point Point=(Point)arg1;
            return "X:"+Point.getX()+"Y:"+PointgetY();
        }
    }
    /*
    创建一个实体类
    在Action进行封装
    配置全局转换器:必须在src下创建xwork-conversion.properties文件
    文件内容是OA.mode.Point=OA.service.PointConverter
    文件格式是:实体类的全路径=转换该类的全路径
    该例子效果:
    没有转换器的时候:在input框输入12,88,接受的页面显示的是12,88,debug里面是null
    有转换器的时候:在input框输入12,88,接受的页面显示的是"X:12Y:88,debug里面是有值得
    */
    //如果全局转换器配置一个类,则在局部转换器给该类里的一个属性进行转换的时候,有时候不运行

Struts2局部转换器转换器
    package OA.service;
    import java.util.Collection;
    import java.util.Date;
    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ModelDriven;
    import OA.Connection.Connection;
    import OA.mode.Point;
    import OA.mode.Dept;
    public class DeptAction implements ModelDriven<Dept> {
        private Dept entity;
        private Point point;
        private Date time;
        public Date getTime() {
            return time;
        }
        public void setTime(Date time) {
            this.time = time;
        }
        public Point getPoint() {
            return point;
        }
        public void setPoint(Point point) {
            this.point = point;
        }
        public Dept getEntity() {
            return entity;
        }
        public void setEntity(Dept entity) {
            this.entity = entity;
        }
        public String OA() {

            return "OA";
        }
        /**
         * 添加
         * 
         * @return
         */
        public String Add() {
            int id = entity.getDept_id();
            entity.setDept_id(id);
            entity.setDept_name(entity.getDept_name());
            entity.setDept_fig(entity.getDept_fig());
            entity.setDept_description(entity.getDept_description());
            if (id == 0) {
                Connection.Call().Add(entity);
            }
            Connection.Call().Modify(entity);
            return "success";
        }
        /**
         * 查询
         * 
         * @return
         */
        public String Select() {
            Collection<Dept> list = Connection.Call().Select();
            ActionContext.getContext().put("list", list);
            ActionContext.getContext().getSession().put("list", list);
            ActionContext.getContext().getValueStack().push(list);
            ServletActionContext.getRequest().setAttribute("list", list);
            return "success";
        }
        /**
         * 删除
         * 
         * @return
         */
        public String Delete() {
            Connection.Call().delete(entity.getDept_id());
            return "delete";
        }
        @Override
        public Dept getModel() {
            if (entity == null) {
                entity = new Dept();
            }
            return entity;
        }
    }
    //转换类
    package OA.Converter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Map;
    import org.apache.struts2.util.StrutsTypeConverter;
    public class DeptConverter extends StrutsTypeConverter {
        private SimpleDateFormat sim = new SimpleDateFormat("yyyy/MM/dd");
        @Override
        public Object convertFromString(Map arg0, String[] value, Class arg2) {
            Date date = null;
            try {
                date = sim.parse(value[0]);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return date;
        }
        @Override
        public String convertToString(Map arg0, Object arg1) {
            System.out.println("执行完了");
            Date Date = (Date) arg1;
            return "时间:" + sim.format(Date);
        }
    }
    /*
    在Action命名一个time,封装一下.
    文件名:DeptAction-conversion.properties,格式是ActionClass-conversion.properties
    位置:文件必须在Action同一个包中,
    内容:time=OA.Converter.DeptConverter
    格式是:要转换的time=转换类的路径
    例子效果:
    没有转换器的时候:在input框输入2012/12/12,接受的页面显示的是2012/12/12,debug里面是null
    有转换器的时候:在input框输入2012/12/12,接受的页面显示的是时间:2012/12/12 ,debug里面是有值的
    */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值