最近学Struts框架,其原理没有搞懂,那位帮助解释下
例子:
struts.xml片断
...
<package name="struts2" namespace="/mystruts" extends="struts-default">
<action name="sum" class="action.FirstAction">
<result name="positive">/positive.jsp</result>
<result name="negative">/negative.jsp</result>
</action>
</package>
...
----------------------------------------------------------------------------
FirstAction.java
package action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L;
private int operand1;
private int operand2;
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
this.operand1 = operand1;
}
public int getOperand2() {
return operand2;
}
public void setOperand2(int operand2) {
this.operand2 = operand2;
}
public String execute() throws Exception
{
if(getSum() >=0){
return "positive";
}
else
{
return "negative";
}
}
public int getSum() {
// TODO Auto-generated method stub
return operand1 + operand2;
}
}
------------------------------------------------------------------
sum.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>输入操作数</title>
</head>
<body>
求代数和
<br/>
<s:form action="mystruts/sum.action">
<s:textfield name="operand1" label="操作数1" />
<s:textfield name="operand2" label="操作数2" />
<s:submit value="代数和"/>
</s:form>
</body>
</html>
------------------------------------------------------------------
negative.jsp<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>显示代数和</title>
</head>
<body>
代数和为负整数
<h1><s:property value="sum"/> </h1>
</body>
</html>
----------------------------------------------------------------------
positive.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>显示代数和</title>
</head>
<body>
代数和为非负整数
<h1><s:property value ="sum" /> </h1>
</body>
</html>我想知道的是,最后页面上显示的结果的数是怎么得到地,即数据流向,即<s:property value ="sum" />表示什么意思,与<action name="sum" class="action.FirstAction">又是什么关系?因为还有一个例子如下:struts.xml片断:
.........
<package name="demo" extends="struts-default">
<action name="submit" class="action.MoreSubmitAction">
<result name="save">/result.jsp</result>
<result name="print">/result.jsp</result>
</action>
</package>
....
-----------------------------------------------MoreSubmitAction.java
package action;import javax.servlet.http.HttpServletRequest;import com.opensymphony.xwork2.ActionSupport;public class MoreSubmitAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String msg;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String save() throws Exception
{
request.setAttribute("result", "成功保存["+msg+"]");
return "save";
}
public String print() throws Exception
{
request.setAttribute("result", "成功打印["+msg+"]");
return "print";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}-----------------------------------------------
more_submit.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>My JSP 'hello.jsp' starting page</title>
</head>
<body><s:form action ="submit.action">
<s:textfield name="msg" label="输入内容"/>
<s:submit name="save" value="保存" align="left" method="save" />
<s:submit name="print" value="打印" align="left" method="print"/>
</s:form></body>
</html>
---------------------------------------------------
result.jsp<%@ page language="java" pageEncoding="GBK"%><html>
<head><title>提交结果</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>--------------------------------------
此时运行时,点按钮报错如下:
HTTP Status 404 - There is no Action mapped for namespace / and action name submit.--------------------------------------------------------------------------------type Status reportmessage There is no Action mapped for namespace / and action name submit.description The requested resource (There is no Action mapped for namespace / and action name submit.) is not available.
--------------------------------------------------------------------------------Apache Tomcat/7.0.27那位高手帮助指定下.目前看了很多有关SSH的书,总觉得结果得来莫名其妙地,小弟在此跪求赐教!!!
例子:
struts.xml片断
...
<package name="struts2" namespace="/mystruts" extends="struts-default">
<action name="sum" class="action.FirstAction">
<result name="positive">/positive.jsp</result>
<result name="negative">/negative.jsp</result>
</action>
</package>
...
----------------------------------------------------------------------------
FirstAction.java
package action;import com.opensymphony.xwork2.ActionSupport;public class FirstAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L;
private int operand1;
private int operand2;
public int getOperand1() {
return operand1;
}
public void setOperand1(int operand1) {
this.operand1 = operand1;
}
public int getOperand2() {
return operand2;
}
public void setOperand2(int operand2) {
this.operand2 = operand2;
}
public String execute() throws Exception
{
if(getSum() >=0){
return "positive";
}
else
{
return "negative";
}
}
public int getSum() {
// TODO Auto-generated method stub
return operand1 + operand2;
}
}
------------------------------------------------------------------
sum.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>输入操作数</title>
</head>
<body>
求代数和
<br/>
<s:form action="mystruts/sum.action">
<s:textfield name="operand1" label="操作数1" />
<s:textfield name="operand2" label="操作数2" />
<s:submit value="代数和"/>
</s:form>
</body>
</html>
------------------------------------------------------------------
negative.jsp<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>显示代数和</title>
</head>
<body>
代数和为负整数
<h1><s:property value="sum"/> </h1>
</body>
</html>
----------------------------------------------------------------------
positive.jsp
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>显示代数和</title>
</head>
<body>
代数和为非负整数
<h1><s:property value ="sum" /> </h1>
</body>
</html>我想知道的是,最后页面上显示的结果的数是怎么得到地,即数据流向,即<s:property value ="sum" />表示什么意思,与<action name="sum" class="action.FirstAction">又是什么关系?因为还有一个例子如下:struts.xml片断:
.........
<package name="demo" extends="struts-default">
<action name="submit" class="action.MoreSubmitAction">
<result name="save">/result.jsp</result>
<result name="print">/result.jsp</result>
</action>
</package>
....
-----------------------------------------------MoreSubmitAction.java
package action;import javax.servlet.http.HttpServletRequest;import com.opensymphony.xwork2.ActionSupport;public class MoreSubmitAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String msg;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
public String save() throws Exception
{
request.setAttribute("result", "成功保存["+msg+"]");
return "save";
}
public String print() throws Exception
{
request.setAttribute("result", "成功打印["+msg+"]");
return "print";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}-----------------------------------------------
more_submit.jsp<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>My JSP 'hello.jsp' starting page</title>
</head>
<body><s:form action ="submit.action">
<s:textfield name="msg" label="输入内容"/>
<s:submit name="save" value="保存" align="left" method="save" />
<s:submit name="print" value="打印" align="left" method="print"/>
</s:form></body>
</html>
---------------------------------------------------
result.jsp<%@ page language="java" pageEncoding="GBK"%><html>
<head><title>提交结果</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>--------------------------------------
此时运行时,点按钮报错如下:
HTTP Status 404 - There is no Action mapped for namespace / and action name submit.--------------------------------------------------------------------------------type Status reportmessage There is no Action mapped for namespace / and action name submit.description The requested resource (There is no Action mapped for namespace / and action name submit.) is not available.
--------------------------------------------------------------------------------Apache Tomcat/7.0.27那位高手帮助指定下.目前看了很多有关SSH的书,总觉得结果得来莫名其妙地,小弟在此跪求赐教!!!
解决方案 »
- 请教 acegi 权限设计 的问题
- 关于SOCKET 文件流的实现,希望CSDN的兄弟们帮帮忙
- 关于通过ip地址获取对应城市的问题。
- 请问我安装的weblogic的console中的左边的导航树怎么点击无效?
- hibernate执行10万条数据时候,要6秒?
- bean 自定义的事件怎样才能被调用?求求大家了回答一下吧?
- 请给一个TOMCAT的下载地址啊,谢谢!
- 怎样在客户端打印一个pdf文件
- jdon 上传图片有问题!!!(在线给分,不够再加,up也行)谢谢!~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 生成的链接怎么设置只允许访问一次
- 连接ActiveMQ的JMX问题
- JSONObject.toBean()方法报空指针异常
<s:textfield name="msg" label="输入内容"/>
<s:submit name="save" value="保存" align="left" method="save" />
<s:submit name="print" value="打印" align="left" method="print"/>
</s:form>这里action ="submit.action" 不对吧,在struts.xml中没有配置submit.action,而是配的submit
建议拿本书静下来看几天就都看懂了。拿网上的一段代码来问,没什么效果的。一点建议了。
<s:property value ="sum" />表示 会去调用bean的getSum() ,一定要有这个方法
和 <action name="sum" class="action.FirstAction">没有关系
我想知道的是,最后页面上显示的结果的数是怎么得到地,即数据流向,即<s:property value ="sum" />表示什么意思,与<action name="sum" class="action.FirstAction">又是什么关系?因为还有一个例子如下:
<s:property value ="sum" />表示输出一个key是sum的值,这个在action里保存在session里,或者一个map里,
<action name="sum" class="action.FirstAction">跟这个没有关系,顶多是在FirstAction类里,保存了一个key是sum的值,session,或者一个map。
action name="sum" class="action.FirstAction"这个表示 FirstAction这action,可以通过 访问 sum 比如:http://xx/sum.action ,这样就可调用FirstAction类里的excute()方法,<package name="demo" extends="struts-default">
<action name="submit" class="action.MoreSubmitAction">
<result name="save">/result.jsp</result>
<result name="print">/result.jsp</result>
像你这个,MoreSubmitAction里没有excute()方法,你要指定访问submit,时,调用哪个方法,
需要 <action name="submit" class="action.MoreSubmitAction" method = “save”>
你想要 在一个action,执行不通的方法,你那样是不行的,你要做通配,
通配很简单,看一书就明白了
你的struts可以写成这样
<action name="sum_*" class="action.FirstAction" method="{1}">
*是通配符,你写请求地址时用方法名代替,你请求的地址也有问题,没加上命名空间。
应该写成 /mystruts/sum_你要执行的方法名
form表单提交后 前台的值传递给action属性 ,属性接到值以后,action方法执行,对值进行处理,然后返回去某JSP页。
例如:action有一个属性name 。该action执行完毕以后去某JSP页,在该JSP页中,${name}或者<s:property value="name">都可以获取action中属性name的值
还有:action中执行代码 ActionContext.getContext().put("age",21),也可以将值放入候选列表,配合${name}或者<s:property value="age">同样能将值从action传递给JSP页面。对于<s:property value="name">和${name}来说 暂时不用去考虑底层执行原理,你也说了你是刚学框架 那就先学会Struts转发和重定向的流、值传递的流。弄熟练了 以后再慢慢研究吧
jsp--->Struts2
页面的请求发送到服务器由服务器容器管理如tomcat。这时web.xml知道你配的拦截器是struts2的请求拦截。就由struts2管理你的请求。struts2的请求默认是actionName or actionName.action,也可以自己配后缀。struts2明显的好处省去了写servlet的一大推getAttribute("name");在struts2应用在jsp页面上控件name属性值和action中的属性一致就行了(实际上是getter和setter方法后面的字母一致;比如 setAge()和页面的name="age"相匹配。只要请求不被拦截或者验证失败,就会得到值)然后就可以操作这些数据了!action的方法都有返回值,这和你struts2.xml配置相匹配。转发or重定向 etc 都在struts2.xml上体现。看看struts2的网站上面的例子和知识点!
第一个问题,这两个sum没有关系,<s:property value ="sum" />是取的action对象里的sum属性,而这个action对象可以是任意一个action,只要跳转到这个jsp处理的结果集页及可,只不过这个例子是actionName为sum跳到了此jsp页面,<action name="sum" class="action.FirstAction">定义了一个action,他的名称为sum,访问就使用sum,处理类为action.FirstAction。命名空间在pack里定义了。第二个错误应该是配置没有生效。可能是由于没有execute方法引起的
<action name="sum" class="action.FirstAction" method=“这里添加FirstAction类当中执行的方法,method的默认值是execute方法,即LZ在FirstAction必须要有execute方法,如果没有,可以指定其他的方法,否则出错
而下面的<result name="positive">,<result name="negative">当中的值就必须作为该方法的返回值
”>实例如下:
<package name="struts2" namespace="/mystruts" extends="struts-default">
<action name="sum" class="action.FirstAction" method=“abc”
<result name="positive">/positive.jsp</result>
<result name="negative">/negative.jsp</result></action>
public class FirstAction extends ActionSupport {
....
public String abc(){
if(...)
{...
return "positive";
//注意struts默认值为返回Success
//此时可以改为<result name="Success">/negative.jsp</result>
//或<result>/negative.jsp</result>
}
else
{
....
return "negative";
}
}
...
}
建议LZ 去看一下传值播客的SSH视频,他们是分开Spring hibernate struts2讲的,最后还有一个整合视频,很不错的 我就是看他们的视频学会的 一个人估计看的快1个月搞定了,千万别自己看书,很难懂的