Struts2添加用户练习

  • 向数据库中插入一个一个新用户的功能
  1.    提供一个页面可以输入新用户信息(用户名、年龄、地址)
  2. 输入完信息,将请求提交到一个user-add-Action
  3. Action收到请求后直接存储到数据库中
    package com.Action;
    
    public class UserAction {
    
    	//Action的请求参数
    	//如果想要在Action中获得用户发送的请求参数,只需在Action中创建一个参数同名的属性
    	//并且为属性提供get和set方法,Struts2会将属性自动注入Action中
    	private String username;
    	private int age;
    	private String address;
    	
    	
    	
    
    	public String getUsername() {
    		return username;
    	}
    
    
    	public void setUsername(String username) {
    		this.username = username;
    	}
    
    
    	public int getAge() {
    		return age;
    	}
    
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    
    	public String getAddress() {
    		return address;
    	}
    
    
    	public void setAddress(String address) {
    		this.address = address;
    	}
    
    
    	public String add() {
    		System.out.println(username+"年龄"+age+"--"+address);
    		System.out.println("请求成功");
    		return "success";
    		
    	}
    }
    

    input

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>登录页面</title>
    </head>
    <body>
    	<h3>输入用户信息</h3>
    		<form action="user-add"method="post">
    		用户名<input type="text" name="username"/><br/><br/><br/>
    		年龄<input type="text" name="age"/><br/><br/><br/>
    		地址<input type="text" name="address"/><br/><br/><br/>
    		<input type="submit" value="添加用户">
    		</form>
    </body>
    </html>

    index

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <a href="user-input">添加用户</a>
    </body>
    </html>

    Struts2.XML

  4. <?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>
    
    	<!-- constant用来配置Struts运行时的常量 -->
    	<!-- constant标签中有两个属性:name指定的常量的名字,value指定的是常量的值 -->
    	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    	
    	<!--struts.devMode表示Struts是否是在开发模式,当项目处于开发模式式,错误会在页面中直接显示  -->
    	<constant name="struts.devMode" value="true" />
    
    	<!-- package用来为action分组,一般我们将相关的action都放在同一个package方便管理 -->
    	<!-- name:package的名字,这个属性时一个必选属性 -->
    	<!-- namespace:命名空间,指定访问action时的前缀,默认值时/ 代表的项目的根目录 -->
    	<!-- 当访问一个action时,通过 http://localhost:8080/项目名/namespace/actionName -->
    	<!-- extends:表示当前package继承自那个package ,struts-default是我们最常用的基础的package 
    		struts-default这个package中声明很多的结果类型、拦截器、拦截器栈、默认的拦截器栈、还有默认的action
    	-->
    	<package name="default" namespace="/" extends="struts-default">
    	
    		<!-- 我们将由struts2处理的请求称为action,处理请求的那个类叫Action -->
    		<!-- 我们使用action标签在struts中映射一个Action -->
    		<!-- name:action的名字,我们通过action的name属性来访问action -->
    		<!-- class:指定Action的类 , 如果不写则默认是com.opensymphony.xwork2.ActionSupport -->
    		<!-- method:指定处理请求时要调用的那个方法
    				这个方法要求没有参数,必须有一个String类型的返回值
    				如果不写则默认找execute方法
    		 -->
    		 <!-- 一个package下可以配置多个Action -->
    		<action name="user-input" >
    			<!-- result为action映射一个结果,他可以根据action方法返回值,返回不同的页面 -->
    			<!-- name属性的结果的名字,action中通过返回结果名,来选择视图 ,name属性如果不写则默认是success-->
    			<!-- type指定如何去目标资源 ,默认值type是dispatcher,通过转发去目标资源 -->
    			<result>/WEB-INF/pages/input.jsp</result>
    			<allowed-methods>*</allowed-methods>
    		</action>
    		 <action name="user-add" class="com.Action.UserAction" method="add">
    		<result>/WEB-INF/pages/success.jsp</result>
    		<allowed-methods>*</allowed-methods>
    		</action>
    	</package>
    
    
    </struts>
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 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-app>

    success

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <h1>登录成功</h1>
    	
    	用户名:${username }<br/><br/>
    	年龄:${age }<br/><br/>
    	地址:${address }<br/><br/>
    </body>
    </html>

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值