1. 程式人生 > >寫一個簡單的struts2

寫一個簡單的struts2

return 簡單的 index taglib struts2 text apach prepare mil

導包: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>

寫一個簡單的struts2