- 浏览: 9963 次
- 性别:
- 来自: 北京
最新评论
一、实现WEB层
package com.hanz.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.hanz.domain.Department;
import com.hanz.domain.Popedom;
import com.hanz.domain.User;
import com.hanz.service.DepartmentService;
import com.hanz.service.PopedomService;
import com.hanz.service.UserService;
import com.hanz.util.UserGridPrint;
import com.opensymphony.xwork2.ActionContext;
@Component("user")
public class UserAction extends BaseAction {
private UserService userService;
@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
private DepartmentService departmentService;
@Resource(name = "departmentService")
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
private PopedomService popedomService;
@Resource(name = "popedomService")
public void setPopedomService(PopedomService popedomService) {
this.popedomService = popedomService;
}
private PageBean pageBean;// 包含分布信息的bean
private int start;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
private int limit;
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public String login() throws Exception {
String status = request.getParameter("status");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
PrintWriter out = response.getWriter();
try {// 非法身份导致异常
Integer.valueOf(status);
} catch (Exception e) {
out.write("{'success':false}");
out.flush();
out.close();
e.printStackTrace();
return null;
}
System.out.println("status:" + status + ";username:" + username
+ ";pwd:" + pwd);
// 组装用户对象并验证
User user = new User();
user.setPopedom((Popedom) popedomService.get(Popedom.class, Integer.valueOf(status)));
user.setUser_code(username);
user.setPwd(pwd);
User u = userService.login(user);
if (u != null) {
Map session = ActionContext.getContext().getSession();
session.put(WebConstant.USER,u);
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
return null;
}
public String alter() throws Exception {
String userid = request.getParameter("userid");
Integer userid_id = Integer.parseInt(userid);
String status_m = request.getParameter("status_m");
Integer status_Id = Integer.parseInt(status_m);
String department_m = request.getParameter("department_m");
Integer department_Id = Integer.parseInt(department_m);
String user_code_ = request.getParameter("user_code_");
String pwd_ = request.getParameter("pwd_");
String name_ = request.getParameter("name_");
String sex_ = request.getParameter("sex_");
String email_ = request.getParameter("email_");
System.out.println("新建部门信息页面-->接收到参数:");
System.out.println("status_m:" + status_m + "|department_m:"
+ department_m + "|user_code_:" + user_code_);
User user = new User();
user.setId(userid_id);
user.setUser_code(user_code_);
user.setPwd(pwd_);
user.setName(name_);
user.setEmail(email_);
user.setSex(sex_);
user.setPopedom((Popedom) popedomService.get(Popedom.class, status_Id));
user.setDepartment((Department) departmentService.get(Department.class,
department_Id));
Boolean ok = userService.update(user);
try {
PrintWriter out = response.getWriter();
if (ok) {
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String delete() throws Exception {
String departmentid = request.getParameter("userid");
Integer userid = Integer.parseInt(departmentid);
System.out.println("删除部门信息页面-->接收到参数:");
System.out.println("departmentid_:" + userid);
Boolean ok = userService.delete(User.class, userid);
try {
String msg = "failed";
if (ok) {
msg = "success";
}
PrintWriter out = response.getWriter();
out.write(msg);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String add() throws Exception {
String status_m = request.getParameter("status_m");
Integer status_Id = Integer.parseInt(status_m);
String department_m = request.getParameter("department_m");
Integer department_Id = Integer.parseInt(department_m);
String user_code_ = request.getParameter("user_code_");
String pwd_ = request.getParameter("pwd_");
String name_ = request.getParameter("name_");
String sex_ = request.getParameter("sex_");
String email_ = request.getParameter("email_");
System.out.println("新建部门信息页面-->接收到参数:");
System.out.println("status_m:" + status_m + "|department_m:"
+ department_m + "|user_code_:" + user_code_);
User user = new User();
user.setUser_code(user_code_);
user.setPwd(pwd_);
user.setName(name_);
user.setEmail(email_);
user.setSex(sex_);
user.setPopedom((Popedom) popedomService.get(Popedom.class, status_Id));
user.setDepartment((Department) departmentService.get(Department.class,
department_Id));
Boolean ok = userService.save(user);
try {
PrintWriter out = response.getWriter();
if (ok) {
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String search() throws Exception {
String hql = "from User u";
String name = request.getParameter("name");
String user_code = request.getParameter("user_code");
String department = request.getParameter("department");
if (name != null && !name.equals("") || user_code != null
&& !user_code.equals("") || department != null
&& !department.equals(""))
hql = hql + " where 1=1 ";
if (name != null && !name.equals(""))
hql = hql + " and u.name like '%" + name + "%'";
if (user_code != null && !user_code.equals(""))
hql = hql + " and u.user_code like '%" + user_code + "%'";
if (department != null && !department.equals(""))
hql = hql + " and u.department = " + department + "";
this.pageBean = userService.queryForPage(hql, start, limit);
String result = UserGridPrint.printUserList(pageBean.getList(),
pageBean.getTotalCount());
System.out.println("查询结果:" + result);
if (result != null && !result.equals("")) {
PrintWriter out;
try {
out = response.getWriter();
out.write(result);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String searchComboBox() throws Exception {
this.pageBean = userService.queryForPage();
String result = UserGridPrint.printComboBoxUserList(pageBean.getList(),
pageBean.getTotalCount());
System.out.println("查询结果:" + result);
if (result != null && !result.equals("")) {
PrintWriter out;
try {
out = response.getWriter();
out.write(result);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String input() throws Exception {
return "input";
}
}
二、struts.xml和struts.properties配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 -->
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="configdebug"
class="com.configdebug.interceptor.ConfigDebugInterceptor" />
<!--定义一个名为authority的拦截器 -->
<interceptor class="com.hanz.Interceptor.AuthorityInterceptor"
name="authority" />
<!--定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="mydefault">
<!--配置内建默认拦截器 -->
<interceptor-ref name="defaultStack" />
<!--配置自定义的拦截器 -->
<interceptor-ref name="authority" />
<!-- 配置debug监控流程器 -->
<interceptor-ref name="configdebug" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefault" />
<!-- 定义全局Result映射 -->
<global-results>
<!-- 定义sql、root两个异常对应的视图页 -->
<result name="business">/error.jsp</result>
<result name="root">/error.jsp</result>
<!-- 定义login逻辑视图对应的视图资源 -->
<result name="login">/index.jsp</result>
</global-results>
<!-- 定义全局异常映射 -->
<global-exception-mappings>
<!-- 当Action中遇到HrException异常时, 系统将转入name为business的结果中 -->
<exception-mapping exception="com.hanz.exception.ErpException"
result="business" />
<!-- 当Action中遇到Exception异常时, 系统将转入name为root的结果中 -->
<exception-mapping exception="java.lang.Exception"
result="root" />
</global-exception-mappings>
<!-- 用户管理 -->
<action name="user_*" class="com.hanz.action.UserAction"
method="{1}" />
method="{1}" />
<action name="*">
<result>/{1}.jsp</result>
</action>
</package>
</struts>
struts.properties文件配置
struts.custom.i18n.resources=resource
struts.i18n.encoding=UTF-8
struts.devMode=true
struts.objectFactory=spring
package com.hanz.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.hanz.domain.Department;
import com.hanz.domain.Popedom;
import com.hanz.domain.User;
import com.hanz.service.DepartmentService;
import com.hanz.service.PopedomService;
import com.hanz.service.UserService;
import com.hanz.util.UserGridPrint;
import com.opensymphony.xwork2.ActionContext;
@Component("user")
public class UserAction extends BaseAction {
private UserService userService;
@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
private DepartmentService departmentService;
@Resource(name = "departmentService")
public void setDepartmentService(DepartmentService departmentService) {
this.departmentService = departmentService;
}
private PopedomService popedomService;
@Resource(name = "popedomService")
public void setPopedomService(PopedomService popedomService) {
this.popedomService = popedomService;
}
private PageBean pageBean;// 包含分布信息的bean
private int start;
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
private int limit;
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
public String login() throws Exception {
String status = request.getParameter("status");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
PrintWriter out = response.getWriter();
try {// 非法身份导致异常
Integer.valueOf(status);
} catch (Exception e) {
out.write("{'success':false}");
out.flush();
out.close();
e.printStackTrace();
return null;
}
System.out.println("status:" + status + ";username:" + username
+ ";pwd:" + pwd);
// 组装用户对象并验证
User user = new User();
user.setPopedom((Popedom) popedomService.get(Popedom.class, Integer.valueOf(status)));
user.setUser_code(username);
user.setPwd(pwd);
User u = userService.login(user);
if (u != null) {
Map session = ActionContext.getContext().getSession();
session.put(WebConstant.USER,u);
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
return null;
}
public String alter() throws Exception {
String userid = request.getParameter("userid");
Integer userid_id = Integer.parseInt(userid);
String status_m = request.getParameter("status_m");
Integer status_Id = Integer.parseInt(status_m);
String department_m = request.getParameter("department_m");
Integer department_Id = Integer.parseInt(department_m);
String user_code_ = request.getParameter("user_code_");
String pwd_ = request.getParameter("pwd_");
String name_ = request.getParameter("name_");
String sex_ = request.getParameter("sex_");
String email_ = request.getParameter("email_");
System.out.println("新建部门信息页面-->接收到参数:");
System.out.println("status_m:" + status_m + "|department_m:"
+ department_m + "|user_code_:" + user_code_);
User user = new User();
user.setId(userid_id);
user.setUser_code(user_code_);
user.setPwd(pwd_);
user.setName(name_);
user.setEmail(email_);
user.setSex(sex_);
user.setPopedom((Popedom) popedomService.get(Popedom.class, status_Id));
user.setDepartment((Department) departmentService.get(Department.class,
department_Id));
Boolean ok = userService.update(user);
try {
PrintWriter out = response.getWriter();
if (ok) {
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String delete() throws Exception {
String departmentid = request.getParameter("userid");
Integer userid = Integer.parseInt(departmentid);
System.out.println("删除部门信息页面-->接收到参数:");
System.out.println("departmentid_:" + userid);
Boolean ok = userService.delete(User.class, userid);
try {
String msg = "failed";
if (ok) {
msg = "success";
}
PrintWriter out = response.getWriter();
out.write(msg);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String add() throws Exception {
String status_m = request.getParameter("status_m");
Integer status_Id = Integer.parseInt(status_m);
String department_m = request.getParameter("department_m");
Integer department_Id = Integer.parseInt(department_m);
String user_code_ = request.getParameter("user_code_");
String pwd_ = request.getParameter("pwd_");
String name_ = request.getParameter("name_");
String sex_ = request.getParameter("sex_");
String email_ = request.getParameter("email_");
System.out.println("新建部门信息页面-->接收到参数:");
System.out.println("status_m:" + status_m + "|department_m:"
+ department_m + "|user_code_:" + user_code_);
User user = new User();
user.setUser_code(user_code_);
user.setPwd(pwd_);
user.setName(name_);
user.setEmail(email_);
user.setSex(sex_);
user.setPopedom((Popedom) popedomService.get(Popedom.class, status_Id));
user.setDepartment((Department) departmentService.get(Department.class,
department_Id));
Boolean ok = userService.save(user);
try {
PrintWriter out = response.getWriter();
if (ok) {
out.write("{'success':true}");
} else {
out.write("{'success':false}");
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public String search() throws Exception {
String hql = "from User u";
String name = request.getParameter("name");
String user_code = request.getParameter("user_code");
String department = request.getParameter("department");
if (name != null && !name.equals("") || user_code != null
&& !user_code.equals("") || department != null
&& !department.equals(""))
hql = hql + " where 1=1 ";
if (name != null && !name.equals(""))
hql = hql + " and u.name like '%" + name + "%'";
if (user_code != null && !user_code.equals(""))
hql = hql + " and u.user_code like '%" + user_code + "%'";
if (department != null && !department.equals(""))
hql = hql + " and u.department = " + department + "";
this.pageBean = userService.queryForPage(hql, start, limit);
String result = UserGridPrint.printUserList(pageBean.getList(),
pageBean.getTotalCount());
System.out.println("查询结果:" + result);
if (result != null && !result.equals("")) {
PrintWriter out;
try {
out = response.getWriter();
out.write(result);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String searchComboBox() throws Exception {
this.pageBean = userService.queryForPage();
String result = UserGridPrint.printComboBoxUserList(pageBean.getList(),
pageBean.getTotalCount());
System.out.println("查询结果:" + result);
if (result != null && !result.equals("")) {
PrintWriter out;
try {
out = response.getWriter();
out.write(result);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public String input() throws Exception {
return "input";
}
}
二、struts.xml和struts.properties配置
<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Struts2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<!-- Struts2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 -->
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="configdebug"
class="com.configdebug.interceptor.ConfigDebugInterceptor" />
<!--定义一个名为authority的拦截器 -->
<interceptor class="com.hanz.Interceptor.AuthorityInterceptor"
name="authority" />
<!--定义一个包含权限检查的拦截器栈 -->
<interceptor-stack name="mydefault">
<!--配置内建默认拦截器 -->
<interceptor-ref name="defaultStack" />
<!--配置自定义的拦截器 -->
<interceptor-ref name="authority" />
<!-- 配置debug监控流程器 -->
<interceptor-ref name="configdebug" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="mydefault" />
<!-- 定义全局Result映射 -->
<global-results>
<!-- 定义sql、root两个异常对应的视图页 -->
<result name="business">/error.jsp</result>
<result name="root">/error.jsp</result>
<!-- 定义login逻辑视图对应的视图资源 -->
<result name="login">/index.jsp</result>
</global-results>
<!-- 定义全局异常映射 -->
<global-exception-mappings>
<!-- 当Action中遇到HrException异常时, 系统将转入name为business的结果中 -->
<exception-mapping exception="com.hanz.exception.ErpException"
result="business" />
<!-- 当Action中遇到Exception异常时, 系统将转入name为root的结果中 -->
<exception-mapping exception="java.lang.Exception"
result="root" />
</global-exception-mappings>
<!-- 用户管理 -->
<action name="user_*" class="com.hanz.action.UserAction"
method="{1}" />
method="{1}" />
<action name="*">
<result>/{1}.jsp</result>
</action>
</package>
</struts>
struts.properties文件配置
struts.custom.i18n.resources=resource
struts.i18n.encoding=UTF-8
struts.devMode=true
struts.objectFactory=spring
相关推荐
项目配置:Hibernate3.3.2+Spring3.0+Struts2.2.3 + ext3.4.0,真实项目实现,项目中可参考经典权限设计,通用数据录入,批量数据处理(poi),统一的JSON格式封装,灵活的组合模式设计,EXTJS面向组件编程参考......
Spring3.0+Hibernate4.0+SpringMVC整合ExtJS4是一个集成的应用系统,希望对学习javaweb和extjs的朋友有帮助
ExtJSweb 开发指南中的 简单图书管理系统 ExtJS3.0+spring+hibernate+struts
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。这个是分卷,同时下载2.3.4部分
Hibernate3.3.2+Spring2.5.5+Struts2.1.6+Extjs3.0.0_Annotations方式快速开发框架资料。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
DWR2+EXTJS2.2+Hibernate3.3+Spring2+Acegi 做的综合管理系统,数据库采用MYSQL,分层清晰,业务相对复杂,是学习框架不可多得的项目。
extjs+spring+struts+hibernate整合实例
Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS开发CRUD功能Hibernate+Spring+Struts2+ExtJS...
extjs3.0+struts2 实现登陆 验证码 tree树的加载 首页面。 主要代码还是借鉴别人,然后我再原有基础上再加工。 还没添加数据库类。对于初学者来说还是不错的入门源码。
Struts2(json-plugin) + Spring2 + ExtJS2.2 开源网络硬盘系统
Struts2+Spring+Hibernate+ExtJS开发CRUD功能
Hibernate+Spring+Struts2+ExtJS开发CRUD功能 ,里面包含源码及安装说明文档。可以作为项目开发的基础框架。
Hibernate+Spring+Struts2+ExtJS整合开发实例 非常经典!学习的好帮助!
strut2+hibernate+spring+extjs3.0+excel 动态加载grid 数据导出excel,导入excel datasource自己修改你自己的数据源,数据库中的表可见实体类创建,或者自己修改实体类
采用了struts2.0,spring2.2,ibatis,quartz,extjs3.0 实现了关于短信的调度框架,对接收人维护,动态添加每日短信数据,编写短信模版,最后通过quartz定时发送,因为短信接口收费所以不提供,需要jar包自己下载,...
利用dwr,整合extjs,spring,hibernate.是你学习dwr的参考指南
Extjs3.1+struts2+spring2.5+hibernate3.5环境