一口一口吃掉Struts(三)——一个简单Struts实例
了解了Struts的基本流程,那么我们利用Struts来实现一个简单的登陆功能:
先用时序图来描述一下登录功能的处理流程:
核心操作:
1、配置struts * 拷贝struts相关的jar到WEB-INF/lib下 * 在web.xml文件中配置ActionServlet * 提供struts-config.xml文件,需要放到WEB-INF下
2、实现 *建立login.jsp/loginSuccess.jsp/loginFail.jsp *建立了LoginAction.java/userActionForm.java *将LoginAction/LoginActionForm/相关jsp配置到struts-config.xml文件中
代码:
配置文件
- <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>2</load-on-startup>
- </servlet>
- <!-- Standard Action Servlet Mapping -->
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
JSP(View)
- <form action="login.do" method="post">
- <input type="text" name="name" />
- <br />
- <input type="password" name="password" />
- <input type="submit" value="登录"/>
- </form>
LoginSuccess.jsp
- <body>
- 登录成功》》》》》》》》》》》》
- </body>
LoginFail.jsp
- <body>
- 登录失败》》》》》》》》》》》》
- </body>
LoginAction
- package com.jialin;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.apache.struts.action.Action;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- /**
- * 登录action
- * @author jialin
- *作用:取得表单数据,调用model层业务逻辑,返回转向信息
- */
- public class LoginAction extends Action {
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- UserActionForm userForm=(UserActionForm)form;
- String userName=userForm.getName();
- int password=userForm.getPassword(); //这里不用我们手动强转类型了。
- UserManage userManage=new UserManage();//后面我们再改进
- User user=new User();
- user.setName(userName);
- user.setPassword(password);
- try {
- userManage.ValidateUser(user);
- return mapping.findForward("success");
- } catch (UserNotFoundException e) {
- e.printStackTrace();
- request.setAttribute("message", e.getMessage());
- }catch(PasswordErrorException e){
- e.printStackTrace();
- request.setAttribute("message", e.getMessage());
- }
- return mapping.findForward("fail"); }
- }
struts-config.xml
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
- "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
- <struts-config>
- <!-- set ActionForm info-->
- <form-beans>
- <form-bean name="userForm" type="com.jialin.UserActionForm" />
- </form-beans>
- <action-mappings>
- <!-- Set path,action,actionform,scope,forward info -->
- <action path="/login" type="com.jialin.LoginAction" name="userForm"
- scope="request">
- <forward name="success" path="/LoginSuccess.jsp" />
- <forward name="fail" path="/LoginFail.jsp" />
- </action>
- </action-mappings>
- </struts-config>
UserActionForm
- package com.jialin;
- import org.apache.struts.action.ActionForm;
- /**
- * 用户ActionForm ,负责收集表单数据
- * @author jialin
- *注:get和set后的字符串首字母变小写后,必须和表单中属性的名字一致
- */
- public class UserActionForm extends ActionForm {
- private String name;
- private int password;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPassword() {
- return password;
- }
- public void setPassword(int password) {
- this.password = password;
- }
- }
UserManage(Model)
- package com.jialin;
- /**
- * MODEL层业务逻辑
- * @author jialin
- *判断用户是否合法
- */
- public class UserManage {
- public void ValidateUser(User user) {
- // 判断用户名密码是否正确
- if (!"jialin".equals(user.getName())) {
- throw new UserNotFoundException("用户不存在!用户名为:" + user.getName());
- } else if (user.getPassword() != 123456) {
- throw new PasswordErrorException("密码错误");
- }
- }}
用户实体和异常类省略。
上述实现过程,其中ActionServlet的功能,被Struts封装了,所以我们的代码部分,并没有涉及到ActionServlet的部分。
分析代码可知道,Action不再需要我们手动创建,读取配置文件等基础服务已被Struts封装了,表单中数据类型也不需要我们手动转换了,页面转向可以进行配置,也不需要我们手动去配置文件中取了。
可以说,Struts把公共的服务为封装好了,我们只需要做很少的事,就可以获得很大的灵活性。
下篇继续!
huidaoli版权所有:转载请注明出处,谢谢合作!