helloword入门系列之struts1简单跳转—附源码

好久没写blog了,新年的第一篇,写点入门的东西,其实struts1已经很久不用了,最近发现就算是以前会的东西用的时候也会忘得差不多了,现在用struts1的人不多了,不过做为一个精典的框架,还是写出来留做一个备忘吧,也给新人当做个入门的东西。明天加入了dao数据访问层的。

项目结构以及引用到的包


代码只有关键部分,如果要细看的话,就下载整个工程看吧。

web.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>3</param-value>  
    </init-param>  
    <init-param>  
      <param-name>detail</param-name>  
      <param-value>3</param-value>  
    </init-param>  
    <load-on-startup>0</load-on-startup>  
  </servlet>  

struts-config.xml

<struts-config>  
  <form-beans >  
    <form-bean name="userForm" type="com.mefly.struts1.form.UserForm" ></form-bean>  
  </form-beans>  
  <global-exceptions />  
  <global-forwards />  
  <action-mappings >  
    <action   
      path="/userAction"  
      type="com.mefly.struts1.action.UserAction"  
      name="userForm"  
      parameter="method"  
      scope="request"  
      validate="true"  
      input="/login.jsp"  
      >  
      <forward name="login" path="/login.jsp"/>  
      <forward name="success" path="/success.jsp"/>
   </action>  
  </action-mappings>  
  <message-resources parameter="com.mefly.struts1.ApplicationResources" />  
</struts-config>  

from

public class UserForm extends ActionForm{  
	 private int id;  
	 private String username;  
	 private String password;  

 action

package com.mefly.struts1.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

import com.mefly.struts1.form.UserForm;

public class UserAction extends DispatchAction {

	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		UserForm userForm = (UserForm) form;
		if ("mefly".equals(userForm.getUsername())
				&& "123".equals(userForm.getPassword())) {
			return mapping.findForward("success");//用户名密码正确的话跳转到成功页
		}
		
		ActionMessages message = new ActionMessages(); 
		message.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("exception_10001")); 
		saveMessages(request,message); 
		return mapping.findForward("login");
	}

}

 login.jsp

 <form action="userAction.do" method="post">  
输入mefly/123才会跳转:<br/>  
  <input type="hidden" name="method" value="login" />   
  用户名:<br/>  
  <input type="text" name="username" id="username" /><br/>  
  密码:<br/>  
  <input type="password" name="password" id="password" /><br/>  
  <input type="submit" value="提交" />  
  <input type="reset" value="重置"/>  
  <br/>
<html:messages id="messages" message="true">
		<bean:write name="messages" />
	</html:messages>
 </form>  

没有什么过多需求解释的,成功跳到success,失败跳到login.jsp,还会显示错误信息,错误信息写在资源文件里了。

action 继承了 DispatchAction。

猜你喜欢

转载自mefly.iteye.com/blog/1379278