Struts2支持使用注解配置Action,减少配置文件的配置
Struts2如果要支持注解配置Action,需要插件的支持,导入插件struts2-convention-plugin-2.1.8.1.jar
项目目录树:
1.导入struts2需要的基本包
2.修改web.xml,让struts2拦截Action
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>s2_1</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3.提供实体类User.java
package com.orange.domain; public class User { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4.提供Action类LoginAction.java
package com.orange.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import com.orange.domain.User; //默认可以不写 @ParentPackage("struts-default") //根命名空间,可以不写 @Namespace("/") //全局配置,如果方法上不指定result,则使用该Result //@Results({@Result(name="success",location="/success.jsp"), // @Result(name="error",location="/error.jsp")}) public class LoginAction extends ActionSupport{ private static final long serialVersionUID = 1L; private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } //@Action(value="login2") @Action(value="login2" ,results={@Result(name="success",location="/success.jsp"), @Result(name="error",location="/error.jsp")}) public String login() throws Exception{ if("admin".equals(this.user.getUserName()) && "admin".equals(this.user.getPassword())){ return SUCCESS; } return ERROR; } }
5.配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true" /> <constant name="struts.configuration.xml.reload" value="true" /> </struts>
6.提供相关的jsp
login.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> <form action="login2" method="post"> User:<input type="text" name="user.userName"><br> Password:<input type="text" name="user.password"><br> <input type="submit" value="submit"> </form> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> Success! </body> </html>
error.jsp
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> </head> <body> Error! </body> </html>
7.开始测试
输入admin/admin
配置成功!!!