81,092
社区成员
发帖
与我相关
我的任务
分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beanRefServer.xml</param-value>
</context-param>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!--
默认的spring配置文件是在WEB-INF下的applicationContext.xml
-->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value><!-- 强制进行转码 -->
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 默认所对应的配置文件是WEB-INF下的{servlet-name}-servlet.xml,这里便是:spring3-servlet.xml -->
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
@Controller
@RequestMapping("/user")
public class UserController {
private IUserService userServer;
private IDeptService deptServer;
public IDeptService getDeptService() {
return deptServer;
}
public void setDeptService(IDeptService deptServer) {
this.deptServer = deptServer;
}
public IUserService getUserService() {
return userServer;
}
public void setUserService(IUserService userServer) {
this.userServer = userServer;
}
@RequestMapping(value="/login",method=RequestMethod.POST)
public String login(@RequestParam String name,@RequestParam String password,Model model,HttpServletRequest request) throws Exception{
TUser user1 = userServer.getUserByName(name);
if(user1 == null) {
model.addAttribute("message", "用户不存在");
return "login";
}else if(password == null || !password.equals(user1.getPassword()) ){
model.addAttribute("message", "密码错误");
return "login";
}else {
request.getSession().setAttribute(Constants.USER_INFO_SESSION, user1);
return "welcome";
}
}
以下略...
<?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"
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/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxws:endpoint id="Loginn" implementor="hy.ea.service.Impl.LoginImpl"
address="/login">
</jaxws:endpoint>
<!-- 注意上面的default-autowire="byName",如果没有这个声明那么HibernateDaoSupport中的sessionFactory不会被注入 -->
<!-- 约定优于配置,约定优于配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="ro34dt1234"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingDirectoryLocations">
<list><!-- 这里直接映射的pojo类所在的包,简单方便不用没次加一个pojo类都需要到这里来添加 -->
<value>classpath:com/impf2010/model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
</bean>
<!-- 自动扫描组件,这里要把web下面的 controller去除,他们是在spring3-servlet.xml中配置的,如果不去除会影响事务管理的。-->
<context:component-scan base-package="com.impf2010">
<context:exclude-filter type="regex" expression="com.impf2010.web.*"/>
</context:component-scan>
<!-- 下面是配置声明式事务管理的,个人感觉比用注解管理事务要简单方便 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<aop:config>
<aop:advisor pointcut="execution(* com.impf2010.service.*Service.*(..))" advice-ref="txAdvice"/>
</aop:config>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="*" rollback-for="Exception"/>
</tx:attributes>
</tx:advice>
</beans>
WebService代码:
@WebService(endpointInterface = "hy.ea.service.Login", serviceName = "Login")
public class LoginImpl implements Login {
public String login(TUser user) {
try {
if(null == user)
return "传入User为空";
TUser user1 = userServer.getUserByName(user.getName());
if(user1 != null)
return "成功";
else{
return "调用返回空";
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
private IUserService userServer;
/**
* @return the userServer
*/
public IUserService getUserServer() {
return userServer;
}
/**
* @param userServer the userServer to set
*/
public void setUserServer(IUserService userServer) {
this.userServer = userServer;
}
}
UserService代码如下:
@Service("userServer")
public class UserServiceImpl implements IUserService {
private IUserDao userDao;
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public void addUser(TUser user) throws Exception {
userDao.save(user);
}
public void deleteUser(int id) throws Exception {
userDao.delete(getUserById(id));
}
public TUser getUserById(int id) throws Exception {
return userDao.findById(id);
}
public TUser getUserByName(String name) throws Exception {
List list = userDao.findByName(name);
return list==null || list.size() == 0 ? null : (TUser)list.get(0);
}
public List<TUser> getUserList() throws Exception {
return userDao.findAll();
}
public void updateUser(TUser user) throws Exception {
userDao.merge(user);
}
}
UserDao代码如下:
@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao {
private static final Logger log = LoggerFactory.getLogger(UserDaoImpl.class);
protected void initDao() {
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#save(com.impf2010.model.TUser)
*/
public void save(TUser transientInstance) {
log.debug("saving TUser instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#delete(com.impf2010.model.TUser)
*/
public void delete(TUser persistentInstance) {
log.debug("deleting TUser instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findById(java.lang.Integer)
*/
public TUser findById(java.lang.Integer id) {
log.debug("getting TUser instance with id: " + id);
try {
TUser instance = (TUser) getHibernateTemplate().get(
"com.impf2010.model.TUser", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findByExample(com.impf2010.model.TUser)
*/
public List findByExample(TUser instance) {
log.debug("finding TUser instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findByProperty(java.lang.String, java.lang.Object)
*/
public List findByProperty(String propertyName, Object value) {
log.debug("finding TUser instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from TUser as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findByName(java.lang.Object)
*/
public List findByName(Object name) {
return findByProperty(NAME, name);
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findByAge(java.lang.Object)
*/
public List findByAge(Object age) {
return findByProperty(AGE, age);
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findByDeptId(java.lang.Object)
*/
public List findByDeptId(Object deptId) {
return findByProperty(DEPT_ID, deptId);
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#findAll()
*/
public List findAll() {
log.debug("finding all TUser instances");
try {
String queryString = "from TUser";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#merge(com.impf2010.model.TUser)
*/
public TUser merge(TUser detachedInstance) {
log.debug("merging TUser instance");
try {
TUser result = (TUser) getHibernateTemplate().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#attachDirty(com.impf2010.model.TUser)
*/
public void attachDirty(TUser instance) {
log.debug("attaching dirty TUser instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/* (non-Javadoc)
* @see com.impf2010.dao.IUserDao#attachClean(com.impf2010.model.TUser)
*/
public void attachClean(TUser instance) {
log.debug("attaching clean TUser instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static IUserDao getFromApplicationContext(ApplicationContext ctx) {
return (IUserDao) ctx.getBean("TUserDAO");
}
}