今天在写struts2的注解时遇到了低级错误下面给个分享:
总结action配置:
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;
}
}
这段配置相信大家都很熟悉,但是重点来了:
struts2默认会去找*.actions或*.action包下的action,这点很关键。
所以我总结了配置注解有两种方式:
A.将你的action文件放在*.actions或*.action包下就可以了,注解生效(注意一定是在*.actions或*.action包下哦)
B.你的action文件可以随便放,那么就需要在web.xml中配置参数:
<?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">
<!-- 添加struts2、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>
以上是这次低级错误的总结,希望给朋友带来帮助!
来源:oschina
链接:https://my.oschina.net/u/1016595/blog/122202