spring 3.0.5 + jotm 实现的的spring mvc 的例子

1.web.xml

 

<servlet>
		<servlet-name>Dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Dispatcher</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

 2.加入编码格式

<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>
	</filter>
	<filter-mapping>
		<filter-name>Set Character Encoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

 3.由于spring 3.0 里不在提供对jotm的封装,我们可以手动的编写加入代码如下

 

/*
 * Copyright 20010-2011
 */

package com.demo.spring.jotm;

import javax.naming.NamingException;
import javax.transaction.SystemException;

import org.objectweb.jotm.Current;
import org.objectweb.jotm.Jotm;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;

/**
 * @author fenglingcorp
 */
@SuppressWarnings("rawtypes")
public class JotmFactoryBean implements FactoryBean, DisposableBean {

	private Current jotmCurrent;

	private Jotm jotm;

	public JotmFactoryBean() throws NamingException {
		// Check for already active JOTM instance.
		this.jotmCurrent = Current.getCurrent();

		// If none found, create new local JOTM instance.
		if (this.jotmCurrent == null) {
			// Only for use within the current Spring context:
			// local, not bound to registry.
			this.jotm = new Jotm(true, false);
			this.jotmCurrent = Current.getCurrent();
		}
	}

	public void setDefaultTimeout(int defaultTimeout) {
		this.jotmCurrent.setDefaultTimeout(defaultTimeout);
		// The following is a JOTM oddity: should be used for demarcation
		// transaction only,
		// but is required here in order to actually get rid of JOTM's default
		// (60 seconds).
		try {
			this.jotmCurrent.setTransactionTimeout(defaultTimeout);
		} catch (SystemException ex) {
			// should never happen
		}
	}

	public Jotm getJotm() {
		return this.jotm;
	}

	public Object getObject() {
		return this.jotmCurrent;
	}

	public Class getObjectType() {
		return this.jotmCurrent.getClass();
	}

	public boolean isSingleton() {
		return true;
	}

	public void destroy() {
		if (this.jotm != null) {
			this.jotm.stop();
		}
	}

}

 4.加入jotm事务和xpool数据源

<!-- jotm -->
	<bean id="jotm" class="com.demo.spring.jotm.JotmFactoryBean" />

	<bean id="transactionManager"
		class="org.springframework.transaction.jta.JtaTransactionManager">
		<property name="userTransaction" ref="jotm" />
	</bean>

	<!-- StandardXAPoolDataSourc 数据源 -->
	<bean id="dataSource" class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
		<property name="dataSource">
			<bean id="innerDataSource" class="org.enhydra.jdbc.standard.StandardXADataSource">
				<property name="transactionManager">
					<ref local="jotm" />
				</property>
				<property name="driverName">
					<value>${datasource.driverClassName}</value>
				</property>
				<property name="url">
					<value>${datasource.g_db_w_url}</value>
				</property>
				<property name="user">
					<value>${datasource.g_db_w_username}</value>
				</property>
				<property name="password">
					<value>${datasource.g_db_w_password}</value>
				</property>
			</bean>
		</property>
		<property name="user">
			<value>${datasource.g_db_w_username}</value>
		</property>
		<property name="password">
			<value>${datasource.g_db_w_password}</value>
		</property>
		<property name="maxSize">
			<value>5</value>
		</property>
		<property name="minSize">
			<value>2</value>
		</property>
	</bean>

 5.加入aop支持

<aop:config>
		<aop:pointcut id="serviceMethods"
			expression="execution(* com.demo.spring.dao.*.*(..))" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
	</aop:config>
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" rollback-for="Exception" />
			<tx:method name="insert*" rollback-for="Exception" />
			<tx:method name="save*" rollback-for="Exception" />
			<tx:method name="update*" rollback-for="Exception" />
			<tx:method name="del*" rollback-for="Exception" />
			<tx:method name="remove*" rollback-for="Exception" />
			<tx:method name="*" read-only="true" rollback-for="Exception" />
		</tx:attributes>
	</tx:advice>

编写登入的类

package com.demo.spring.actions;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.demo.spring.forms.LoginForm;
import com.demo.spring.service.LoginServiceInterface;
import com.demo.spring.util.Constants;

@SuppressWarnings("deprecation")
public class LoginAction extends SimpleFormController {

	private LoginServiceInterface loginservice;

	public LoginServiceInterface getLoginservice() {
		return loginservice;
	}

	public void setLoginservice(LoginServiceInterface loginservice) {
		this.loginservice = loginservice;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		LoginForm loginForm = (LoginForm) command;
		if (isValid(loginForm)) {
			request.getSession().setAttribute(Constants.USERNAME_KEY,
					loginForm.getUsername());
			request.getSession().getServletContext()
			.getRequestDispatcher("/list.do?method=list")
			.forward(request, response);
			return null;
		} else {
			Map modle = errors.getModel();
			modle.put("loginForm", loginForm);
			return new ModelAndView(getFormView(), modle);
		}
	}

	public boolean isValid(LoginForm loginForm) {
		if (loginservice.isValid(loginForm.getUsername(),
				loginForm.getPassword())) {
			return true;
		} else {
			return false;
		}
	}
}

  

完整代码在附件中

你可能感兴趣的:(spring,AOP,mvc,bean,servlet)