Struts2的零配置方法总结

以下内容参考网上的资料

间接实现零配置
   struts.xml

Java代码 复制代码
  1. <action name="*/*" method="{2}" class="workbench.web.actions.{1}Action">      
  2. <result name="custom">/view/{1}/${target}.jsp</result>   
  3. </action>  
<action name="*/*" method="{2}" class="workbench.web.actions.{1}Action">   
<result name="custom">/view/{1}/${target}.jsp</result>
</action>



编写一个基础的Action

Java代码 复制代码
  1. public abstract class BaseAction {          
  2. protected final String CUSTOM = "custom";   
  3. private String target;   
  4. protected final Log logger = LogFactory.getLog(getClass());       public String getTarget() {   
  5. return target;}   
  6. public void setTarget(String target) {   
  7. this.target = target;}                 
  8. protected String render(String _target){   
  9. setTarget(_target);   
  10. return CUSTOM;}   
  11. }   
public abstract class BaseAction {       
protected final String CUSTOM = "custom";
private String target;
protected final Log logger = LogFactory.getLog(getClass());       public String getTarget() {
return target;}
public void setTarget(String target) {
this.target = target;}              
protected String render(String _target){
setTarget(_target);
return CUSTOM;}
} 

 

Java代码 复制代码
  1. public class UserAction extends BaseAction{       private User user;   
  2. private UserService userService;   
  3. public void setUserService(UserService userService) {   
  4. this.userService = userService; }                 
  5. public User getUser() {   
  6. return user;}          
  7. public void setUser(User user) {   
  8. this.user = user; }          
  9. public String test(){   
  10. user = userService.get(1l);   
  11. return render("test"); }   
  12. }   
public class UserAction extends BaseAction{       private User user;
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService; }              
public User getUser() {
return user;}       
public void setUser(User user) {
this.user = user; }       
public String test(){
user = userService.get(1l);
return render("test"); }
} 


访问:http://localhost:8080/workbench/User/test

真正的夫配置,它是基于包的路径的
原文:http://www.iteye.com/post/390374
web.xml:

Java代码 复制代码
  1. <filter>      
  2.     <filter-name>struts2</filter-name>      
  3.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>      
  4.     <init-param>      
  5.         <param-name>actionPackages</param-name>      
  6.         <param-value>demo.actions1,demo.actions2</param-value>      
  7.     </init-param>      
  8. </filter>  
<filter>   
	<filter-name>struts2</filter-name>   
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
	<init-param>   
		<param-name>actionPackages</param-name>   
		<param-value>demo.actions1,demo.actions2</param-value>   
	</init-param>   
</filter>


Struts2扫描到Action后,从actionPackages指定的包开始,子包名会成为这个Action的namespace,而Action的name则由这个Action的类名决定。将类名首字母小写,如果类名以Action结尾,则去掉"Action"后缀,形成的名字就是这个Action的名字。在如上所述的示例中,actionPackages指定为demo.actions1,demo.actions2,那么你可以这样访问demo.actions1.app.person.ListPeopleAction:
http://localhost:8080/app/person/listPeople
Struts2是通过"Result"和"Results"两个类级别的annotations来指定Results的。
作为示例,我们在webapp目录下建两个html文件:success.html和error.html,随便写点什么内容都可以。现在假设我们访问/app/person/listPeople时,或Action返回success就转到success.html页面,若是error就转到error.html页面,这只需要在ListPeopleAction类上加上一段注解就可以了:

Java代码 复制代码
  1. @Results({      
  2.     @Result(name="success", type=NullResult.class, value = "/success.html", params = {}),      
  3.     @Result(name="error", type=NullResult.class, value = "/error.html", params = {})      
  4. })     
  5.     
  6. public class ListPeopleAction {      
  7.     public String execute() {      
  8.         return "success";      
  9.     }      

http://qichunren.iteye.com/blog/193386 

你可能感兴趣的:(apache,html,jsp,Web,struts)