コード:JSP Webページを実行すると、Javaで記述された関数をbroswerが見つけることができないのはなぜですか?
printUserTable.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page pageEncoding="UTF-8" %>
<html>
<head>
<title>USETABLE</title>
<link rel="stylesheet" href="css/searchPage.css">
</head>
<body>
<form action="PrintUserTable.action" namespace="/" method="post">
<input type="submit" value="打印表格"/>
</form>
<table class="hovertable">
<tr><th colspan="5">UserTable in biosql</th></tr>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>tel</th>
<th>mail</th>
</tr>
</table>
<%
List list = null;
//判断session里list是否为空
if(session.getAttribute("USERTABLEINFO") != null){
list = (List)session.getAttribute("USERTABLEINFO");
out.print(list);
//判断list中的数据size是否大于0
if(list.size()>0){
//遍历List中的数据
UserTable userTable;
int idNum = 0;
for (int i = 0; i < list.size(); i++) {
userTable = new UserTable();
userTable = (UserTable) list.get(i);
%>
<tr onmouseover="this.style.backgroundColor = '#ffff66';"
onmouseout="this.style.backgroundColor = '#d4e3e5';">
<td><%=userTable.getId()%></td>
<td><%=userTable.getUsername()%></td>
<td><%=userTable.getPassword()%></td>
<td><%=userTable.getTel()%></td>
<td><%=userTable.getMail()%></td>
</tr>
<%
}
}
}
%>
</body>
</html>
サーブレット:
package com.eodream.service;
import com.eodream.model.UserTable;
import com.eodream.util.ConnectionFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Andyliwr on 2016/4/16.
*/
public class UserTableService {
//和数据库建立连接
private Connection dbConnection;
//建立数据库操作执行语句变量
private Statement st;
//记录执行的结果
private ResultSet rs;
private String sql;
private List list;
//创建一个usertable对象
private UserTable userTable;
public List getUserTableInfo(){
list = new ArrayList<>();
//获取数据库连接
dbConnection = ConnectionFactory.getInstance().makeConnection();
try {
//合成SQL语句
st = (Statement) dbConnection.createStatement();
sql = "SELECT * FROM biosql.usertable";
//执行sql语句
rs = st.executeQuery(sql);
//获取结果集里的数据
while(rs.next()){
userTable = new UserTable();
userTable.setId(rs.getInt("id"));
userTable.setUsername(rs.getString("username"));
userTable.setPassword(rs.getString("password"));
userTable.setTel(rs.getString("tel"));
userTable.setMail(rs.getString("mail"));
//把userTable加入到list中
list.add(userTable);
System.out.println("List的数据有"+list.size()+"条");
}
} catch (SQLException e) {
System.out.print("合成并执行SQL语句失败");
e.printStackTrace();
}
return list;
}
/*//测试函数
public static void main(String args[]){
UserTableService userTableService = new UserTableService();
System.out.println("查询得到的结果为:"+userTableService.getUserTableInfo());
}*/
}
struts.xmlとweb.xml: web.xmlの
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--在web.xml文件中的<welcome-file>信息中是否配置了自己工程的启动页面-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PrintUserTable</servlet-name>
<servlet-class>com.eodream.servlet.PrintUserTable</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrintUserTable</servlet-name>
<url-pattern>/PrintUserTable</url-pattern>
</servlet-mapping>
</web-app>
struts.xml:?エラーがどこにあるか私に言うことができる、私は長い間、それに混同されている
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="PrintUserTable"/>
<action name="PrintUserTable" class="com.eodream.servlet.PrintUserTable">
<result name="success">printUserTable.jsp</result>
</action>
</package>
</struts>
。すみません、私の英語は貧しいです...
あなたのブラウザのエラーを投稿していません。 –
また、終了コード ''の前にjspにJavaコードを入れてください。 –
S2でサーブレットを使用する理由は何ですか? S2アクションを使用します。 –