第14講 struts2中struts.xml中的標籤配置
阿新 • • 發佈:2018-11-11
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
public String getName() {
return name;
}
public void setName(String name) {
this
}
@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}取不到值