SSH中Struts2获取不到jsp页面中值,求大神指点

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>

包结构:
图片说明

取不到页面值,求大神指点

3个回答

EmpAction 类不需要实例化Emp对象,应该是
private Emp emp;
public void setEmp(Emp emp){
this.emp=emp;
}
publilc String getEmp(){
return emp;
}
你可以看看域模型(DomainModel),再看看驱动模型,看看他们的区别

yanglancheng
流影草 我朋友帮我解决了,跟你说的一样,谢谢,点个赞
接近 4 年之前 回复

主要是这两个模型你弄混了

问题已解决,同时感谢楼上热心回答
图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐