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/LoginForm.java
*将LoginAction/LoginActionForm/相关jsp配置到struts-config.xml文件中

 

配置文件

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">
    <servlet>

        <servlet-name>action</servlet-name>
        <!-- 引入Servlet的包所有的路径 -->
        <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>
        <!-- 当项目一启动就实例化servlet -->
        <load-on-startup>0</load-on-startup>
    </servlet>

    <!-- 对外提供服务 -->
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <!-- 对所有的请求过滤 -->
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

</web-app>

 

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
  
<!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>  
    <form-beans>  
        <form-bean name="loginForm" type="cn.itcast.LoginForm">  
        </form-bean>
    </form-beans>  
          
    <action-mappings>  
        <action path="/login" type="cn.itcast.LoginAction" name="loginForm">  
            <forward name="success" path="/loginSuccess.jsp" />  
            <forward name="fail" path="/loginFail.jsp" />  
        </action>  
    </action-mappings>
  
</struts-config>

 

 login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form action="<%=request.getContextPath()%>/login.do" method="post">
        name:<input type="text" name="name" />  
        <br />  
        password:<input type="password" name="password" />  
        <input type="submit" value="LOGIN"/>  
    </form>
</body>
</html>

loginFail.jsp

<%@ page language="java" contentType="charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    LoginFail
</body>
</html>

loginSuccess.jsp

<%@ page language="java" contentType="charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    LoginSuccess
</body>
</html>

LoginAction

package cn.itcast;

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 ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        LoginForm loginform = (LoginForm) form;
        
        String keyword = "fail";
        if (loginform.getName().equals("itcast")) {
            keyword = "success";
        } 
        
        return mapping.findForward(keyword);
    }
}

LoginForm

package cn.itcast;

import org.apache.struts.action.ActionForm;

public class LoginForm 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;
    }  

}

 

posted @ 2017-07-31 17:52  紫华  阅读(128)  评论(0编辑  收藏  举报