四.源码示例
Struts环境搭建:
1.首先要下载struts的jar包,地址http://struts.apache.org/
2.解压后将lib文件夹里的jar包都加入到项目中
3.在webapps文件夹中解压struts-blank.war,将其中的struts-config.xml拷贝到项目中
4.配置web.xml部署ActionServlet。这个格式是相对固定的,所以一般复制即可:
<?xml version="1.0"encoding="UTF-8"?> <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"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <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> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
5.配置struts-config.xml部署Action、ActionForm。
其中type均为你项目中Action和ActionForm所在包的完整路径;
name可以随便起,但是Action中的name必须与它用到的ActionForm的name相一致;
action中要添加scope="request"这个属性,因为scope默认是session,这个可以根据实际情况而定。
<?xml version="1.0"encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache SoftwareFoundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-beanname="loginForm" type="com.dan.struts.LoginActionForm"/> </form-beans> <action-mappings> <actionpath="/login" type="com.dan.struts.LoginAction" name="loginForm" scope="request" > <forwardname="success" path="/login_success.jsp"/> <forwardname="error"path="/login_error.jsp"/> </action> </action-mappings> </struts-config>
源码:
LoginAction.java
package com.dan.struts; 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; public class LoginAction extends Action { @Override public ActionForwardexecute(ActionMapping mapping, ActionFormform, HttpServletRequestrequest, HttpServletResponseresponse) throwsException { LoginActionFormlaf = (LoginActionForm)form; Stringusername =laf.getUsername(); Stringpassword =laf.getPassword(); UserManageruserManager = newUserManager(); try{ userManager.login(username,password); returnmapping.findForward("success"); }catch(Exception e){ e.printStackTrace(); } returnmapping.findForward("error"); } }
LoginActionForm.java
package com.dan.struts; import org.apache.struts.action.ActionForm; public class LoginActionForm extends ActionForm{ private Stringusername; private String password; public String getUsername(){ returnusername; } public void setUsername(String username){ this.username= username; } public String getPassword(){ returnpassword; } public void setPassword(String password){ this.password=password; } }
UserManager.java
package com.dan.struts; public class UserManager { public void login(String username, String password)throws Exception{ if(!"admin".equals(username) || !"admin".equals(password)) { thrownewException(); } } }
login.jsp
<body> <formaction="login.do"method="post"> 用户:<input type="text"name="username"><br> 密码:<input type="password"name="password"></br> <inputtype="submit" value="登录"> </form> </body>
login_success.jsp
<body> ${loginForm.username},登录成功 </body>
login_error.jsp
<body> 登录失败! </body>
通过上面对Struts的原理的讲解,我们可以看到,Struts其实就是对Model2中的Servlet的职责又进行了划分,它将一些框架处理部分(如截取URL,分发等)放到ActionServlet进行封装,使我们只需要关心页面和具体的页面流转实现,服务更加人性化。