今天在写struts2的注解时遇到了低级错误下面给个分享:
总结action配置:
01 | package com.test.web.actons; |
03 | import org.apache.struts2.convention.annotation.Action; |
04 | import org.apache.struts2.convention.annotation.Namespace; |
05 | import org.apache.struts2.convention.annotation.Result; |
07 | import com.opensymphony.xwork2.ActionSupport; |
09 | @Namespace (value= "/test" ) |
10 | public class TestAction extends ActionSupport{ |
11 | private static final long serialVersionUID = 2118537853660540192L; |
13 | @Action (value= "login" ,results={ @Result (location= "bookList.jsp" )}) |
14 | public String login() throws Exception{ |
15 | System.out.println( "hello struts2!!" ); |
这段配置相信大家都很熟悉,但是重点来了:
struts2默认会去找*.actions或*.action包下的action,这点很关键。
所以我总结了配置注解有两种方式:
A.将你的action文件放在*.actions或*.action包下就可以了,注解生效(注意一定是在*.actions或*.action包下哦)
B.你的action文件可以随便放,那么就需要在web.xml中配置参数:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < web-app version = "2.5" |
03 | xmlns = "http://java.sun.com/xml/ns/javaee" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation="http://java.sun.com/xml/ns/javaee |
06 | http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> |
09 | < filter-name >struts</ filter-name > |
10 | < filter-class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class > |
12 | < param-name >actionPackages</ param-name > |
13 | < param-value >com.test.servlet</ par <span></ span >< span ></ span >am-val< span ></ span >ue> |
17 | < filter-name >struts</ filter-name > |
18 | < url-pattern >/*</ url-pattern > |
21 | < welcome-file >index.jsp</ welcome-file > |
以上是这次低级错误的总结,希望给朋友带来帮助!