关于ssh的struts的ModelDriven获取表单信息

来源:1-1 监听器介绍

cccca

2019-12-17 17:45:34

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
 <%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>图书分类添加</title>
</head>
<body>
	<center>
		<h1>图书分类添加</h1>
		<div style="color:red"><h2><s:property value="#session.categoryMsg"/></h2></div>
		<s:form action="CategoryAction_add" method="post" namespace="/">
			<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">
				<tr>
					<td>名&nbsp;字</td>
					<td><input type="text" name="Category.cName"> </td>
				</tr>
				<tr>
					<td>描&nbsp;述</td>
					<td>
					<input type="text" name="Category.cDescription"> </td>
				</tr>
				<tr>
					<td colspan="2" style="text-align:center">
						<input type="submit" value="添加">
						<input type="reset" value="重置">
					</td>
				</tr>
			</table>
		</s:form>
	</center>
</body>
</html>
package com.ssh.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.ssh.entity.Category;
import com.ssh.service.LibService;

public class CategoryAction extends ActionSupport implements ModelDriven<Category> {
	private static final long serialVersionUID = 1L;
	private Category cate=new Category();
	private LibService libService;
	public void setLibService(LibService libService) {
		this.libService = libService;
	}
	@Override
	public Category getModel() {
		// TODO Auto-generated method stub
		System.out.println(cate.toString());
		return cate;
		
	}	
	
	public Category getCate() {
		return cate;
	}
	public void setCate(Category cate) {
		this.cate = cate;
	}
	public String add() {
		System.out.println(this.getModel().toString());
		System.out.println("================");
		System.out.println(cate.toString());
		List<Category>cates= libService.getCategory(getModel().getcName());
		if(cates==null) {
			cate.setBooks(null);
			ActionContext.getContext().getSession().put("categoryMsg", "");
			libService.save(getModel());
			return "success";
		}else {
			ActionContext.getContext().getSession().put("categoryMsg", "添加失败,分类已存在");
			return "error";
		}
	}



	
}


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- 配置SessionFactory -->

<bean id="sessionFactory"

class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

<property name="dataSource">

<ref bean="dataSource" />

</property>

<!-- 配置Hibernate的属性 -->

<property name="hibernateProperties">

<props>

<!-- 配置数据库方言 -->

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<!--输出运行时生成的SQL语句 -->

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">udate</prop>

</props>

</property>

<!-- 指定HIbernate映射文件的路径 -->

<property name="mappingResources">

<list>

<value>com/ssh/entity/Book.hbm.xml</value>

<value>com/ssh/entity/Category.hbm.xml</value>

<value>com/ssh/entity/User.hbm.xml</value>

</list>

</property>

</bean>


<!-- 配置dataSource -->

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<!-- 配置数据库JDBC驱动 -->

<property name="driverClassName">

<value>com.mysql.jdbc.Driver</value>

</property>

<!-- 配置数据库连接URL -->

<property name="url" value="jdbc:mysql://localhost:3306/ssh_library"></property>

<!-- 配置数据库用户名 -->

<property name="username">

<value>root</value>

</property>

<!-- 配置数据库密码 -->

<property name="password">

<value>root</value>

</property>

</bean>

<!--action层bean配置  -->

<bean id="LoginAction" class="com.ssh.action.LoginAction" scope="prototype">

<property name="libService" ref="libService"></property>

</bean>

<bean id="CategoryAction" class="com.ssh.action.CategoryAction" scope="prototype" >

<property name="libService" ref="libService"></property>

</bean>

<!--service层bean配置  -->

<bean id="libService" class="com.ssh.service.LibServiceImpl" >

<property name="libDao" ref="libDao"></property>

</bean>

<!--dao层bean配置  -->

<bean id="libDao" class="com.ssh.dao.LibDaolmpl">

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>


</beans>

<?xml version="1.0" encoding="UTF-8" ?>  

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<!--struts根标签 -->

<struts>

<!-- <constant name="struts.devMode" value="true" /> 打开开发模式 -->

<constant name="struts.objectFactory" value="spring" /> <!--引入spring框架 -->

<!--定义包 -->

<package name="default" namespace="/" extends="struts-default">

<action name="LoginAction_*" class="LoginAction" method="{1}">

<result >/server.jsp</result>

<result name="error">/login.jsp</result>

<result name="registSuccess">/login.jsp</result>

<result name="registerror">/regist.jsp</result>

</action>

<action name="CategoryAction_*" class="CategoryAction" method="{1}">

<result >/category.jsp</result>

<result name="error" >/category.jsp</result>

</action>

</package>

</struts> 

有跳转,有提交,但就是获取不到表达信息,属性全部都是null

写回答

1回答

好帮手慕珊

2019-12-17

同学,你好!老师对于Struts的细节问题了解得不多。不过从你的问题描述看,有可能是属性名和Java类中的名称不匹配导致没有得到数据。

你确认一下struts的语法,下面表单中的name怎么写,才能和类中的属性对应

http://img.mukewang.com/climg/5df8b98b09b0fdb505460211.jpg

祝学习愉快!

0

0 学习 · 9666 问题

查看课程