流影草 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 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?
  • ¥20 部件内部的CT图像数据集
  • ¥15 Visual studio调用动态库
  • ¥15 双目摄像头标定后的校准文件
  • ¥15 powerbi矩阵增加度量值后出现对应关系错乱
  • ¥30 频率分析法分析绘制奈奎斯特图、波特图
  • ¥15 弹出来一万个系统找不到指定的文件框框,怎么解决
  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow