Struts2基础
hello world 实例
jsp页面:
<%@ page contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head></head> <body> <h1>Struts 2 Hello World Example</h1> <s:form action="Welcome"> <s:textfield name="username" label="Username" /> <s:password name="password" label="Password" /> <s:submit /> </s:form> </body> </html>
action类:
public class WelcomeUserAction { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } // all struts logic here public String execute() { return "SUCCESS"; } }
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="user" namespace="/User" extends="struts-default"> <action name="Login"> <result>/login.jsp</result> </action> <action name="Welcome" class="com.yiibai.user.action.WelcomeUserAction"> <result name="SUCCESS">/welcome_user.jsp</result> </action> </package> </struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.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>*.action</url-pattern> </filter-mapping></web-app>