我觉得学习struts,看来最麻烦的就是struts的标签。
这东西,如果要美工页面,那么美工还得要有struts标签基础。
不过以前看过一些例子,好像页面不用struts标签,也能实现。但是我不知道是怎么做的,以前看过没保密下来。
今天抱着试试的心态,做了一下,好像很有问题。例子很简单
hello.jsp
<logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present> <html:form action="/HelloWorld.do" focus="userName" > <bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/><br> <html:submit property="submit" value="Submit"/>
<html:reset/> </html:form>
我把他换成 <logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present><form action="/HelloWorld.do" method="post">
<input name="userName" type="text">
<input name="" type="submit" value="aaa">
<input name="" type="reset" value="bbb">
</form>
就表单地方换成,一般html写法。于是出错了。
type Status reportmessage /HelloWorld.dodescription The requested resource (/HelloWorld.do) is not available.
不知道怎么回事,我的配置文件:
<action-mappings>
<!-- Say Hello! -->
<action path = "/HelloWorld"
type = "hello.HelloAction"
name = "HelloForm"
scope = "request"
validate = "true"
input = "/hello.jsp"
>
<forward name="SayHello" path="/hello.jsp" />
</action>
</action-mappings>class文件package hello;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;public final class HelloAction extends Action { /**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { // These "messages" come from the ApplicationResources.properties file
MessageResources messages = getResources(request); /*
* Validate the request parameters specified by the user
* Note: Basic field validation done in HelloForm.java
* Business logic validation done in HelloAction.java
*/
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName(); String badUserName = "Monster"; if (userName.equalsIgnoreCase(badUserName)) {
errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} /*
* Having received and validated the data submitted
* from the View, we now update the model
*/
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.saveToPersistentStore(); /*
* If there was a choice of View components that depended on the model
* (or some other) status, we'd make the decision here as to which
* to display. In this case, there is only one View component.
*
* We pass data to the View components by setting them as attributes
* in the page, request, session or servlet context. In this case, the
* most appropriate scoping is the "request" context since the data
* will not be neaded after the View is generated.
*
* Constants.PERSON_KEY provides a key accessible by both the
* Controller component (i.e. this class) and the View component
* (i.e. the jsp file we forward to).
*/ request.setAttribute( Constants.PERSON_KEY, pb); // Remove the Form Bean - don't need to carry values forward
request.removeAttribute(mapping.getAttribute()); // Forward control to the specified success URI
return (mapping.findForward("SayHello")); }
}
这东西,如果要美工页面,那么美工还得要有struts标签基础。
不过以前看过一些例子,好像页面不用struts标签,也能实现。但是我不知道是怎么做的,以前看过没保密下来。
今天抱着试试的心态,做了一下,好像很有问题。例子很简单
hello.jsp
<logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present> <html:form action="/HelloWorld.do" focus="userName" > <bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/><br> <html:submit property="submit" value="Submit"/>
<html:reset/> </html:form>
我把他换成 <logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present><form action="/HelloWorld.do" method="post">
<input name="userName" type="text">
<input name="" type="submit" value="aaa">
<input name="" type="reset" value="bbb">
</form>
就表单地方换成,一般html写法。于是出错了。
type Status reportmessage /HelloWorld.dodescription The requested resource (/HelloWorld.do) is not available.
不知道怎么回事,我的配置文件:
<action-mappings>
<!-- Say Hello! -->
<action path = "/HelloWorld"
type = "hello.HelloAction"
name = "HelloForm"
scope = "request"
validate = "true"
input = "/hello.jsp"
>
<forward name="SayHello" path="/hello.jsp" />
</action>
</action-mappings>class文件package hello;import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;public final class HelloAction extends Action { /**
* Process the specified HTTP request, and create the corresponding HTTP
* response (or forward to another web component that will create it).
* Return an <code>ActionForward</code> instance describing where and how
* control should be forwarded, or <code>null</code> if the response has
* already been completed.
*/
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception { // These "messages" come from the ApplicationResources.properties file
MessageResources messages = getResources(request); /*
* Validate the request parameters specified by the user
* Note: Basic field validation done in HelloForm.java
* Business logic validation done in HelloAction.java
*/
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName(); String badUserName = "Monster"; if (userName.equalsIgnoreCase(badUserName)) {
errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
} /*
* Having received and validated the data submitted
* from the View, we now update the model
*/
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.saveToPersistentStore(); /*
* If there was a choice of View components that depended on the model
* (or some other) status, we'd make the decision here as to which
* to display. In this case, there is only one View component.
*
* We pass data to the View components by setting them as attributes
* in the page, request, session or servlet context. In this case, the
* most appropriate scoping is the "request" context since the data
* will not be neaded after the View is generated.
*
* Constants.PERSON_KEY provides a key accessible by both the
* Controller component (i.e. this class) and the View component
* (i.e. the jsp file we forward to).
*/ request.setAttribute( Constants.PERSON_KEY, pb); // Remove the Form Bean - don't need to carry values forward
request.removeAttribute(mapping.getAttribute()); // Forward control to the specified success URI
return (mapping.findForward("SayHello")); }
}
解决方案 »
- quartz spring hibernate session问题
- Struts2 拦截器和AOP有什么区别?
- struts1的form和hibernate的table bean的问题
- 关于hibernate连接数据库的问题
- 高分求JSP的或者CGI开发的Blog论坛源码,最好是JSP的
- 怎样将POWERDESIGN中设计的表的中文名和英文名导入到WORD中。
- 谁能解答我的疑问呢?
- jsp与标识符
- 请问刚开始学习JSP和JAVA,用什么书好?
- 向高手请教,jsp环境问题。
- 请教大家一个非常简单的问题(Struts中消息资源bundle指定问题),即时结贴!
- 为什么我的javabean总是会出现异常
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;public final class HelloForm extends ActionForm { private String userName = null; public String getUserName() {
return (this.userName);
} public void setUserName(String userName) {
this.userName = userName;
}
/**
* Reset all properties to their default values.
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
} /**
* Validate the properties posted in this request. If validation errors are
* found, return an <code>ActionErrors</code> object containing the errors.
* If no validation errors occur, return <code>null</code> or an empty
* <code>ActionErrors</code> object.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error")); return errors;
}
}
<html:form action="/HelloWorld.do" focus="userName" >
</html:form>这个是必需的,别的可以换
如果你的ActionForm的名字是HelloForm ,那么就应该这样写<form name="helloForm" action="hello.do">
<text name="ActionForm的属性名字">
</form>
The requested resource (/HelloWorld.do) is not available.不过没关系,总之写成
<html:form action="/HelloWorld.do" focus="userName" ></html:form>
就行了,也不影响美工。