struts2 hibernate mysql 实例_Java Struts+Spring+Hibernate SSH框架实例源码(含数据库脚本)...

该实例展示了如何整合Struts2(版本2.3.16)、Spring(版本4.1.1)和Hibernate(版本4.3.6)进行SSH开发。首先,你需要配置数据库,然后修改jdbc.properties文件。可能出现的问题包括Tomcat和JRE版本不匹配,解决方法见链接。登录Action中,通过EmpService查询数据库,并使用Gson将Emp对象转换为JSON。如果查询成功,跳转到success.jsp,否则显示failure.jsp。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【实例简介】

本实例SSH版本 Struts2.3.16Spring4.1.1Hibernate4.3.6

首先你要创建mysql 或者 oracle数据库(doc目录下有),

然后你需要修改src/jdbc.properties 文件中的数据库配置串

然后即可浏览程序

常见的问题 tomcat版本不一致,jre版本不一致 提示 unbound,修改方法 点击这里查看

【实例截图】

94a5061d69758498c2e06f6acf57e9dd.png

e9395ba01a93e6245dd5b7a515a9e070.gif

【核心代码】

package com.xmm.demo.action;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import org.apache.struts2.convention.annotation.Action;

import org.apache.struts2.convention.annotation.ExceptionMapping;

import org.apache.struts2.convention.annotation.ExceptionMappings;

import org.apache.struts2.convention.annotation.Namespace;

import org.apache.struts2.convention.annotation.ParentPackage;

import org.apache.struts2.convention.annotation.Result;

import org.springframework.stereotype.Controller;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

import com.opensymphony.xwork2.ActionSupport;

import com.xmm.demo.domain.Emp;

import com.xmm.demo.service.EmpService;

@Controller

@ParentPackage("struts-default")

@Namespace(value = "/loginAction")

@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })

public class LoginAction extends ActionSupport {

private static final long serialVersionUID = 1L;

@Resource

private EmpService empService;

private String empname;

private String password;

@Action(value = "login" , results = { @Result(name = "success", location = "/success.jsp"),

@Result(name = "failure", location = "/failure.jsp"),

@Result(name = "error", location = "/login.jsp")})

public String login() {

System.out.println("\n提交的请求参数如下:");

System.out.println("empname:" empname);

System.out.println("password:" password "\n");

int eid = 0;

try {

eid = Integer.parseInt(empname);

} catch (Exception ex) {

}

Emp emp = empService.findEmpById(eid);// 为了测试结果,这里写死了

//Listlistemp=empService.findAllList();

if (emp != null) {

System.out.println("\n根据主键ID查询记录:查到了,查询成功!");

System.out.println(emp.toString() "\n");

HttpServletRequest request = ServletActionContext.getRequest();// 在Struts2的Action中获取Servlet的原生API

request.setAttribute("empname", emp.getEname());

// 注意:这里使用GsonBuilder来创建Gson实例

Gson gson = new GsonBuilder().setPrettyPrinting().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS").create();

String empJson = gson.toJson(emp);

System.out.println("\nEmp对象转化为JSON格式:");

System.out.println(empJson "\n");

return SUCCESS;

} else {

System.out.println("\n根据主键ID查询记录:没查到,查询失败,记录不存在!\n");

return "failure";

}

}

public String getEmpname() {

return empname;

}

public void setEmpname(String empname) {

this.empname = empname;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值