初学Struts2

1、新建工程,引用Struts2

项目结构如下:

2、Web配置,web.xml:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <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>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>
复制代码

3、Struts配置:

复制代码
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="demo.controllers" extends="struts-default">
        <action name="HelloWord" class="demo.controllers.HelloWord"> --配置Action名称
            <result>helloword.jsp</result>  --配置action 执行成功后返回的页面
            <result name="error">error.jsp</result> --配置Action执行返回error时的页面
        </action>
    </package>
</struts>
复制代码

4、Controller:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package demo.controllers;
 
import com.opensymphony.xwork2.ActionSupport;
import java.text.DateFormat;
import java.util.Date;
 
/**
 * Created by Administrator on 2014/11/13.
 */
public class HelloWord extends ActionSupport{
 
    private String message;
 
    public String getMessage() {
        return message;
    }
 
    public String execute(){
        message="hello word now is:"+ DateFormat.getInstance().format(new Date());
        return ERROR;  --由Struts中HelloWord 中result name="error" 指定页面显示} }

 说明:Action返回一个结果的名字字符串,如SUCCESS,ERROR;从Struts中读取映射信息。一个给定的结果字符串将返回指定的资源返回给客户端

posted @   tyb1222  阅读(180)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示