第02講 struts2的get和set方法自動獲取和自動設定資料
阿新 • • 發佈:2018-11-10
第二章設定資料
1,複製專案HeadFirstStruts2chapter01 改名為:HeadFirstStruts2chapter02。修改Web Project Settings 為HeadFirstStruts2chapter02,HelloWorldAction中設定成員變數name屬性,get()和set()方法,struts.xml保持不變。
HelloWorldAction.java如下:
package com.cruise.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
this
}
@Override
public String execute() throws Exception {
System.out.println("struts2的預設的方法執行了");
return "success";
}
}
struts.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="suibiandingyi" extends="struts-default">
<action name="hello" class="com.cruise.action.HelloWorldAction">
<result name="success">helloworld.jspresult>
action>
package>
struts>
2, 修改HelloWorld.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 heretitle>
head>
<body>
你好:歡迎${name}來到Cruise技能學習網!
body>
html>
3,測試
4,結論,通過攔截器,先執行set方法賦值,然後執行get方法返回給頁面
5,在WebContent下,建立index.jsp檔案,寫form表單提交,
<%@ 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>
<form action="hello" method="post">
name:<input type="text" name="name"/><input type="submit" value="提交">
</form>
</body>
</html>
6,測試,
點選提交:
7,結論同上第4條