码迷,mamicode.com
首页 > 其他好文 > 详细

写一个简单的struts2

时间:2017-09-05 12:28:26      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:doc   default   tsp   span   pat   http   soft   content   size   

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

标签:doc   default   tsp   span   pat   http   soft   content   size   

原文地址:http://www.cnblogs.com/wendy-depp/p/7477956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!