Struts2实现简单输入姓名后给出欢迎信息

本文介绍了一个简单的Struts2应用实例,通过创建一个包含用户输入姓名并显示问候消息的基本项目来演示Struts2框架的基本用法。项目包括index.jsp用于获取用户输入,msg.jsp用于显示结果,以及HelloAction2.java作为业务逻辑处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先建立项目,在lib目录下导入Struts2所需要的jar包:

两个jsp页面:index.jsp:用户输入你的名字。msg.jsp:输入您好,名字信息。
[color=blue]index.jsp[/color]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
<form action="hello2.action" type="post">
请输入你的大名:<input type="text" name="uname" value="" /><br>
<input type="submit" value="确定" />
</form>
</body>
</html>


[color=blue]msg.jsp[/color]
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
${msg }
</body>
</html>


[color=blue]HelloAction2.java[/color]
package com.topnet.af.Action;

public class HelloAction2 {

private String uname;
private String msg;

public String sayHello() {
msg = "你好:"+uname;
return "success";
}

public String getUname() {
return uname;
}

public void setUname(String uname) {
this.uname = uname;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}


[color=blue]struts.xml配置文件[/color]
<?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>
<!-- 命名空间 -->
<package name="struts2" extends="struts-default">
<action name="hello2" class="com.topnet.af.Action.HelloAction2" method="sayHello">
<result name="success">/msg.jsp</result>
</action>
</package>
</struts>

启动Tomcat服务器,运行即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值