大家好,我正在與struts 2.x合作,並且對於這個框架非常新穎。我遵循教程和步驟列表以及我的應用程序正常工作,但有一件事對我來說並不清楚就像我在Action類中聲明一個專用字段並使用struts標記可以訪問jsp頁面的專用字段。那麼它是如何發生的,是不是可以理解爲我:在struts 2.x的jsp頁面上如何訪問私有字段?
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="default" namespace="/home" extends="struts-default">
<action name="message" class="com.csc.action.MessageAction">
<result name="hello">/hello.jsp</result>
<result name="bye">/bye.jsp</result>
</action>
<action name="add" class="com.csc.action.Mathaction">
<result name="success">/hello.jsp</result>
<result name="fail">/bye.jsp</result>
</action>
</package>
</struts>
MessageAction.java
package com.csc.action;
import com.csc.service.BussniessServ;
public class MessageAction {
private String result;
private String value;
private String fstvalue;
private String scndvalue;
public String getFstvalue() {
return fstvalue;
}
public void setFstvalue(String fstvalue) {
this.fstvalue = fstvalue;
}
public String getScndvalue() {
return scndvalue;
}
public void setScndvalue(String scndvalue) {
this.scndvalue = scndvalue;
}
// method which get value from input parameter
public String getValue() {
return value;
}
// method which show the value of input parameter
public void setValue(String value) {
this.value = value;
}
// method which run as default and execute
public String execute() {
BussniessServ serv = new BussniessServ();
setMessag(serv.Addition(fstvalue, scndvalue));
return "hello";
}
// method to show message on jsp page
public String getResult() {
return result;
}
// method for save message
public void setResult(String result) {
this.result = result;
}
}
的Result.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:property value="result"/>
</body>
</html>
你說的是什麼意思?你訪問有訪問者,獲取者或設置者。 – 2014-10-01 08:52:11