Struts2+MyBatis项目

目录

web.xml

struts.xml

mybatis-config.xml

log4j.properties

database.properties

com.bdqn.entity

com.bdqn.utils

com.bdqn.dao

com.bdqn.action

lib

index.jsp

Login.jsp


 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>strutschu第一次莱尼联</display-name>

	<filter>
		<!-- Struts2 的核心类配置,是必须配置 -->
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<!-- 一个filter可以对应多个mapping,出了写成/*还可以携程.action表示拦截以此结尾的请求 -->
		<!-- 拦截以后所有的东西都交由action来查找,action加载Struts.xml该配置文件和Struts1放在web-INF下不同,它是放在src文件夹下的 -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 中文乱码过滤器配置——但是管不了ajax和action方法的结果交互 -->
	<filter>
		<filter-name>EncodingFilter</filter-name>
		<filter-class>com.bdqn.utils.EncodingFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

struts.xml
 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">      <!-- //头必须要改成这个 -->
<struts>
	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
	<package name="default" namespace="/" extends="struts-default">
	
	<!-- 设置了下面的配置,只需要在页面输入方法.action就可以准确定位方法所在位置了 -->
		<global-allowed-methods>regex:.*</global-allowed-methods>    <!-- //2.5版本以上要加这一句 -->
		<!-- 下面这个action默认的是走method=“execute"方法 -->
		<action name="GoodsAction" class="com.bdqn.action.GoodsAction">
			<!-- 结果为“success”时,跳转至success.jsp页面 -->
			<result name="success">/index.jsp</result>
			<!-- 结果为"error"时,跳转至fail.jsp页面 -->
			<result name="error">/fail.jsp</result>
			<allowed-methods>findALL</allowed-methods>     <!-- ←这里是为了后面快捷访问网址/项目名/action的name+"!"+方法名.action 配置的对应方法 -->
		</action>
		
		<!-- 下面的action配置,每个对应的都是action中的一个method,前面的名字可以随意取,只是在其它地方的引用名字 -->
		<action name = "findALLAction" class = "com.bdqn.action.GoodsAction" method = "findALL">
			<result name="success">/index.jsp</result>
		</action>
		<action name = "DeleteGoods" class = "com.bdqn.action.GoodsAction" method = "DeleteGoods">
			<result name="success" type="redirectAction">fenye</result>
		</action>
		<action name = "addGoods" class = "com.bdqn.action.GoodsAction" method = "addGoods">
			<result name="success" type="redirectAction">fenye</result>
		</action>
		<action name = "fenye" class = "com.bdqn.action.GoodsAction" method = "fenye">
			<result name="success">/index.jsp</result>
		</action>
		<action name = "update" class = "com.bdqn.action.GoodsAction" method = "update">
			<result name="success" type="redirectAction">fenye</result>
		</action>
		<action name = "findById" class = "com.bdqn.action.GoodsAction" method = "findById">
		</action>
		
		<!-- 标准的只有一个execute方法的实例,假如method不写,默认找execute,假如class不写默认找actionsupprt,假如result的name不写默认走success -->
		<action name="Users1Action" class="com.bdqn.action.Users1Action">
			<!-- <result type="redirectAction" name="success">findALLAction</result> -->
			<result name="success" type="redirectAction">fenye</result>
			<result name="error">/Login.jsp</result>
			<allowed-methods>login</allowed-methods>     <!-- ←这里是为了后面快捷访问网址/项目名/action的name+"!"+方法名.action 配置的对应方法 -->
		</action>
	</package>
</struts>

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<!--设置外置配置文件-->
	<properties resource="database.properties"></properties>
	
	<!--设置各种参数,包括类别名-->
	<settings>
        <setting name="logImpl" value="LOG4J" />
    </settings>
	
	<!--设置类别名  p29-->
	<typeAliases>
		<package name="com.bdqn.entity"/>
	</typeAliases>
	
	<!--p30  -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<!-- 配置数据库连接信息 -->
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${user}" />
				<property name="password" value="${pwd}" />
			</dataSource>
		</environment>
	</environments>

	<!--p31  -->
	<mappers>
		<!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml -->
		<mapper resource="com/bdqn/dao/GoodsMapper.xml"/>
		<mapper resource="com/bdqn/dao/Users1Mapper.xml"/>
	</mappers>
</configuration>

log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

database.properties

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl1
user=system
pwd=1234

com.bdqn.entity

Goods.java

package com.bdqn.entity;
/*
 * 实体类虽然没什么好说的:但是对应的是Oracle所以有几点要注意
 * 如果在数据库的number是2位只能用byte,4位short,6位int,所以一般都设置为6位
 */
public class Goods {
	private int id;
	private String name;
	private double price;
	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 double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Goods(int id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}
	
	
}

Users1.java

package com.bdqn.entity;
/*
 * 实体类虽然没什么好说的:但是对应的是Oracle所以有几点要注意
 * 如果在数据库的number是2位只能用byte,4位short,6位int,所以一般都设置为6位
 */
/*
 * 由于Oracle数据库user是关键字,所以不能用user建表
 */
public class Users1 {
	private int id;
	private String name;
	private String password;
	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 String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Users1(int id, String name, String password) {
		super();
		this.id = id;
		this.name = name;
		this.password = password;
	}
	public Users1() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
}

com.bdqn.utils

EncodingFilter.java

package com.bdqn.utils;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class EncodingFilter implements Filter {
 
  
    
 
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
 
        request.setCharacterEncoding("UTF-8");
 
        chain.doFilter(request, response);
        System.out.println("过滤器");
        response.setCharacterEncoding("UTF-8");
    }
 
  
    


	public void destroy() {
		// TODO Auto-generated method stub
		
	}





	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}
 
}

MyBatis.java

package com.bdqn.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;



public class MyBatis {
	private static SqlSessionFactory factory;
	static{
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			factory = new SqlSessionFactoryBuilder().build(is);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SqlSession createSqlSession(){
		return factory.openSession(false);
		//这样就不自动提交事务了
	}
	
	public static void closeSqlSession(SqlSession sqlSession){
		if (sqlSession!=null) {
			sqlSession.close();
		}
	}

}

com.bdqn.dao

GoodsMapper.java

package com.bdqn.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.bdqn.entity.Goods;

public interface GoodsMapper {
	//查询全部信息
	List<Goods> findALL();
	//根据id删除对应的数据
	int deleteGoods(@Param("id")int id);
	//添加商品
	int addGoods(Goods goods);
	//分页方法:这两个one和two不是第几页,也不是每页几条,这是通过index和size计算出来的,笔记在有道笔记
	List<Goods> fenye(@Param("one")int one,@Param("two")int two);
	//跟分页查询配套的方法:用来查询一个弄有几条数据,用来计算总页数的
	int findCount();
	//修改方法:底层写成了MMyBatis的动态SQL
	int update(Goods goods);
	//通过id来寻找一个商品:跟修改是配套方法
	Goods findById(@Param("id")int id);
}

GoodsMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bdqn.dao.GoodsMapper">
	<select id="findALL" resultType="Goods">
		select * from goods
	</select>
	<!-- 这个分页查询方法语句有个小于号是绝对报错的,所以把它替换成&lt; -->
	<select id="fenye" resultType="Goods">
		SELECT * FROM  (  SELECT A.*, ROWNUM RN  FROM (SELECT * FROM goods) A  
		WHERE ROWNUM &lt; #{one} ) WHERE RN >= #{two}
	</select>
	<select id="findCount" resultType="int">
		select count(1) from goods
	</select>
	<select id="findById" resultType="Goods">
		select * from goods where id = #{id}
	</select>
	<delete id="deleteGoods" parameterType="int">
		delete from goods where id = #{id}
	</delete>
	<insert id="addGoods" parameterType="Goods">
		insert into goods values(goods_sequence.nextval,#{name},#{price})
	</insert>
	<update id="update" parameterType="Goods">
		update goods 
		<set>
			<if test="name != null">name = #{name},</if>
			<if test="price != null">price = #{price},</if>
		</set> 
		where id = #{id}
	</update>
</mapper>

Users1Mapper.java

package com.bdqn.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.bdqn.entity.Users1;

public interface Users1Mapper {
	//登录验证方法
	List<Users1> login (@Param("name")String name,@Param("password")String password);
}

Users1Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.bdqn.dao.Users1Mapper">
	<select id="login" resultType="Users1">
		select * from users1 where name=#{name} and password=#{password}
	</select>
</mapper>

com.bdqn.action

GoodsAction.java

package com.bdqn.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSONArray;
import com.bdqn.dao.GoodsMapper;
import com.bdqn.entity.Goods;
import com.bdqn.utils.MyBatis;
import com.opensymphony.xwork2.ActionSupport;


public class GoodsAction extends ActionSupport{
	//说实话Struts2是个很好用的框架,其它的框架都需要用request来获取值,然后在来进行转换,还有可能为空报错
	//但是在Struts2只需设置一个变量并给他set和get方法就可以直接使用这个变量名来存值取值
	
	//返回信息的变量
	private String mess;
	//记录页面删除按钮的value的值
	private int Delete;
	//添加方法封装用户传来的信息
	private Goods addGoods;
	//默认设置第一次访问第一个页面
	private int index = 1;
	//每页显示两条数据
	private int size = 2;
	//一共有几页
	private int count = 1;
	//传递修改方法的对象封装
	private Goods goods;
	
	//查询全部方法——在页面有一个查询全部的按钮,跳转路径写的就是这个方法
	public String findALL() throws Exception {
		//使用MyBatisSQL语句的方法,先获取sqlsession链接对象
		//其它的步骤都封装在utils包下了
		SqlSession session = MyBatis.createSqlSession();
		//调用GoodsMapper下的方法,他会自动根据MyBAtis下的配置,自动去寻找xml的SQL语句
		List<Goods> list = session.getMapper(GoodsMapper.class).findALL();
		//使用完一定要关闭链接,不然查询的时候链接太多会造成多连接是的缓慢和延迟,亲身体验过!
		MyBatis.closeSqlSession(session);
		//在Struts2获取request和response可以通过核心类来获取
		HttpServletRequest request= ServletActionContext.getRequest();
		//获取session链接
		HttpSession session1 = request.getSession();
		session1.setAttribute("list", list);
		return SUCCESS;
	}
	//删除的方法
	public String DeleteGoods() throws Exception {
		//使用MyBatisSQL语句的方法,先获取sqlsession链接对象
		//其它的步骤都封装在utils包下了
		SqlSession session = MyBatis.createSqlSession();
		//调用GoodsMapper下的方法,他会自动根据MyBAtis下的配置,自动去寻找xml的SQL语句
		session.getMapper(GoodsMapper.class).deleteGoods(Delete);
		//记得事物是一定要提交才会写入数据库,不然你的SQL语句是对的,返回结果是一但是查询和数据库都是空的就很难受了!亲身体验!
		session.commit();
		MyBatis.closeSqlSession(session);
		return SUCCESS;
	}
	//添加一个新的商品方法
	public String addGoods() throws Exception {
		//使用MyBatisSQL语句的方法,先获取sqlsession链接对象
		//其它的步骤都封装在utils包下了
		SqlSession session = MyBatis.createSqlSession();
		//调用GoodsMapper下的方法,他会自动根据MyBAtis下的配置,自动去寻找xml的SQL语句
		int count = session.getMapper(GoodsMapper.class).addGoods(addGoods);
		session.commit();
		MyBatis.closeSqlSession(session);
		return SUCCESS;
	}
	//分页查询方法
	public String fenye() throws Exception {
		SqlSession session = MyBatis.createSqlSession();
		//先获取数据库该表有几条数据total,直接写一个select count(1) from goods就可以了
		int total = session.getMapper(GoodsMapper.class).findCount();
		//用页面量size和total来计算一共该显示几页,就算最后一页只有一条数据,也应该用一页来显示,所以必须先计算total/size是不是等于0
		count = total%size==0?total/size:(total/size)+1;
		//当一直点击上一页,到了第一页时就不能让index继续减小,因为没有第0页
		if (index<1) {
			index=1;
		//同理到了最大页数的时候也不能让他继续加大
		}else if(index>count){
			index=count;
		}
		//这是计算分页语句需要的参数值小于第几条数据
		int one = (((index-1)*size)+1)+size;
		//这是计算该从第几条数据开始显示
		int two = ((index-1)*size)+1;
		
		//分页方法
		List<Goods> list = session.getMapper(GoodsMapper.class).fenye(one, two);
		//使用完关闭下
		MyBatis.closeSqlSession(session);
		
		//获取request和sqlsession 
		HttpServletRequest request= ServletActionContext.getRequest();
		HttpSession session1 = request.getSession();
		
		//在把值存入session之前先清空原先的值
		session1.removeAttribute("list");
		session1.setAttribute("list", list);

		return SUCCESS;
	}
	public String update() throws Exception {
		SqlSession session = MyBatis.createSqlSession();
		int count = session.getMapper(GoodsMapper.class).update(goods);
		session.commit();
		MyBatis.closeSqlSession(session);
		return SUCCESS;
	}
	public String findById() throws Exception {
		//获取和响应器获取
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		//这里跟Ajax前端交互,但是过滤器是没有的,所以手动设置
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		//获取前端的id值然后查出该对象,然后返回给前端
		int id = Integer.parseInt(request.getParameter("id"));
		Goods good = MyBatis.createSqlSession().getMapper(GoodsMapper.class).findById(id);
		//用的是Ajax的json传值方法
		response.getWriter().print(JSONArray.toJSONString(good));
		return NONE;
	}
	
	public String getMess() {
		return mess;
	}
	public void setMess(String mess) {
		this.mess = mess;
	}
	public int getDelete() {
		return Delete;
	}
	public void setDelete(int delete) {
		Delete = delete;
	}
	public Goods getAddGoods() {
		return addGoods;
	}
	public void setAddGoods(Goods addGoods) {
		this.addGoods = addGoods;
	}
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public Goods getGoods() {
		return goods;
	}
	public void setGoods(Goods goods) {
		this.goods = goods;
	}
	
	
}

Users1Action.java

package com.bdqn.action;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bdqn.dao.Users1Mapper;
import com.bdqn.entity.Users1;
import com.bdqn.utils.MyBatis;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
//在struts2中我们有两种选择继承的方法:
//第一:implements Action这种方法必须实现execute方法,
//但是我们假如生成了EXEcute方法,在后面多方法时地址跳转的时候,会默认先找寻
//
public class Users1Action extends ActionSupport{
	//由于Struts有个很方便的属性封装的功能,所以我们只需要在action中
	//定义变量和它的set和get方法就可以前后页面互相传值
	//不用通过request.getparameter来获取了
	private String name;
	private String password;
	//登录验证方法
	public String login() throws Exception {
		SqlSession session = MyBatis.createSqlSession();
		List<Users1> list= session.getMapper(Users1Mapper.class).login(name,password);
		if (list.size()>0) {
			return SUCCESS;
		}
		//这里的返回值有两种写法:
		//要么"success"或者SUCCESS,这两种写法的效果是等同的
		//大写的方法只有五个参数,指代不同的意思是Struts2自带的
		//SUCCESS成功
		//ERROR失败
		//NONE  什么时都不返回
		//LOGIN 
		//INPUT 
		//这些是java的默认final值所以需要大写,其实代表的意思还需到配置文件去配置,才会有效果;
		return ERROR;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Users1Action() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Users1Action(String name, String password) {
		super();
		this.name = name;
		this.password = password;
	}
	
	

}

WEB-INF

lib

commons-fileupload-1.3.3.jar
commons-io-2.5.jar
commons-lang3-3.6.jar
fastjson-1.2.6.jar
freemarker-2.3.26-incubating.jar
javassist-3.20.0-GA.jar
log4j-1.2.17.jar
log4j-api-2.10.0.jar
mybatis-3.2.2.jar
mybatis-3.4.6.jar
mysql-connector-java-5.1.0-bin.jar
ognl-3.1.15.jar
ojdbc6.jar
struts2-core-2.5.17.jar

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
  </head>
  
  <body>
  
    <div style="width: 400px;height: 400px;position: absolute;top: 50%;left: 50%;margin-left: -200px;margin-top: -200px;border: 1px solid black">
    	<table border="2">
    	<tr><td><a href="/封装变量/Users1Action/findALLAction"><button>全部查询</button></a></td></tr>
    	<tr>
    	<td>ID</td>
    	<td>商品名</td>
    	<td>价格</td>
    	<td>操作</td>
    	</tr>
    	<c:forEach items="${list}" var="li" varStatus="str">
    		<tr>
    			<td>${li.id}</td>
    			<td>${li.name}</td>
    			<td>${li.price}</td>
    			<td>
    			<a href="/封装变量/DeleteGoods?Delete=${li.id}"><button>删除</button></a>
    			<a><button name="updateButton" value="${li.id}">修改</button></a>
    			</td>
    		</tr>
    	</c:forEach>
    	</table>
    	<p>
    	共${count}/${index}页
    	<a href="/封装变量/GoodsAction/fenye?index=1&size=${size}&count=${count}"><button>首页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${index-1}&size=${size}"><button>上一页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${index+1}&size=${size}"><button>下一页</button></a>
    	<a href="/封装变量/GoodsAction/fenye?index=${count}&size=${size}"><button>尾页</button></a>
    	</p>
    	
    	
    	
    	<p><button id="add">上架商品</button></p>
    	<div id="addGoods" style="display: none">
    	<form action="/封装变量/GoodsAction/addGoods">
    	填写新增商品名称:<input type="text" name="addGoods.name"><br/>
    	填写价格:<input type="text" name="addGoods.price"><br/>
    	<input type="submit" value="新增">
    	</form>
    	</div>
    	<div id="update" style="display: none">
    	<form action="/封装变量/GoodsAction/update">
    	修改商品名称:<input type="text" name="goods.name" id="goodsname"><br/>
    	修改商品价格:<input type="text" name="goods.price" id="goodsprice"><br/>
    	<input type="hidden" name="goods.id" id="goodsid"><br/>
    	<input type="submit" value="修改">
    	</form>
    	</div>
    </div>
    <script>
 	$(function(){
 		/* 点击事件,只要点击添加按钮,就显示添加表单 */
 		$("#add").bind("click",function(){
 			$("#addGoods").fadeIn(2000);
 		});
 		/* 只要点击修改事件,就获取该按钮的value值,对应该行的商品id,然后把id给后台,获取goods对象,把值给表单,客户修改完后再提交 */
 		$("[name='updateButton']").bind("click",function(){
 			var id = $(this).val();
 			$.ajax({
 				url:"http://localhost:8080/封装变量/GoodsAction/findById",
 				type:"post",
 				data:{"id":id},
 				dataType:"json",
 				success:function(data){
 					alert(data.name);
 					$("#goodsname").val(data.name);
 					$("#goodsprice").val(data.price);
 				}
 			});
 			$("#goodsid").val(id);
 			$("#update").show();
 		});
 	});
 	
 	
  </script>
  </body>
</html>

Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <div style="width: 400px;height: 100px;position: absolute;top: 50%;left: 50%;margin-left: -200px;margin-top: -50px ">
    <form action="/封装变量/Users1Action!login.action">
    	登录名:<input type="text"  name="name"><br/>
    	密码:<input type="text"  name="password"><br/>
    	<input type="submit" value="提交">
    </form>
    </div>
  </body>
</html>

 

  • 0
    点赞
  • 1
    评论
  • 6
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

评论 1 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

jinqianwang

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值