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/。
2.修改默认值
要在<struts>标签下加<constant></constant>. constant里面name=”struts.action.extension”是默认值,不可以改,value=””可以更改(指更改默认后缀.action)
解释:上图中的valu=“do,action,,”只的是后缀可以说.do或.action或者是什么都不加的
3.result
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;
}}