Struts1 基礎(三)

迎著太陽走向遠方發表於2017-03-27
注意:附件中有完整案例

1.建立一個struts1的登入例子
2.步驟

myeclipse -new--web project--strust1--專案右鍵--myeclipse-add struts capablities.. //這樣就可以了看到專案中加入了struts1的jar 和 struts-config.xml

3.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.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>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>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


4.struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
  <data-sources />
  <form-beans>
  	<form-bean name="loginForm" type="com.luob.struts.form.LoginForm"></form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings>
  	<action path="/login"
  	 attribute="loginForm"
  	 input="/login.jsp"
  	 name="loginForm"
  	 scope="request"
  	 type="com.luob.struts.action.LoginAction">
  	 <forward name="success" path="/success.jsp"/>
  	 <forward name="fail" path="/fail.jsp"/>
  	 </action>
  </action-mappings>
  <message-resources parameter="com.struts.ApplicationResources" />
</struts-config>


5.ActionForm

public class LoginForm extends ActionForm{

	private String userName;
	private String userPassword;
	
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
		
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPassword() {
		return userPassword;
	}
	public void setUserPassword(String userPassword) {
		this.userPassword = userPassword;
	}

}

6.Action

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginform=(LoginForm)form;
		UserManager um=new UserManager();
		boolean flag=um.userLogin(loginform.getUserName(),loginform.getUserPassword());
		String page="fail";
		if(flag){
			page="success";
		}
		return mapping.findForward(page);
	}	
}

7.common

public class UserManager {
	public boolean userLogin(String userName,String userPassword){
		boolean flag=false;
		if("admin".equals(userName)&&"10101".equals(userPassword)){
			flag=true;
		}
		return flag;
	}
}

相關文章