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

Struts2

时间:2017-07-13 20:37:25      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:需要   结果   exce   map   play   str   bsp   exception   eth   

来自W3Cschool的学习。https://www.w3cschool.cn/struts_2/struts_examples.html

1. 主页 index.jsp (里面有一些form或者button,根据其action属性,到struts.xml配置文件中查找对应的Action类)

  <form action="hello">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>

2. 配置文件 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="cn.w3cschool.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

class属性:name="hello"的action,所对应的实体类为"cn.w3cschool.struts2.HelloWorldAction"。(带包名)

method属性:指定要执行HelloWorldAction类中的哪个方法。

result属性:根据HelloWorldAction类中execute()方法的返回值,来确定之后跳转的页面。

3. 结果页面 HelloWorld.jsp

根据实际需要,可以有多个处理结果和结果界面。

4. 项目配置文件 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
   
   <display-name>Struts 2</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.FilterDispatcher
      </filter-class>
   </filter>

   <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>
</web-app>

 标签属性之后完善。

5. 各Action类

package cn.w3cschool.struts2;

public class HelloWorldAction{
   private String name;

   public String execute() throws Exception {
      return "success";
   }
   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

 6. 日志文件

 

Struts2

标签:需要   结果   exce   map   play   str   bsp   exception   eth   

原文地址:http://www.cnblogs.com/zhengmengen/p/7153891.html

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