下面是我做的相关程序!!在程序中的struts-config.xml
<struts-config>
<form-beans>
<form-bean name="userForm" type="test.UserForm"/>
</form-beans>
<global-forwards>
<forward name="failed" path="/failed.do"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<action-mappings>
<action path="/regist" type="test.RegistAction" name="userForm" scope="request"
input="/index.jsp"/>
<action path ="/overview" forward="/hello.jsp"/>
<action path ="/failed" forward="wpsing.jsp"/>
</action-mappings>
</struts-config>下面是web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config><session-timeout>
30
</session-timeout></session-config><welcome-file-list><welcome-file>
index.jsp
</welcome-file></welcome-file-list></web-app>
下面是一个ActionForm
package test;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;/**
*
* @author Administrator
*/
public class UserForm extends ActionForm{
private User user=new User();
public void setName(String s){
this.user.setName(s);}
public String getName()
{
return this.user.getName();}
public void setPs(String s)
{
this.user.setPs(s);}
public String getPs()
{
return this.user.getPs();}
public void setUser(User user)
{
this.user=user;
}
public User getUser(){
return this.user;}
}
下面是一个Action
package test;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import test.UserForm;public final class RegistAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages=getResources(request);
HttpSession session = request.getSession();
UserForm userform =(UserForm) form;
User user=userform.getUser();
request.setAttribute("user",user);
if ("lpw".equals(userform.getName()))
return (mapping.findForward("failed"));
else
return (mapping.findForward("regist"));
}
}
还有几个jsp页面程序hello.jsp,index.jsp,regist.jsp,wpsing.jsp
如下:hello.jsp
<%@page contentType="text/html" language="java"%>
<%@page 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>hello Page</title>
</head>
<body>
<h1>site map</h1>
The following is content filling by reader
</body>
</html>
index.jsp程序:
<%@page contentType="text/html" language="java"%>
<%@page import="test.*"%>
<%@page 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>index Page</title>
</head>
<body>
<h1>index Page</h1>
<br/>
<a href="./overview.do">站点导航</a>
<form action="regist.do" method="post">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="ps"><br>
<input type=submit value="新增用户">
</form>
</body>
</html>
regist.jsp程序
<%@page contentType="text/html" language="java" %>
<%@page pageEncoding="UTF-8"%>
<jsp:useBean id="userform" class="test.UserForm" scope="session"/><!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>regist Page</title>
</head>
<body> <h1>regist Page</h1>
新用户帐号:<%=userform.getName()%><br>
密码:<%=userform.getPs()%>
</body>
</html>wpsing.jsp程序:
<%@page contentType="text/html" language="java"%>
<%@page 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>wpsing page</title>
</head>
<body>
<h1>wpsing Page</h1>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
现有用户:<%=beanlpw.getName()%><br>
密码:<%=beanlpw.getPs()%><br>
</body>
</html>
我要想得到的结果是通过表单,改变name与ps的值!!但是值总是null,不知道那里错了,请高手指教!!!
<struts-config>
<form-beans>
<form-bean name="userForm" type="test.UserForm"/>
</form-beans>
<global-forwards>
<forward name="failed" path="/failed.do"/>
<forward name="regist" path="/regist.jsp"/>
</global-forwards>
<action-mappings>
<action path="/regist" type="test.RegistAction" name="userForm" scope="request"
input="/index.jsp"/>
<action path ="/overview" forward="/hello.jsp"/>
<action path ="/failed" forward="wpsing.jsp"/>
</action-mappings>
</struts-config>下面是web.xml
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<session-config><session-timeout>
30
</session-timeout></session-config><welcome-file-list><welcome-file>
index.jsp
</welcome-file></welcome-file-list></web-app>
下面是一个ActionForm
package test;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionMapping;/**
*
* @author Administrator
*/
public class UserForm extends ActionForm{
private User user=new User();
public void setName(String s){
this.user.setName(s);}
public String getName()
{
return this.user.getName();}
public void setPs(String s)
{
this.user.setPs(s);}
public String getPs()
{
return this.user.getPs();}
public void setUser(User user)
{
this.user=user;
}
public User getUser(){
return this.user;}
}
下面是一个Action
package test;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import test.UserForm;public final class RegistAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
Locale locale = getLocale(request);
MessageResources messages=getResources(request);
HttpSession session = request.getSession();
UserForm userform =(UserForm) form;
User user=userform.getUser();
request.setAttribute("user",user);
if ("lpw".equals(userform.getName()))
return (mapping.findForward("failed"));
else
return (mapping.findForward("regist"));
}
}
还有几个jsp页面程序hello.jsp,index.jsp,regist.jsp,wpsing.jsp
如下:hello.jsp
<%@page contentType="text/html" language="java"%>
<%@page 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>hello Page</title>
</head>
<body>
<h1>site map</h1>
The following is content filling by reader
</body>
</html>
index.jsp程序:
<%@page contentType="text/html" language="java"%>
<%@page import="test.*"%>
<%@page 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>index Page</title>
</head>
<body>
<h1>index Page</h1>
<br/>
<a href="./overview.do">站点导航</a>
<form action="regist.do" method="post">
用户:<input type="text" name="name"><br>
密码:<input type="password" name="ps"><br>
<input type=submit value="新增用户">
</form>
</body>
</html>
regist.jsp程序
<%@page contentType="text/html" language="java" %>
<%@page pageEncoding="UTF-8"%>
<jsp:useBean id="userform" class="test.UserForm" scope="session"/><!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>regist Page</title>
</head>
<body> <h1>regist Page</h1>
新用户帐号:<%=userform.getName()%><br>
密码:<%=userform.getPs()%>
</body>
</html>wpsing.jsp程序:
<%@page contentType="text/html" language="java"%>
<%@page 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>wpsing page</title>
</head>
<body>
<h1>wpsing Page</h1>
<jsp:useBean id="beanlpw" class="test.UserForm" scope="session"/>
现有用户:<%=beanlpw.getName()%><br>
密码:<%=beanlpw.getPs()%><br>
</body>
</html>
我要想得到的结果是通过表单,改变name与ps的值!!但是值总是null,不知道那里错了,请高手指教!!!
解决方案 »
- 怎么让CXF生成的wsdl带有参数名称
- window.location.href在IE中失效
- 论坛不支持js代码,怎么办?
- 关于java调用存储过程
- 用jspsmartuplaod组件,实现下载功能,可以正常下载,但是如果让保存时点击取消的话,则会出现异常!
- 散分:畅所欲言,大家看看这我样的水平在北京行不行
- Jsp中如何用getProperty得到数组的值
- 一个jsp关于tomcat4,beans,oracledriver的问题,online给分
- 写入文本文件的问题!!!
- 再利用JavaBean连接查询数据库时的一些疑问
- 我向oracle的一个字段下,要插入数据,如果数据超过4000个字节该如何处理(java)
- 关于加http://头的问题
public void setUser(User user)
{
this.user=user;
}
改为 :
public void setName(String s)
{
this.user.setName(s);
}
如果你用的JBUILDER的话见意这么去加BEAN:
点工作区下面的BEAN再点PROPERTIES,再点ADD PROPERTY去添加PROPERTY