电影网站:www.aikan66.com

项目网站:www.aikan66.com
游戏网站:www.aikan66.com
图片网站:www.aikan66.com
书籍网站:www.aikan66.com
学习网站:www.aikan66.com
Java网站:www.aikan66.com
iOS网站:www.aikan66.com

----

Struts框架中通过Action的结果映射配置返回视图,Action对象是Struts2框架中的请求处理对象那,针对不同的业务请求及处理结果,Action将返回一个字符串,这个字符串就是Action处理结果的逻辑名,Struts2框架将更加逻辑视图名称,到配置文件struts.xml中查找逻辑视图名称匹配的视图,找到之后将这个视图回应给浏览器。

----

要求:编写Action对象,处理对表单提交的数据,模拟实现对指定用户的问候。

----

1、创建web项目,jwrm04-helloToSB,把包添加到lib,web.xml中注册过滤器。(详见web08)。

----

2、创建类GreetingAction的Action对象。

package dog;

import com.opensymphony.xwork2.ActionSupport;

public class GreetingAction extends ActionSupport{
private static final long serialVersionUID=1L; //用户名
private String username;
//处理请求
@Override
public String execute() throws Exception{
//判断用户名是否有效
if(username==null||"".equals(username)){
//返回到错误页面
return ERROR;
}else{
//返回到成功界面
return SUCCESS;
}
} //username的getter方法
public String getUsername(){
return username;
}
//username的setter方法
public void setUsername(String username){
this.username=username;
}
}

GreetingAction类用于对表单提交的username进行处理。

----

3、配置struts.xml

<?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="myPackage" extends="struts-default">
<!-- 定义action -->
<action name="greeting" class="dog.GreetingAction">
<!-- 定义成功的映射页面 -->
<result name="success">success.jsp</result>
<!-- 定义失败的映射页面 -->
<result name="error">error.jsp</result>
</action>
</package>
</struts>

就是说,当web应用访问目录下“greeting”时,将有GreetingAction类对请求作出处理。

----
4、index.jsp

<%@ 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="greeting.action" method="post">
请输入您的姓名:<input type="text" name="username">
<input type="submit" value="提交">
</form>
</body>
</html>

----

5、创建success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'success.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>
<font color="red">
<s:property value="username"/>
</font>
,您好!欢迎来到本站。
</body>
</html>

类似,创建error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'error.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>
<font color="red">
错误,您没有输入用户名!
</font>
</body>
</html>

----

6、部署,访问:http://localhost:8080/jwrm04-helloToSB/index.jsp

点击“提交”

输入框为空时点击“提交”

----

完毕

05-11 17:31