流影草 2016-10-02 09:02 采纳率: 50%
浏览 2120
已采纳

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条回答 默认 最新

  • 枫之木落 2016-10-02 09:47
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?