我的SUCCESS.JSP:
<%@ page contentType="text/html; charset=UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>注册成功</title>
</head>
<body>
欢迎<s:property value="username"/>,注册成功! <!--调用<s:property>标签去获取username的值-->
</body>
</html>但是报错:严重: Servlet.service() for servlet default threw exception
org.apache.jasper.JasperException: /success.jsp(17,38) According to TLD, tag s:property must be empty, but is not不知道是什么原因啊?
<%@ page contentType="text/html; charset=UTF-8"%><%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>注册成功</title>
</head>
<body>
欢迎<s:property value="username"/>,注册成功! <!--调用<s:property>标签去获取username的值-->
</body>
</html>但是报错:严重: Servlet.service() for servlet default threw exception
org.apache.jasper.JasperException: /success.jsp(17,38) According to TLD, tag s:property must be empty, but is not不知道是什么原因啊?
还有一点 你的命名不符合java规范哦
public class LoginAction extends ActionSupport {
private String userName;
private String passWord; @Override
public String execute() throws Exception {
// 在此处进行判断,如果内容合法,则跳转到成功页
if (getUserName().equals("pany") && getPassWord().equals("pany")) {
ActionContext context = ActionContext.getContext();
context.put("userName", getUserName());
System.out.println("success");
return "success";
} else {
return "error";
}
}
public String delete(){
System.out.println("进入方法体");
ActionContext text = ActionContext.getContext();
text.getSession().put("delete", "自定义Action");
return ActionSupport.LOGIN;
}
public void validate(){
System.out.println("enter Validate Method");
if(userName == null || userName.equals("")){
addFieldError("user", "用户名不能为空");
}
}
public String getPassWord() {
return passWord;
} public void setPassWord(String passWord) {
this.passWord = passWord;
} public String getUserName() {
return userName;
} public void setUserName(String userName) {
this.userName = userName;
}
}
前台jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %><html>
<head>
<title>My JSP 'hello.jsp' starting page</title>
</head>
<body>
<h1>您好:<s:property value="userName"/></h1>
<h6>${requestScope.userName}</h6>
</body>
</html>
struts.xml中的配置
<action name="hello" class="org.pany.struts2.action.LoginAction">
<result name="success">
/hello.jsp
</result>
<result name="error">
/errors.jsp
</result>
<result name="input">
/index.jsp
</result>
</action>
<action name="Delete" class="org.pany.struts2.action.LoginAction" method="delete">
<result name="LOGIN">
/errors.jsp
</result>
</action>
我试了一下,成功了,我还有一个问题:
我想在succuess.jsp 页面得到username ,password,并且把它放到MYSQL 中,我的success.jsp 怎么设计啊?
可以参考一下:
<%
String DBDRIVER="org.gjt.mm.mysql.Driver";
String DBURL="jdbc:mysql://localhost:3306/shp";
String USERNAME="username";
String PASSWORD="password";
String sql="select * from user_info";
Connection conn = null;
ResultSet rs = null;
Statement stmt=null;
try
{Class.forName(DBDRIVER).newInstance();
}
catch(java.lang.ClassNotFoundException e)
{System.err.println(e);
}
try
{conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
%>
<%
while(rs.next()){%>
<td><%=rs.getString(1)%></td>
<% } %>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>当然也可以放在后台操作,但不知道怎么做啊?
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%@page import="java.sql.*" %><title>欢迎界面</title></head>
<body>
<h1>您好: <s:property value="username"/> </h1> <!--获取session对象中保存的username属性-->
<%
String DBDRIVER="org.gjt.mm.mysql.Driver";
String DBURL="jdbc:mysql://localhost:3306/shp";
String USERNAME="username";
String PASSWORD="password";
String reg="insert into user_info values(?,?";
Connection conn = null;
ResultSet rs = null;
Statement stmt=null;
try
{Class.forName(DBDRIVER).newInstance();
}
catch(java.lang.ClassNotFoundException e)
{System.err.println(e);
}
try
{conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
stmt = conn.createStatement();
rs = stmt.executeQuery(reg);
}
catch(SQLException ex)
{System.err.println("aq.executeQuery:"+ex.getMessage());
}
try
{PreparedStatement pstmt = conn.prepareStatement(reg);
pstmt.setString(1, "username");
pstmt.setString(2, "password");
pstmt.executeUpdate();
} catch(Exception e){
e.printStackTrace();
}
%>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
package test;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.SQLException;
public class SqlConnect {
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
public static final String DBURL = "jdbc:mysql://localhost:3306/shp";
//现在使用的是mysql数据库,是直接连接的,所以此处必须有用户名和密码
public static final String USERNAME = "root";
public static final String PASSWORD = "mysqladmin";
public static void main(String[] args) {
//数据库连接对象
Connection conn = null;
//数据库操作对象
Statement stmt = null;
//1、加载驱动程序
try {
Class.forName(DBDRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2、连接数据库
//通过连接管理器连接数据库
try {
//在连接的时候直接输入用户名和密码才可以连接
conn = DriverManager.getConnection(DBURL,USERNAME,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
//3、向数据库中插入一条数据
String sql = "INSERT INTO user_info(username,password) VALUES ('david','30')";
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//4、执行语句
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
//5、关闭操作,步骤相反哈~
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}是可以插入数据的,就是想把界面的username ,password 放到数据库中呀,不知道怎么办啊?
jsp页面如下
<s:form action="saveStudent.action" method="post">
<s:hidden name="sa.id"></s:hidden>
<table>
<s:textfield name="sa.username" label="用户名"></s:textfield>
<s:textfield name="sa.love" label="爱 好"></s:textfield>
<s:textfield name="sa.dece" label="描述"></s:textfield>
</table>
<s:if test="${sa.id == null}">
<s:submit name="submit" value="提交"></s:submit>
</s:if>
<s:else>
<s:submit name="submit" value="更新" onclick="doUpdate();"></s:submit>
</s:else>
</s:form>后台java处理 private Integer id;
private String dece;
private String love;
private String username;
private List resultList;//
private StudentAction sa;//
public StudentAction getSa() {
return sa;
}
public void setSa(StudentAction sa) {
this.sa = sa;
}
public List getResultList() {
return resultList;
}
public void setResultList(List resultList) {
this.resultList = resultList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDece() {
return dece;
}
public void setDece(String dece) {
this.dece = dece;
}
public String getLove() {
return love;
}
public void setLove(String love) {
this.love = love;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String execute(){
Dao dao = new Dao();
Student st = new Student();
st.setDece(sa.getDece());
st.setLove(sa.getLove());
st.setUsername(sa.getUsername());
dao.insert(st);
return "suc";
}