myeclipse自带json包及json插件使用法
为什么要用myeclipse自带的json包及json插件呢?一句话,方便,不用找struts2相匹配的json插件,这是优点,缺点是json新开发的功能没有。不过json原有的功能已经够使用了。
1、新建web project
2、添加struts2框架(add struts2 capabilities)
3、选择请求方式,/.*
4、查看项目文件夹下Struts2 Core Libraries文件夹下是否有json的jar包及json插件
5、创建action、dao、实体类(User),json使用实例
action类代码
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package woye.action;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import https://www.360docs.net/doc/e116839779.html,er;
import https://www.360docs.net/doc/e116839779.html,erDaoImpl;
import woye.exceptions.DaoException;
import woye.exceptions.ParameterException;
import woye.exceptions.ValidateException;
import woye.util.MD5;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
/**
*
*/
//private static final long serialVersionUID = 1L;
private UserDaoImpl userDao = new UserDaoImpl();
public User user;
public User getUser() {
return user;
}
public void setUser(User user) {
https://www.360docs.net/doc/e116839779.html,er = user;
}
// 登录
public String login() throws DaoException, ValidateException, Exception { if(user==null || user.equals("")){
return ERROR;
}
if(user.getNickName()==null||user.getNickName().equals("")){
return ERROR;
}
if(user.getPassWord()==null||user.getPassWord().equals("")){
return ERROR;
}
user = https://www.360docs.net/doc/e116839779.html,erLogon(user.getNickName(), user.getPassWord()); System.out.println(user.toString());
if(user != null && !user.equals("")) {
return SUCCESS;
}
return ERROR;
}
}
-------------------------------------------------------------------------------
dao类代码
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package woye.dao.impl;
import java.util.List;
import woye.dao.IUser;
import woye.dao.base.BaseDAOImpl;
import https://www.360docs.net/doc/e116839779.html,er;
import woye.exceptions.DaoException;
import woye.exceptions.ParameterException;
import woye.exceptions.ValidateException;
import woye.util.MD5;
public class UserDaoImpl extends BaseDAOImpl {
// 登录
public User userLogon(String userName, String userPwd) throws DaoException, ValidateException, Exception {
StringBuffer strSql = new StringBuffer();
strSql.append("SELECT USERID AS userId,NICKNAME AS nickName,PASSWORD AS PassWord,TELEPHONE AS telephone,STATUS AS status,logtime as logtime");
strSql.append(" FROM td_user");
strSql.append(" where nickName='" + userName + "'");
strSql.append(" and passWord='" + userPwd + "'");
System.out.println("==" + strSql);
return (User) getObject(strSql, User.class);
}
}
注:BaseDAOImpl 是一个父类dao,完成了基本的增删改查的功能,另外此实例的JDBC使用了DBUtils框架,也就是BaseDAOImpl 中各个功能都是由DBUtils完成的
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
实体类(user)
------------------------------------------------------------------------------- package woye.dao.bean;
public class User {
// 用户编号
private String userId;
// 昵称
private String nickName;
// 密码
private String passWord;
// 手机号
private String telephone;
// 发布时间
private String logtime;
// 状态
private String status;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
https://www.360docs.net/doc/e116839779.html,erId = userId;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getLogtime() {
return logtime;
}
public void setLogtime(String logtime) {
this.logtime = logtime;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "User [logtime=" + logtime + ", nickName=" + nickName + ", passWord=" + passWord + ", status=" + status
+ ", telephone=" + telephone + ", userId=" + userId + "]";
}
public User() {
super();
}
public User(String passWord, String nickName, String telephone, String logtime, String status) {
super();
this.nickName = nickName;
this.passWord = passWord;
this.telephone = telephone;
this.logtime = logtime;
this.status = status;
}
注:user用的上的属性就三个,id,name,password,其余的不要了,我懒了就没删,自己敲吧,要是复制就没有学习的意义了,这都省了好多。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6、struts.xml
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"https://www.360docs.net/doc/e116839779.html,/dtds/struts-2.0.dtd">
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7、index.jsp
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path +"/";
%>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
8、启动tomcat,输入用户名和密码,提交
提交后
注:在编写过程中,难免有错。