MyEclipse+Tomcat+mysql+struts2

MyEclipse配置Tomcat(图解) :http://shz2008bj.iteye.com/blog/166721%20

MyEclipse开发web项目(jsp+Servlet):http://blog.sina.com.cn/s/blog_628cc2b70100jrpa.html%20

MyEclipse开发web项目(jsp+Servlet):http://blog.sina.com.cn/s/blog_628cc2b70100p5ip.html

struts2 框架下MyEclipse编写HelloWorld程序:http://blog.sina.com.cn/s/blog_628cc2b70100p7zg.html%20

使用Struts2开发Java Web应用程序(目录) :http://blog.csdn.net/struts2/article/details/1721752

Maven起步-教你开始使用Maven:http://www.blogjava.net/i369/articles/86037.html

MyEclipse连接MySQL的方法  :http://blog.163.com/w375257253@126/blog/static/17048785520105365416661/

个人心得:

调用http://xujiaxuan-pc:8080/struts2_demo/login.action 可以直接被拦截,并被转发到jsp界面

成功例子:java案例教程的3章+myeclipse 

注意要点

1.原来是配置文件struts.xml应该放在src下。同时web.xml放在webroot下

2.eclipse或myeclipse调试没有source code 可能是每个类都继承自object有些没办法看到具体的初始化。继续f5就好了

步骤:

(1)myeclipse10

myeclipse破解参考网址:http://blog.csdn.net/ll136078/article/details/7197809

myeclipse10版本的下载:

http://downloads.myeclipseide.com/downloads/products/eworkbench/indigo/installers/myeclipse-10.0-offline-installer-windows.exe

 破解补丁以及下载地址:http://pan.baidu.com/share/link?shareid=165650&uk=1678594189

(2)mysql 官方网址:http://www.mysql.com

mysql驱动程序:http://pan.baidu.com/share/link?shareid=165653&uk=1678594189

1.正常安装

2.关于mysql的使用:直接点击MYSQL->MySQL Server 5.5->mysql 5.5 command line client

3.输入密码 (默认的用户名为root) 或者

开始,运行,cmd [ ,cd  mysql安装目录/bin ],  mysql -u userName -p Password

 

(3)struts2官方网址:http://struts.apache.org/download.cgi#struts2211

书籍推荐:《java高级编程框架应用开发案例教程》(含code+ppt):http://pan.baidu.com/share/link?shareid=165674&uk=1678594189

例子:

文件结构

mysql:

View Code
create database acesys;
use acesys;
create table Usr(
id int  primary key,
username nvarchar(50),
password nvarchar(50),
fullname nvarchar(50),
title nvarchar(50),
companyname nvarchar(50),
companyaddress nvarchar(50),
city nvarchar(50),
job nvarchar(50),
email nvarchar(50),
country nvarchar(50),
zip nvarchar(50),
tel nvarchar(50),
superuser nvarchar(50),
delsoft nvarchar(50),
note nvarchar(50))insert into Usr values (4,"1","1","1","1","1","1","1","1","1","1","1","1","p","1","1");

struts.xml:

View Code
<?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="struts2" extends="struts-default">
        <action name="usrLoginAction" class="com.ascent.struts2.action.UsrLoginAction">
            <result name="anli_success_1">/registUsrWelcome.jsp</result>
            <result name="anli_success_2">/registUsrWelcome.jsp</result>
            <result name="anli_success_3">/adminWelcome.jsp</result>
            <result name="anli_error">/login.jsp</result>
        </action>
    </package>
</struts>

web.xml

View Code
<?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">
      <display-name>Struts 2.0</display-name>
    <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>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

DBConn

View Code
package com.ascent.struts2.anli;
import java.sql.*;
public class DBConn {

    public static Connection getConn()
    {
    Connection con=null;
    try 
    {
    //Class。forName("com.mysql.jdbc.Driver");
    Class.forName("com.mysql.jdbc.Driver");
    String url="jdbc:mysql://localhost:3306/acesys";
    String user="root";
    String password="root";
    con=DriverManager.getConnection(url,user,password);
    }
    catch(ClassNotFoundException e){
        System.out.println("---找不到到驱动器");
    }
    catch (SQLException e)
    {
        System.out.println("获得数据库连接失败");
    }
    return con;
    }
    
    public static void dbClose(Connection con,Statement st,ResultSet rs){
        try{
            if(rs!=null)
            {
                rs.close();
                rs=null;
            }
            if(st!=null){
                st.close();
                st=null;

            }
            if(con!=null)
            {
                con.close();

            con=null;}
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}

UsrDAO

View Code
package com.ascent.struts2.anli;
import java.util.ArrayList;
import java.util.List;
import com.ascent.struts2.po.Usr;
import java.sql.*;

public class UsrDAO {
    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;
    public Usr checkUsr(String username,String password){
        Usr u=null;
        String sql="select * from Usr";
        try{
            con=DBConn.getConn();
            ps=con.prepareStatement(sql);
            //ps.setString(1, username);
            //ps.setString(2,password);
            rs=ps.executeQuery();
            if(rs.next())
            {
                u=new Usr();
                u.setId(rs.getInt("id"));
                u.setUsername(rs.getString("username"));
                u.setEmail(rs.getString("email"));
                u.setTel(rs.getString("tel"));
                u.setSuperuser(rs.getString("superuser"));
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally{
        DBConn.dbClose(con,ps,rs);
        }
        return u;
    }
}

Usr

View Code
package com.ascent.struts2.po;
/**
 * Usr entity. @author MyEclipse Persistence Tools
 */
public class Usr implements java.io.Serializable {
    // Fields
    private int id;
    private String username;
    private String password;
    private String fullname;
    private String title;
    private String companyname;
    private String companyaddress;
    private String city;
    private String job;
    private String tel;
    private String email;
    private String country;
    private String zip;
    private String superuser;
    private String delsoft;
    private String note;
    // Constructors
    /** default constructor */
    public Usr() {
    }
    /** full constructor */
    public Usr(String username, String password, String fullname, String title,
            String companyname, String companyaddress, String city, String job,
            String tel, String email, String country, String zip,
            String superuser, String delsoft, String note) {
        this.username = username;
        this.password = password;
        this.fullname = fullname;
        this.title = title;
        this.companyname = companyname;
        this.companyaddress = companyaddress;
        this.city = city;
        this.job = job;
        this.tel = tel;
        this.email = email;
        this.country = country;
        this.zip = zip;
        this.superuser = superuser;
        this.delsoft = delsoft;
        this.note = note;
    }

    // Property accessors

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFullname() {
        return this.fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCompanyname() {
        return this.companyname;
    }

    public void setCompanyname(String companyname) {
        this.companyname = companyname;
    }

    public String getCompanyaddress() {
        return this.companyaddress;
    }

    public void setCompanyaddress(String companyaddress) {
        this.companyaddress = companyaddress;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getJob() {
        return this.job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getTel() {
        return this.tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getEmail() {
        return this.email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getCountry() {
        return this.country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getZip() {
        return this.zip;
    }

    public void setZip(String zip) {
        this.zip = zip;
    }

    public String getSuperuser() {
        return this.superuser;
    }

    public void setSuperuser(String superuser) {
        this.superuser = superuser;
    }

    public String getDelsoft() {
        return this.delsoft;
    }

    public void setDelsoft(String delsoft) {
        this.delsoft = delsoft;
    }

    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }

}

login.jsp

View Code
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%
String path=request.getContextPath();
String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC"-/W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> 
<base href="<%=basePath%>">
<title>MY JSP 'LOGIN.JSP'STARTING PAGE</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="usrLoginAction.action" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>

index.jsp

View Code
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>

regisUsrWelcome.jsp

View Code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'welcome.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
regisusr welcome
  </body>
</html>

adminWelcome.jsp

View Code
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'adminWelcome.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
  <body>
admin welcome
  </body>
</html>

error.jsp

View Code
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'error.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
    Struts2Demo<br>
    登陆失败!
  </body>
</html>


运行结果:

修改成下面的网址后显示为:

输入用户名以及密码后:

 

 

posted @ 2012-12-14 20:17  xjx_user  阅读(609)  评论(0编辑  收藏  举报