SSH全注解-annotation详细配置.

本文详细介绍了如何使用SSH框架进行全注解配置,并与Spring、Hibernate进行整合,包括配置Spring和Hibernate注解,实现Struts、Spring、Hibernate的注解驱动,以及如何使用通配符配置Result。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SSH全注解-annotation详细配置 [url]http://www.iteye.com/topic/816574[/url]
使用 Spring 2.5 注释驱动的 IoC 功能 [url]https://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/[/url]
只是参考.

先根据[url]http://panyongzheng.iteye.com/blog/1103591[/url]配置好无注解,然后在根据下面的设定来实现全注解。当然,你可以只直接Spring和Hibernate,Struts.xml的东西依然保存。这个看你喜欢。

[b]@Results也可以用于整个Action注解,跟在@ParentPackage("struts-default") 注解的后面,那么这个@Result就相对应整个类。[/b]

[b]1.只注解Sprint & Hibernate的内容。
2.Struts,Spring,Hibernate全部注解。
3.使用通配符配置result。
[/b]


[color=red]1.[b]实现Spring & Hibernate注解[/b](这里不针对Struts使用注解):[/color]
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
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_3_0.xsd">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<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>


<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



struts.xml,class定义的Action,应该跟Spring的注解@Controller("LoginAction")对应。
<?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>
<package name="struts2" extends="struts-default" namespace="/">
<action name="login" class="LoginAction">
<result>/index.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>

</struts>



applicationContext.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:p="http://www.springframework.org/schema/p"
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/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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
">

<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:annotation-config />
<context:component-scan base-package="com" />
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=CITYU_DEV_TEST">
</property>
<property name="username" value="sa"></property>
<property name="password" value="asl12345"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource" p:packagesToScan="com.pojo"
>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>


DAO:
package com.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.dao.IDAO_TEST_TABLE;
import com.pojo.TestTable;

@Repository("DAO_TEST_TABLE")
public class DAO_TEST_TABLE extends HibernateDaoSupport implements IDAO_TEST_TABLE {

/* (non-Javadoc)
* @see com.dao.impl.IDAO_TEST_TABLE#list()
*/
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory){
super.setSessionFactory(sessionFactory);
}


@Override
public List<TestTable> list(){
return getHibernateTemplate().find("from TestTable order by id.userName");
}

/* (non-Javadoc)
* @see com.dao.impl.IDAO_TEST_TABLE#save(com.pojo.TestTable)
*/
@Override
public void save(TestTable t){
getHibernateTemplate().save(t);
}

}


Service:
package com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.IDAO_TEST_TABLE;
import com.pojo.TestTable;
import com.service.ITestTableService;

@Service("TestTableService")
@Transactional
public class TestTableService implements ITestTableService {
@Resource(name="DAO_TEST_TABLE")
public IDAO_TEST_TABLE dao;
/* (non-Javadoc)
* @see com.service.impl.ITestTableService#list()
*/
@Override
@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public List<TestTable> list(){
return dao.list();
}
/* (non-Javadoc)
* @see com.service.impl.ITestTableService#save(com.pojo.TestTable)
*/
@Override
@Transactional(propagation=Propagation.REQUIRED)
public void save(TestTable t){

}
public IDAO_TEST_TABLE getDao() {
return dao;
}
public void setDao(IDAO_TEST_TABLE dao) {
this.dao = dao;
}


}


Action:
package com.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.service.impl.TestTableService;

@Controller("LoginAction")
public class LoginAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = -6434128483294080524L;
@Resource(name="TestTableService")
public TestTableService service;
private String userName;
private String userPassword;


@Override
public String execute() throws Exception {

System.out.println("Call from action.");
List list = service.list();
System.out.println(list.size());
return super.SUCCESS;
};

@Override
public void validate() {
System.out.println(getText("test.i18n"));
};






public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public TestTableService getService() {
return service;
}

public void setService(TestTableService service) {
this.service = service;
}

}



[color=red]2.[b]Struts一并使用注解[/b]:[/color]
修改web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<init-param>
<param-name>actionPackages</param-name>
<param-value>com.action,com.actionother</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


去掉struts.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">
<struts>
</struts>


Action.java增加一些注解:
package com.action;

import java.util.List;

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.opensymphony.xwork2.ActionSupport;
import com.service.impl.TestTableService;

@Controller("LoginAction")
@Namespace("/")
@ParentPackage("struts-default")
public class LoginAction extends ActionSupport {

/**
*
*/
private static final long serialVersionUID = -6434128483294080524L;
@Resource(name="TestTableService")
public TestTableService service;
private String userName;
private String userPassword;


@Override
@Action(value="/login",
results={
@Result(name="success",location="/index.jsp"),
@Result(name="input",location="/login.jsp")
}
)
public String execute() throws Exception {

System.out.println("Call from action.");
List list = service.list();
System.out.println(list.size());
return super.SUCCESS;
};

@Override
public void validate() {
System.out.println(getText("test.i18n"));
};






public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}

public TestTableService getService() {
return service;
}

public void setService(TestTableService service) {
this.service = service;
}
}


[color=red][b]或者换一种配置[/b][/color]:
struts.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">
<struts>
<package name="struts2" extends="struts-default" namespace="/">
</package>
</struts>



Action.java
@Controller("LoginAction")/*这里是可以省略的*/
@ParentPackage("struts2")
/*
@Result可以在这里配置。
*/
public class LoginAction extends ActionSupport {
......
......
}



[color=red][b]3.使用通配符配置Result[/b]:[/color]
@Override
@Action(value="/*PageAction"
,results={
@Result(name="success",location="/{1}.jsp")
}
)
public String execute() throws Exception {
// TODO Auto-generated method stub
//return super.execute();
System.out.println("@@ Call-->PageAction");
return "success";
}

调用Aciton的URL: indexPageAction.action


.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值