简易的CRM系统案例之Struts2+JSP+MySQL版本

简易的CRM系统案例之Servlet+Jsp+MySQL版本改进


Servlet优化为Struts2 学习

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

    <!-- 引入struts核心过滤器 -->
    <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>
    
    <error-page>
        <error-code>404</error-code>
        <location>/notFoundError.jsp</location>
    </error-page>

</web-app>

src下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.0.dtd">


<struts>


    <package name="crm" extends="struts-default">

        <action name="addPolicy" class="com.loaderman.crm.action.AddPolicyAciton" method="execute">
            <result name="getPolicyList" type="redirectAction">getPolicyList</result>
            <result name="addPolicy">/pages/addPolicy.jsp</result>
        </action>
        <action name="addUser" class="com.loaderman.crm.action.AddUserAction" method="execute">
            <result name="getUserList" type="redirectAction">getUserList</result>
            <result name="addUser">/pages/addUser.jsp</result>
        </action>

        <action name="delPolicy" class="com.loaderman.crm.action.DelPolicyAciton" method="execute">
            <result name="success" type="redirectAction">getPolicyList</result>
        </action>
        <action name="delUser" class="com.loaderman.crm.action.DelUserAciton" method="execute">
            <result name="success" type="redirectAction">getUserList</result>
        </action>
        <action name="getPolicyList" class="com.loaderman.crm.action.GetPolicyListAciton" method="execute">
            <result name="success">/pages/policyList.jsp</result>
        </action>
        <action name="getUserList" class="com.loaderman.crm.action.GetUserListAction" method="execute">
            <result name="success" type="redirect">/pages/userList.jsp</result>
        </action>
        <action name="doLogin" class="com.loaderman.crm.action.DoLoginAciton" method="execute">
            <result name="success" type="redirectAction">getUserList</result>
            <result name="login" >/login.jsp</result>
        </action>

    </package>

</struts>
package com.loaderman.crm.action;

import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import com.loaderman.crm.service.impl.UserServiceimp;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class GetUserListAction extends ActionSupport  {

    // 处理请求
    public String execute() throws Exception {
        HttpServletRequest request1 = ServletActionContext.getRequest();
        String name = request1.getParameter("name");
        System.out.println("GetUserListAction name" + name);
        if ( name != null && (! name.isEmpty())) {
//            name =new String(name.getBytes("ISO8859-1"), "UTF-8");
            UserService stuService = new UserServiceimp();
            List<User> list = stuService.getUserByName( name);
            ActionContext.getContext().getSession().put("list", list);
        } else {
            UserService stuService = new UserServiceimp();
            List<User> list = stuService.getAllUser();

            ActionContext.getContext().getSession().put("list", list);

        }
        return "success";
    }



}
package com.loaderman.crm.action;


import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import com.loaderman.crm.service.impl.UserServiceimp;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class AddUserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();


    public String execute() throws Exception {
        System.out.println(user.toString());
        UserService studentService = new UserServiceimp();
        HttpServletRequest request1 = ServletActionContext.getRequest();
        String opr = request1.getParameter("opr");
        int n = 0;
        if (opr.equals("addUser")) {
            n = studentService.addUser(user);
        } else if (opr.equals("modifyUser")) {
            int id = Integer.parseInt(request1.getParameter("id"));
            user.setId(id);
            n = studentService.modifyUser(user);
        }
        if (n > 0) {
            return "getUserList";
        } else {
            return "addUser";
        }
    }

    @Override
    public User getModel() {
        return user;
    }
}
package com.loaderman.crm.action;

import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import com.loaderman.crm.service.impl.UserServiceimp;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;


public class DelUserAciton extends ActionSupport {
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();

        UserService userService = new UserServiceimp();
        System.out.println(request.getParameter("id"));
        int id = Integer.parseInt(request.getParameter("id"));
        User stu = new User();
        stu.setId(id);

        userService.delUser(stu);
        return "success";

    }
}

点击源码下载


 

posted on 2019-01-22 13:39  LoaderMan  阅读(406)  评论(0编辑  收藏  举报

导航