struts2 注解实现零配置实例

阅读更多
1新建一个web项目,添加struts2相关的包,在web.xml中配置struts2


  
  	struts2
  	
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	
  	
  	
  	
  	    actionPackages
  	    
  	    
  	    com.test.action.AnnotationAction
  	
  	
  
  
  	struts2
  	*.action
  
  
      struts2
      *.jsp
  


2. AnnotationAction.java
package com.test.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.dispatcher.ServletRedirectResult;

//命名空间
@Namespace(value="/test")
//结果集
@Results({
	//AnnotationAction传出参数"success"时,页面会跳转到index.jsp
	@Result(name="success",location="/index.jsp"),
	@Result(name="login",location="/login.jsp")
})

//@注解action为annotationAction,在jsp页面可以使用;若无此注解,
//jsp页面中可用action="annotation!add.action",即将Action去掉,首字母小写
@Action(value="annotationAction")
public class AnnotationAction extends ActionSupport{
	public String add(){
		return "success";
	}
	public String login(){
		return "login";
	}
}



3 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>




用户登录  

      
       
          
用户名:
密  码:
      

4. 登陆即可跳转到index.jsp

你可能感兴趣的:(struts,annotation,注解,零配置)