struts---基本配置(以登录页面为例)
1、在web.xml文件定义核心 Filter来拦截用户请求:
<filter><?xml version="1.0" encoding="GBK"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID" version="3.0"><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><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
<s:form action="login"><s:textfield name="username" key="user"/><s:textfield name="password" key="pass"/><s:submit key="login"/></s:form>
public class LoginAction extends ActionSupport {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;}public String execute() throws Exception {if (getUsername().equals("") || this.getPassword().equals(""))return LOGIN;if (getUsername().equals("admin") && this.getPassword().equals("admin")) {return SUCCESS;} else {return ERROR;}}}
<?xml version="1.0" encoding="GBK"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><!-- 指定Struts 2配置文件的根元素 --><struts><constant name="struts.custom.i18n.resources" value="hulu"/><package name="hulu" extends="struts-default"><action name="login" />
以上步骤即可简单实现struts2的开发。