首页 技术 正文
技术 2022年11月10日
0 收藏 591 点赞 2,586 浏览 3381 个字

1.在Struts配置文件的<package   中的 namespace默认是namesopace=”/”.

当生成namespace=“abc/”(abc是自己定义的,类似于html里的css样式中的class)那么在index.jsp测试跳转的<a  href =”abc/test01.action”></a>

  注意:.action后缀,在没有更改默认的情况下,可以不写,默认就是.action。

   这时候就必须在前面加abc/   解释:也就是当action在处理请求的时候是寻找abc/下的test01.action,然后执行系一部的转发跳转操作。

  在Struts.xml 配置文件的<action   中的 name=“test01”前面可以不加abc/。

Struts2整理+课堂代码+注意事项

2.修改默认值

要在<struts>标签下加<constant></constant>.   constant里面name=”struts.action.extension”是默认值,不可以改,value=””可以更改(指更改默认后缀.action)

Struts2整理+课堂代码+注意事项

解释:上图中的valu=“do,action,,”只的是后缀可以说.do或.action或者是什么都不加的

3.result

Struts2整理+课堂代码+注意事项

result中的name=“success”是默认返回值,类型是String型

4.action 中的class指的是com.hanqi.action包下的TestAction类,method=”test”指的是类中的test方法

具体代码如下:

 

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
首页
<br><br><a href="abc/test01.do" rel="external nofollow" >测试页面01</a><br><br>
<a href="test02.action" rel="external nofollow" >测试页面02</a></body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
测试页面01</body>
</html>

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录页
<br><br>
<form action="Login.action" method="post">用户名:<input type="text" name="username" />
<br><br>
密码:<input type="password" name="password" />
<input type="submit" value="登录">
</form>
</body>
</html>

  

<?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>
<!-- 修改默认值 -->
<constant name="struts.action.extension" value="do,action,,"></constant><!-- package对功能模块进行分组管理 -->
<package name="index" extends="struts-default"
namespace="/"> <!-- action 处理的请求 --> <!-- action 处理的默认请求 -->
<action name="test01" class="com.opensymphony.xwork2.ActionSupport"
method="execute">
<result name="success">/WEB-INF/pages/test01.jsp</result>
</action> <action name="test02" class="com.hanqi.action.TestAction"
method="test">
<result name="ok" >/WEB-INF/pages/test02.jsp</result>
<result name="notok">/WEB-INF/pages/error.jsp</result> </action>
<!-- 登入 -->
<action name="Login" class="com.hanqi.action.TestAction"
method="login">
<result>/WEB-INF/pages/Main.jsp</result>
<result name="fail" >/WEB-INF/pages/error.jsp</result>
</action> </package></struts>

  

package com.hanqi.action;
//自定义的Action 类
public class TestAction {//Action 方法
public String test()
{
int i=1;
if(i==1)
{
return "ok";
}
else
{
return "notok";
}
}//验证登录的方法
public String login ()
{
String rtn ="fail";//定义返回值,<result name="fail">
//验证登录
System.out.println("username="+username+ " password="+password);if(username!=null&&password!=null
&&username.equals("admin")&&password.equals("123456"))
{
rtn="success";
}
return rtn;
}private String username;
private String password;public String getUsername() {
return username;
}public void setUsername(String username) {
this.username = username;
}public String getPassword() {
return password;
}public void setPassword(String password) {
this.password = password;
}}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297