- Java code
package com.xxx.servise;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.xxx.basedao.Common;import com.xxx.model.EmployeeInfo;public class EmployeeServise { public void add(EmployeeInfo e) { Connection con = Common.getcon(); String sql = "insert into info values(?,?)"; PreparedStatement pstmt = Common.prepare(con, sql); try { pstmt.setString(1, e.getName()); pstmt.setString(2, e.getDescription()); pstmt.executeUpdate(); } catch (SQLException e1) { e1.printStackTrace(); } finally { Common.close(con, pstmt); } }
- Java code
package com.xxx.action;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.xxx.model.EmployeeInfo;import com.xxx.servise.EmployeeServise;public class EmployeeAction extends ActionSupport { private List<EmployeeInfo> employees; private EmployeeServise employeeServise = new EmployeeServise(); private EmployeeInfo employee; private int id; public String list() { employees = employeeServise.list(); return "list"; } public String add() { employeeServise.add(employee); return "add"; }
- HTML code
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK" import="java.util.*"%><!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="EmployeeAction!add.action" method="post">name: <input name="employee.name" id="employee.name">description:<textarea rows="4" cols="10" name="employee.description" id="employee.description"></textarea><input type="submit" value="add"></form></body></html>
- XML 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> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="Employee" namespace="/" extends="struts-default"> <action name="EmployeeAction" class="com.xxx.action.EmployeeAction" > <result name="add">/Employee-add.jsp</result> </action> </package> <!-- Add packages here --></struts>
------解决方案--------------------------------------------------------
<action name="EmployeeAction" class="com.xxx.action.EmployeeAction" >
照你这种配置 没有add.action 这个配置