struts 搭建成功

来源:互联网 发布:rds数据库 编辑:程序博客网 时间:2024/05/17 08:53

1 下载包,贴到WebContent/WEB-INF/lib 下面  

2 写下jsp  login.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
 <form action="LoginAction.action">
      name<input name="username"><br>
       pw<input type="password" name="userpass"><br>
      <input type="submit" value="login">
      <input type="reset"  value="cancel">
    </form>
</body>
</html>

welcome.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
welcome
</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<!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=EUC-KR">
<title>Insert title here</title>
</head>
<body>
error
</body>
</html>

3 写下类

package struts2demo;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport{
 private String username;
  private String userpass;
 
  public String execute(){
   if("1".equals(username)&&"1".equals(userpass))
    return SUCCESS;
   else
    return ERROR;
  }
 
  public String getUsername() {
   return username;
  }
  public void setUsername(String username) {
   this.username = username;
  }
  public String getUserpass() {
   return userpass;
  }
  public void setUserpass(String userpass) {
   this.userpass = userpass;
  }
}

4 写在重要的配置  struts.xml 放在src下

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="struts2demo" extends="struts-default">
   <action name="LoginAction" class="struts2demo.LoginAction">
     <result name="success">/welcome.jsp</result>
     <result name="error">/error.jsp</result>
   </action>
 </package>

</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="
http://xmlns.jcp.org/xml/ns/javaee"
            xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
   
    <!-- struts2 filter -->
 <filter>
       <filter-name>struts2</filter-name>
       <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher            
       </filter-class>
    </filter>
    <filter-mapping>
       <filter-name>struts2</filter-name>
       <url-pattern>/*</url-pattern>
    </filter-mapping>

 
</web-app>


这样就ok了

本人验证过

开始 struts吧  加油!!!!





0 0
原创粉丝点击