struts2框架搭建

1.新建javaweb项目,命名为S2并创建包,导入jar包

2.db.properties:

url=jdbc:mysql://localhost:8080/sshtest
type=mysql
user=root

psw=root

3.web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>S2</display-name>
  <filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


 
  <welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>

4.DbInt:

package com.util;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;


public interface DbInt {
public Connection getConncetion();


public void closeConnection(Connection conn);


public void close(PreparedStatement pstate, Statement state, ResultSet rs);

}

5.DbImpl:

package com.util;



import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;




public class DbImpl implements DbInt {
public Connection getConncetion() {
Connection conn = null;
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/db.properties");
try {
prop.load(in);
String url = prop.getProperty("url");
String user = prop.getProperty("user");
String psw = prop.getProperty("psw");
switch (prop.getProperty("type")) {
case "mysql":
Class.forName("com.mysql.jdbc.Driver");//加载驱动
conn = DriverManager.getConnection(url, user, psw);// 建立连接;
break;
default:
System.out.println("数据库配置文件错误!");
break;
}
prop.clear();
in.close();
} catch (IOException | ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}


public void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


public void close(PreparedStatement pstmt, Statement state, ResultSet rs) {
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (state != null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


6.SelectDao:

package com.dao;


import java.util.List;


import com.model.TestInfor;


public interface SelectDao {
//查询
public List<TestInfor> select();
//修改
public boolean update(TestInfor testInfor);
//查询详情
public TestInfor selectid(int id);

}

7SelectImpl:

package com.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.dao.SelectDao;
import com.model.TestInfor;
import com.util.DbImpl;
import com.util.DbInt;




public class SelectImpl implements SelectDao{
Connection conn = null;
String sql = "";
PreparedStatement pstmt = null;
ResultSet rs = null;
@Override
public List<TestInfor> select() {
DbInt db = new DbImpl();
conn = db.getConncetion();
sql = "select * from testuserinfo";
List<TestInfor> selectlist=new ArrayList<TestInfor>();
TestInfor testInfor=null;
try {
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
while (rs.next()) {
testInfor = new TestInfor();
testInfor.setId(rs.getInt("id"));
testInfor.setName(rs.getString("name"));
testInfor.setAge(rs.getInt("age"));
testInfor.setImg(rs.getString("img"));
selectlist.add(testInfor);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.close(pstmt, null, rs);
db.closeConnection(conn);
}
return selectlist;
}
@Override
public boolean update(TestInfor testInfor) {
DbInt db = new DbImpl();
conn = db.getConncetion();
sql = "update testuserinfo set img=? where id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, testInfor.getImg());
pstmt.setInt(2, testInfor.getId());
if (pstmt.executeUpdate()>0) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.close(pstmt, null, rs);
db.closeConnection(conn);
}
return false;
}
@Override
public TestInfor selectid(int id) {
DbInt db = new DbImpl();
conn = db.getConncetion();
sql = "select * from testuserinfo where id=?";
TestInfor testInfor=null;
try {

pstmt = conn.prepareStatement(sql);

                        pstmt.setInt(1, id);

rs = pstmt.executeQuery();
if (rs.next()) {
testInfor = new TestInfor();
testInfor.setId(rs.getInt("id"));
testInfor.setName(rs.getString("name"));
testInfor.setAge(rs.getInt("age"));
testInfor.setImg(rs.getString("img"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
db.close(pstmt, null, rs);
db.closeConnection(conn);
}
return testInfor;
}


}

8.SelectAction:

package com.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;


import org.apache.struts2.ServletActionContext;


import com.dao.SelectDao;
import com.dao.impl.SelectImpl;
import com.model.TestInfor;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class SelectAction extends ActionSupport{
SelectDao sDao=new SelectImpl();
List<TestInfor> selectlist;
private TestInfor testInfor;
private int id;
private File upload;
private String uploadFileName;
private String savePath;
public String select(){
selectlist=sDao.select();
return SUCCESS;
}
public String update() throws IOException{
TestInfor testInfor1=sDao.selectid(testInfor.getId());
savePath=ServletActionContext.getServletContext().getRealPath("/img");
if (upload!=null) {
String nameType=getUploadFileName().substring(getUploadFileName().lastIndexOf(".") + 1,
getUploadFileName().length());
setUploadFileName(testInfor.getId()+"img."+nameType);
FileOutputStream fos=new FileOutputStream(getSavePath() + "/" + getUploadFileName());
FileInputStream fis = new FileInputStream(upload);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
fis.close();
testInfor.setImg("img/"+uploadFileName);
}else{
testInfor.setImg(testInfor1.getImg());
}
sDao.update(testInfor);
return SUCCESS;
}
public String selectid(){
testInfor=sDao.selectid(id);
return SUCCESS;
}
public List<TestInfor> getSelectlist() {
return selectlist;
}
public void setSelectlist(List<TestInfor> selectlist) {
this.selectlist = selectlist;
}
public TestInfor getTestInfor() {
return testInfor;
}
public void setTestInfor(TestInfor testInfor) {
this.testInfor = testInfor;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}

}

9.TestInfor:

package com.model;


public class TestInfor {
private int id;
private String name;
private int age;
private String img;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public TestInfor(int id, String name, int age, String img) {
super();
this.id = id;
this.name = name;
this.age = age;
this.img = img;
}
public TestInfor() {
super();
}



}

10.struts.xml:

<?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="default" extends="struts-default" namespace="/">
<action name="select" class="com.action.SelectAction" method="select">
<result name="success">index.jsp</result>
</action>
<action name="update" class="com.action.SelectAction" method="update">
<result name="success" type="redirectAction">select</result>
</action>
<action name="selectid" class="com.action.SelectAction" method="selectid">
<result name="success">selectid.jsp</result>
</action>
</package>
</struts>

11.index.html:

<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
123
</body>

</html>

12.index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:iterator value="selectlist">
<ul>
<li>id:<s:property value="id"/></li>
<li>name:<s:property value="name"/></li>
<li>age:<s:property value="age"/></li>
<li>img:<img src="<s:property value="img"/>"></li>
<li><a href="selectid?id=<s:property value="id"/>">详情</a></li>
</ul>
</s:iterator>
</body>

</html>

13.selectid.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="update" method="post" enctype="multipart/form-data">
<s:iterator var="testInfor">
<ul>
<li><input type="text" name="testInfor.id" value="<s:property value="testInfor.id"/>"/></li>
<li><input type="text" name="testInfor.name" value="<s:property value="testInfor.name"/>"/></li>
<li><input type="text" name="testInfor.age" value="<s:property value="testInfor.age"/>"/></li>
<li><img src="<s:property value="testInfor.img"/>"></li>
<li><input type="file" name="upload"></li>
</ul>
</s:iterator>
<input type="submit" name="submit" value="修改">
</form>
</body>

</html>

14.完整项目下载链接:http://download.csdn.net/download/lxjhzy/10271442

项目中包含创建表的语句

猜你喜欢

转载自blog.csdn.net/lxjhzy/article/details/79453232