在SSH整合的过程中经常遇到空指针的解决方案
先说点废话又隔了一天没有写笔记了前天还说的一天写篇笔记的真该打脸
但昨弄了一天的SSH整合一时忘掉了,这不今天带着我遇到的问题来见大家了,希望对你们有所帮助
困扰了我一天的bug
HTTP Status 500
description The server encountered an internal error that prevented it from fulfilling this request.
在这里出现了错误一般是我们的Srping整合struts2是我们的属性名没有一致
也就是spring中bean的id没有对应Action中service实现类set方法的的属性名
这里我把代码贴一下
spring中的配置
<!--配置Action的项--> <bean id="productAction" class="com.java.action.ProductAction" scope="prototype"> <property name="productService" ref="productService"></property> </bean>
Action中的写法
import com.java.domain.Product; import com.java.service.ProductService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class ProductAction extends ActionSupport implements ModelDriven<Product>{ private static final long serialVersionUID = 1L; //模型驱动使用的项 private Product product=new Product(); //struts2和spring整合中按名称自动注入的项 private ProductService productService; public void setProductService(ProductService productService) { this.productService = productService; } //getModel方法自动将提交的数据给product属性赋值前台不需要使用(对象.属性)的 @Override public Product getModel() { // TODO Auto-generated method stub return product; } public String save(){ System.out.println("Action中的save方法执行了..."); productService.save(product); return SUCCESS; } }
struts2的配置
<?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> <package name="S2SH" extends="struts-default" namespace="/"> <action name="product_*" class="productAction" method="{1}"> <result name="success" type="redirect">/success.jsp</result> </action> </package> </struts>
一定要注意private ProductService productService;这个类名要对应ref=”productService”这个注入名
当然struts2中还有一种写法
这中写法就是把spring中action配置的项删掉,在struts2中的class中直接引入
action类所在的位置例如:class=”com.java.action.ProductAction”
struts2的配置
<?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> <package name="S2SH" extends="struts-default" namespace="/"> <action name="product_*" class="com.java.action.ProductAction" method="{1}" > <result name="success" type="redirect">/success.jsp</result> </action> </package> </struts>
所有的spring的配置
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置hibernate -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql"> true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 加载映射文件 -->
<property name="mappingLocations" value="classpath:com/java/domain/*.hbm.xml"></property>
</bean>
<!-- 配置业务层的项 -->
<bean id="productService" class="com.java.service.impl.ProductServiceImpl">
<property name="productDao" ref="productDao"></property>
</bean>
<!-- 配置DAO的项 -->
<bean id="productDao" class="com.java.dao.impl.ProductDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置Action的项
<bean id="productAction" class="com.java.action.ProductAction" scope="prototype">
<property name="productService" ref="productService"></property>
</bean> -->
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>S2SH</display-name>
<welcome-file-list>
<welcome-file>addProduct.jsp</welcome-file>
</welcome-file-list>
<!-- SPRING 监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- struts2配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加商品</title>
</head>
<body>
<h1>保存商品的页面</h1>
<s:form action="product_save" method="post" namespace="/" theme="simple">
<table border="1" width="400">
<tr>
<td>商品名称</td>
<td><s:textfield name="pname"></s:textfield>
</tr>
<tr>
<td>商品价格</td>
<td><s:textfield name="price"></s:textfield>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"/></td>
</tr>
</table>
</s:form>
</body>
</html>