导包:struts2-core-2.5.1
写action类,
package web; public class HelloWorldAction { private String name; public String execute(){ return "success"; } public String getName() { return name; } public void setName(String name) { this.name = name; }
index.jsp 里action的路径
<%@ page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <html> <head> </head> <body style="font-size:30px;"> <h1>Hello World From Struts2</h1> <form action="helloworld/hello"> <label for="name">Please enter your name</label><br/> <input type="text" name="name"/> <input type="submit" value="Say Hello"/> </form> </body> </html>
返回的jsp页面里taglib命令
<%@ page pageEncoding="utf-8" contentType="text/html;charset=utf-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body style="font-size:30px;"> Hello World, <s:property value="name"/> </body> </html>
Web.xml里的配置
<filter> <display-name>StrutsPrepareAndExecuteFilter</display-name> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>StrutsPrepareAndExecuteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Struts.Xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name="helloworld" extends="struts-default"> <action name="hello" class="web.HelloWorldAction" method="execute"> <result name="success">/WEB-INF/jsp/hello.jsp</result> </action> </package> </struts>
时间: 2024-11-07 15:33:53