ssh框架整合,添加员工信息;
员工,Emp:
package com.blue.entity;
public class Emp {
//id,数据库中为标识列
private int empId;
//姓名
private String empName;
//年龄
private int empAge;
//性别
private String empSex;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpAge() {
return empAge;
}
public void setEmpAge(int empAge) {
this.empAge = empAge;
}
public String getEmpSex() {
return empSex;
}
public void setEmpSex(String empSex) {
this.empSex = empSex;
}
}
jsp页面,form:
<form action="empLogin" method="post">
<table width="300px" height="150px" border="1px">
<tr>
<td width="100px">姓名:</td>
<td><input type="text" name="emp.empName"/></td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="emp.empAge"/></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="emp.empSex"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="保存"></td>
</tr>
</table>
</form>
action:
package com.blue.action;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.blue.entity.Emp;
import com.blue.service.EmpService;
import com.opensymphony.xwork2.ActionSupport;
public class EmpAction extends ActionSupport{
public String result(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
EmpService empService= (EmpService) context.getBean("empService");
Emp emp = (Emp) context.getBean("emp");
//测试
System.out.println("id:"+emp.getEmpId()+" name:"+emp.getEmpName()+" age:"+emp.getEmpAge()+" sex:"+emp.getEmpSex());
int num = empService.save(emp);
if(num == -1){
//值为空
return "empty";
}else if(num > 0){
//添加成功
return SUCCESS;
}else{
//失败
return ERROR;
}
}
}
控制台输出:
id:0 name:null age:0 sex:null
spring配置文件(beans.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="emp" class="com.blue.entity.Emp"></bean>
<bean id="empDao" class="com.blue.dao.impl.EmpDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="empService" class="com.blue.service.impl.EmpServiceImpl">
<property name="empDao" ref="empDao"></property>
</bean>
<bean id="empAction" class="com.blue.action.EmpAction" scope="prototype"></bean>
</beans>
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>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="empLogin" class="com.blue.action.EmpAction" method="result">
<result name="success">welcome.jsp</result>
<result name="error">error.jsp</result>
<!-- 空值 -->
<result name="empty">empty.jsp</result>
</action>
</package>
</struts>
包结构:
取不到页面值,求大神指点