问题描述
- ssh注解,获取不到server。
-
web.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">sshTest1
<!-- 配置Spring的监听器,用于初始化ApplicationContext对象 -->org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext*.xml
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
<!-- 自动扫描action -->actionPackages
com.lovo
struts2
/*login.jsp
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd "><!--建立数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- SQL Server数据驱动 --> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"> </property> <!-- 数据库地址 --> <property name="url" value="jdbc:sqlserver://localhost:1433; DatabaseName=sshTest"> </property> <property name="username" value="sa"></property> <property name="password" value="123"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource" /> </property> <property name="hibernateProperties"> <props> <!--配置Hibernate的方言--> <prop key="hibernate.dialect"> org.hibernate.dialect.SQLServerDialect </prop> <!--格式化输出sql语句--> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.use_sql_comments">false</prop> </props> </property> <!--自动扫描实体 --> <property name="packagesToScan" value="com.lovo.*" /> </bean> <!-- 使用 annotation --> <context:annotation-config /> <!-- 自动扫描与装配bean --> <context:component-scan base-package="com.lovo"></context:component-scan>
<!-- 采用注释的方式配置 aop -->
<!-- 用注解来实现事务管理 -->
/tx:attributes
/tx:advicestruts.xml
<?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"><constant name="struts.devMode" value="true" /> <!-- 将对象交给spring管理 --> <constant name="struts.objectFactory" value="spring" /> <!-- 指定资源编码类型 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 指定每次请求到达,重新加载资源文件 --> <constant name="struts.i18n.reload" value="false" /> <!-- 指定每次配置文件更改后,自动重新加载 --> <constant name="struts.configuration.xml.reload" value="false" /> <!-- 默认后缀名 --> <constant name="struts.action.extension" value="action," />
action
package com.lovo.action;import javax.annotation.Resource;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;import com.lovo.bean.Emp;
import com.lovo.service.EmpService;
import com.opensymphony.xwork2.ActionSupport;@Namespace("/")
@ParentPackage("struts-default")
@Controller
public class EmpAction extends ActionSupport {
@Resource()public Emp emp ;
private EmpService empService ;@ Action(value = "empAction", results = { @Result(name = "success", location = "/index.jsp"), @Result(name = "index", location = "/login.jsp")}) public String add() { try { empService.add(emp); } catch (Exception e) { e.printStackTrace(); return "index"; } return "success"; } public Emp getEmp() { return emp; } public void setEmp(Emp emp) { this.emp = emp; } public EmpService getEmpService() { return empService; } public void setEmpService(EmpService empService) { this.empService = empService; }
}
beanpackage com.lovo.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;@Entity(name="t_emp")
public class Emp implements Serializable {
@Id@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "pk_name")private String name;
@Column(name = "fk_age")
private int age;public Emp(){} public Emp(int id, String name, int age) { super(); this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }
}
dao
package com.lovo.dao;import java.util.List;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;import com.lovo.bean.Emp;
@Repository("empDao")
public class EmpDao {
@Resource(name="sessionFactory")private SessionFactory sessionFactory;
public void add(Emp emp){ Session session = sessionFactory.getCurrentSession(); session.save(emp); } public List findAll(){ return null; } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; }
}
package com.lovo.service;import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import com.lovo.bean.Emp;
import com.lovo.dao.EmpDao;@Service("empService")
@Transactionalpublic class EmpService {
@Resource(name="empDao")
private EmpDao empDao;public EmpService(){} public void add(Emp emp) { empDao.add(emp); } public EmpDao getEmpDao() { return empDao; } public void setEmpDao(EmpDao empDao) { this.empDao = empDao; }
}
action 访问server的时候,没有访问到server获取数据报空指针异常。
高手帮我看看哪里错了。 。。
解决方案
http://bbs.csdn.net/topics/390810378/