1复制项目,HeadFirstStruts2chapter02_06 改名:HeadFirstStruts2chapter02_07,同时修改web project settings
2修改HelloAction,name属性,get() set()方法,
package com.cruise.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
3struts.xml如下:
<?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="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.cruise.action.HelloAction" >
<result name="success" type="dispatcher" >success.jsp</result>
</action>
</package>
</struts>
4修改success.jsp 通过${name}取值
<%@ 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>
name:${name}
</body>
</html>
5新建index.jsp 文件,UTF-8,标签,
<%@ 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>
<a href="hello?name=cruise" target="_blank">内部转发</a>
</body>
</html>
ps:如果是转发,success.jsp 里面是可以渠道${name}的值的,如果是重定向就取不到了
6测试 http://localhost:8080/HeadFirstStruts2chapter02_07/
7修改struts.xml的标签的type属性,改成重定向方式(struts默认是转发方式),本次对比测试采用动态方法调用的方式
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<package name="manage" namespace="/" extends="struts-default">
<action name="hello" class="com.cruise.action.HelloAction" >
<result name="success" type="dispatcher" >success.jsp</result>
<result name="redirect2" type="redirect" >success.jsp</result>
</action>
</package>
</struts>
8HelloAction 添加redirect()方法
package com.cruise.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String execute() throws Exception {
return SUCCESS;
}
public String redirect(){
return "redirect2";
}
}
9修改index.jsp文件,增加重定向的按钮,采用动态方法调用的方式,
<%@ 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>
<a href="hello?name=cruise" target="_blank">内部转发</a>
<a href="hello!redirect?name=cruise2" target="_blank">重定向</a>
</body>
</html>
10测试,比较第6步,${name}取不到值
第14讲 struts2中struts.xml中的标签配置
最新推荐文章于 2022-02-13 11:31:50 发布