<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<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>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<!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>
<!-- 注册ActionForm-->
<form-beans>
<!-- name:名称 type:类路径-->
<form-bean name="loginForm" type="com.login.LoginForm"></form-bean>
</form-beans>
<!-- 设置URL逻辑名称(全局),而且必须写在Action前面,任何Action都可以识别-->
<global-forwards>
<forward name="result" path="/result.jsp"></forward>
</global-forwards>
<!-- 注册Action-->
<action-mappings>
<!-- name:名称 type:类路径 path:客户端提交到服务器端时制定的路径(Form表单中的action="/login.do")-->
<action path="/login" type="com.login.LoginAction" name="loginForm" scope="request">
<!--设置URL逻辑名称(局部),只有这个Action可以识别到,然后将结果返回到指定的页面-->
<forward name="success" path="/login_success.jsp"></forward>
<forward name="error" path="/login_error.jsp"></forward>
</action>
<action path="/beanWrite" type="com.tag.BeanWriteAction">
<forward name="success" path="/tag_beanwrite.jsp"></forward>
</action>
<action path="/logic" type="com.tag.LogicAction">
<forward name="success" path="/tag_logic.jsp"></forward>
</action>
<action path="/iterator" type="com.tag.IteratorAction">
<forward name="success" path="/tag_iterator.jsp"></forward>
</action>
</action-mappings>
</struts-config>
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 10:24:42
*负责接收ActionForm中的数据,并作出处理
* 必须继承 org.apache.struts.action.Action
* 重写execute()处理业务逻辑
* 将Action注册到配置文件中
*/
public class LoginAction extends Action {
/**
* ActionForward:封装了跳转目标的路径
* mapping:访问配置文件
* form:传过来的ActionForm对象
* @param mapping
* @param form
* @param request
* @param response
* @return
* @throws Exception
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
LoginForm lf= (LoginForm) form;
String username=lf.getUsername();
//String username=request.getParameter("username");用此方法同样可以得到表单中的值,但如果用到Struts标签的话就必须用ActionForm了
String password=lf.getPassword();
String errorInfo="";
try{
UserManager.getInstance().login(username,password);
request.setAttribute("username",username);
//跳转
return mapping.findForward("success");
}catch(UserNotFoundException unfe){
unfe.printStackTrace();
errorInfo="用户不存在,输入的用户名=["+username+"]";
}catch(PasswordErrorException pee){
pee.printStackTrace();
errorInfo="密码错误";
}
request.setAttribute("errorInfo",errorInfo);
return mapping.findForward("error");
}
}
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import javax.servlet.http.HttpServletRequest;
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 10:18:47
* 保存表单提交的值
* 必须继承 org.apache.struts.action.ActionForm
* 必须编写和表单元素同名的属性,并且要有get()何set() 比如说表单中有username何password属性
* 必须在Struts1配置文件中进行注册
*/
public class LoginForm extends ActionForm {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
private String password;
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------reset()--------------");
super.reset(mapping,request);
}
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
System.out.println("-------validate()----------");
return super.validate(mapping, request); //To change body of overridden methods use File | Settings | File Templates.
}
}
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 10:36:03
* To change this template use File | Settings | File Templates.
*/
public class UserManager {
private static UserManager instance=new UserManager();
public static UserManager getInstance() {
return instance;
}
public void login(String username,String password){
if(!"admin".equals(username)){
throw new UserNotFoundException();
}
if(!"admin".equals(password)){
throw new PasswordErrorException();
}
}
}
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 10:39:48
* To change this template use File | Settings | File Templates.
*/
public class PasswordErrorException extends RuntimeException{
}
/**
* Created by IntelliJ IDEA.
* User: wangchenyang
* Date: 2010-11-30
* Time: 10:39:25
* To change this template use File | Settings | File Templates.
*/
public class UserNotFoundException extends RuntimeException{
}