struts2註解配置注意事項

今天在寫struts2的註解時遇到了低級錯誤下面給個分享: java

    總結action配置: web

package com.test.web.actons;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import com.opensymphony.xwork2.ActionSupport;

@Namespace(value="/test")
public class TestAction extends ActionSupport{
	private static final long serialVersionUID = 2118537853660540192L;
	
	@Action(value="login",results={@Result(location="bookList.jsp")})
	public String login() throws Exception{
		System.out.println("hello struts2!!");
		return SUCCESS;
	}
}

這段配置相信你們都很熟悉,可是重點來了: apache

    struts2默認會去找*.actions或*.action包下的action,這點很關鍵。 app

因此我總結了配置註解有兩種方式: jsp

    A.將你的action文件放在*.actions或*.action包下就能夠了,註解生效(注意必定是在*.actions或*.action包下哦) url

    B.你的action文件能夠隨便放,那麼就須要在web.xml中配置參數: spa

 

<?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">
 	<!-- 添加struts二、sitemesh支持 -->
    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param> 
			<param-name>actionPackages</param-name> 
			<param-value>com.test.servlet</param-value> 
		</init-param> 
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


 

 

 

 

 以上是此次低級錯誤的總結,但願給朋友帶來幫助! code

相關文章
相關標籤/搜索