zoukankan      html  css  js  c++  java
  • 在Struts2里面嵌入Spring

    第一步:在web.xml中增加以下的listener

    	<listener>
    		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    		<!-- 默认去这个地方找 spring的配置文件并载入   default: /WEB-INF/applicationContext.xml -->
    	</listener>
      <!--整个应用的參数配置,上面的listener启动时会去读这个配置-->
    	<context-param>
    		<param-name>contextConfigLocation</param-name>
    		<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  -->
    		<param-value>classpath:beans.xml</param-value>
    	</context-param>

    第二步:得让struts2找spring去要Action的生成

       先增加struts2-spring -plugin-2.1.6.jar

    然后在Action前面就能够加@Component 了

    @component("user")

    public class UserAction  extends ActionSupport{。。。省略。。。

    }

    @Component("user")
    @Scope("prototype")
    public class UserAction extends ActionSupport {
    	
    	private String username;
    	private String password;
    	private String password2;
    	
    	private UserManager um;
    	
    	
    	
    	public UserManager getUm() {
    		return um;
    	}
    	
    	@Resource(name="userManager")
    	public void setUm(UserManager um) {
    		this.um = um;
    	}
    
    	@Override
    	public String execute() throws Exception {
    		User u = new User();
    		u.setUsername(username);
    		u.setPassword(password);
    		if(um.exists(u)) {
    			return "fail";
    		}
    		um.add(u);
    		return "success";
    	}
    
    	public String getUsername() {
    		return username;
    	}
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getPassword2() {
    		return password2;
    	}
    
    	public void setPassword2(String password2) {
    		this.password2 = password2;
    	}

    struts2已启动就会读配置文件 (有多个。eg:struts2.xml,struts2-spring -plugin.xml.....),先读struts2-default.xml,然后struts2-spring-plugin.xml,再度struts.xml,再struts.properties,最后读web.xml   ,webApliacation 一启动。我们的spring容器就启动了  。然后去找配置文件(
    <param-value>classpath:beans.xml</param-value>

    ),找到之后,初始化bean。

    这样struts.xml 配置时 class="XXX",xxx就能够不是详细的Action类了,能够是和前面的name,也就是UserAction 前面的@Component("user")的user了。

    通过配置ContextLoaderListener监听器,使容器启动时,自己主动载入applicationContext配置,

    由于它实现了ServletContextListener这个接口。容器启动时会自己主动运行它实现的方法。


查看全文
  • 相关阅读:
    A working example of bad SQL statement causes too much CPU usage
    Notes on <<Refactoring Databases Evolutionary Database Design>>
    DBMS_SQL & ORA01000: Too many open cursors
    TTS, not used for schema exp/imp
    Notes on <High Performance MySQL> Ch6: Optimizing Server Settings
    Notes on <High Performance MySQL> Ch3: Schema Optimization and Indexing
    A bug of "sql*loader"?
    11.2.0.2 improves the support for MERGE statement
    Use "OR" in SQL with caution
    关于NORFLASH和NANDfLASH的区别。——Arvin
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10489032.html
  • Copyright © 2011-2022 走看看