流影草 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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能