1.写一个注册页面,把请求交给 struts处理
1 <form action="${pageContext.request.contextPath }/Register.do" method="post"> 2 用户名:<input type="text" name="username"><br/> 3 密码:<input type="password" name="password"><br/> 4 邮箱:<input type="text" name="email"><br/> 5 <input type="submit" value="注册"> 6 </form>
2.导入struts开发包,并在web.xml文件配置struts(ActionServlet)处理所有*.do请求
/WebRoot/WEB-INF/web.xml
1 <servlet> 2 <servlet-name>ActionServlet</servlet-name> 3 <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> 4 <init-param> 5 <param-name>config</param-name> 6 <param-value>/WEB-INF/struts-config.xml</param-value> 7 </init-param> 8 <load-on-startup>2</load-on-startup> 9 10 </servlet> 11 12 <servlet-mapping> 13 <servlet-name>ActionServlet</servlet-name> 14 <url-pattern>*.do</url-pattern> 15 </servlet-mapping>
3.在web-inf目录中加入struts的配置文件:struts-config.xml,并配置struts收到请求后找RegisterAction处理,并配置在找RegisterAction处理请求之前,把数据封装到formbean中
/WebRoot/WEB-INF/struts-config.xml
打开的时候,右键Open With->MyEclipse XML Editor
1 <struts-config> 2 <form-beans> 3 <form-bean name="UserFormBean" type="cn.itcast.web.formbean.UserFormbean"></form-bean> 4 </form-beans> 5 6 <!-- 配置struts收到请求后找一个action处理 --> 7 8 <action-mappings> 9 <action path="/Register" type="cn.itcast.web.action.RegisterAction" name="UserFormBean"></action> 10 </action-mappings> 11 12 </struts-config>
4.把封装数据formbean,以及处理请求的RegisterAction写出来
cn.itcast.web.action.RegisterAction
cn.itcast.web.formbean.UserFormBean