标签:
今天学习了一下Struts2框架的搭配~
也简单的使用Struts2框架学习了Action和怎么使用核心配置文件struts.xml~
主要实现了网页上的简单登录注册~
项目结构如图:
写了俩个类,一个是pojo类,一个是action类~,还有俩个jsp,分别代表登录与注册~
pojo类顾名思义是Bean文件~
action类是包含一些映射配置方法,和struts.xml文件一起使用的~
步骤:
1.先写一个User类,User类包含了一些注册用户的信息,代码如下:
public class User implements Serializable{ private static final long serialVersionUID = 1L; private Long id; private String name; private String password; private Integer age; private Boolean gender; public User() { super(); // TODO Auto-generated constructor stub } public User(Long id, String name, Integer age, Boolean gender,String password) { super(); this.id = id; this.name = name; this.age = age; this.gender = gender; this.password=password; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + ", gender=" + gender + "]"; } }
要在核心配置文件 struts.xml中配置Action
2.要在如图所示的下面配置Action,如图:
有三种配置方式:
第一种:
<action name="user_*" class="com.xu.struts2.web.action.UserAction" method="{1}"> <result name="login">/WEB-INF/jsp/login.jsp</result> <result name="register">/WEB-INF/jsp/register.jsp</result> <result name="index">/WEB-INF/jsp/index.jsp</result> </action>含义如下:
相信应该已经解释的很清楚~,我在UserAction里面写了如下方法:
public String toLogin(){ System.out.println("跳转到登录页面"); return "login"; } public String toRegister(){ System.out.println("跳转到注册页面"); return "register"; }写了这俩个方法还不够,还要写登录和注册的jsp,代码如下:
login.jsp:
<%@ 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> <hr> <form action="LoginServlet"> 用户名:<input type="text" name="name"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" value="登录"> </form> </body> </html>register.jsp:
<%@ 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> <hr> <form > 用户名:<input type="text" name="name"/><br> 密码:<input type="password" name="password"/><br> 确认密码:<input type="password" name="repassword"/><br> 性别:<input type="radio" name="gender" value="true" checked="checked">男 <input type="radio" name="gender" value="false">女 <br> 年龄:<select name="age"> <option value="">请选择</option> <% for(int i=10;i<=30;i++){ %> <option value="<%=i %>"><%=i %>周岁</option> <% } %> </select> <br> <input type="submit" value="注册"> </form> </body> </html>只用一个测试就好~
打开服务器
进入浏览器输入localhost:9999/Struts2/user_toLogin.action,效果如图:
第二种:
特定的action,代码如下:
<action name="toLogin" class="com.xu.struts2.web.action.UserAction" method="toLogin"> <result name="login">/WEB-INF/jsp/login.jsp</result> </action>测试效果:
看,这种方式也可以访问登录页面
第三种--动态方法配置,代码如下:
<struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="" value=""></constant> <package name="default" namespace="/" extends="struts-default"> <!-- 3. --> <!-- user!toLogin.action --> <action name="user" class="com.xu.struts2.web.action.UserAction"> <result name="login">/WEB-INF/jsp/login.jsp</result> <result name="register">/WEB-INF/jsp/register.jsp</result> <result name="index">/WEB-INF/jsp/index.jsp</result> </action> </package> <!-- <include file="example.xml"/> --> </struts>必须要注意的是,这种方式一定要做一个操作,如图:
有没有注意到一点:method方法好像没有了~
测试效果:
接下来就是注册页面向登录页面的跳转了~
3.页面跳转
需要在注册页面加上一行代码:
<form action="user!register.action">默认使用的是第三种方法配置Action
还需要在UserAction添加一个register方法,代码如下:
public String register(){ return "login"; }
效果如图:
有时候我们注册成功的时候回向后台传值回去~,也可以向前台传值,传到登录页面
4.向前台传值-比如要返回个注册成功的信息,这个就用到了--同名参数
同名参数我会在后面介绍~
只需要在登录页面的jsp写上一行代码:
<h4 style="color: red">${msg }</h4>然后还要在UserAction的register方法改动一下:
public String register(){ //接收参数 msg="注册成功"; return "login"; }运行一下,效果如图:
5.向后台传值-得到注册成功的用户名信息,在服务器端得到值~这里也用到了同名参数
只需要在UserAction写下以下代码:
private String name; private String password; private Integer age; private Boolean gender; public String register(){ //接收参数 User user=new User(null, name, age, gender, password); System.out.println(user); msg="注册成功"; return "login"; } 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; }这个是可以接收到input里面的key-value值的
运行效果如图:
其实也可以同时向后台和前台传值,只是需要把代码稍微改一下~
6.同时向后台和前台传值--这里用到了域模型~
域模型我会在后面的文章重新写一遍的~
login.jsp就只是需要改动一行代码:
<h4 style="color: red">${msg }欢迎您:${user.name }</h4>register.jsp改动form表单:
<form action="user!register.action"> 用户名:<input type="text" name="user.name"/><br> 密码:<input type="password" name="user.password"/><br> 确认密码:<input type="password" name="repassword"/><br> 性别:<input type="radio" name="user.gender" value="true" checked="checked">男 <input type="radio" name="user.gender" value="false">女 <br> 年龄:<select name="user.age"> <option value="">请选择</option> <% for(int i=10;i<=30;i++){ %> <option value="<%=i %>"><%=i %>周岁</option> <% } %> </select> <br> <input type="submit" value="注册"> </form>UserAction里:
//域模型 private User user; public String register(){ //接收参数 //User user=new User(null, name, age, gender, password); System.out.println(user); msg="注册成功"; return "login"; }效果图:
核心代码:
UserAction:
package com.xu.struts2.web.action; import com.xu.struts2.bean.User; public class UserAction { /*//前台--》后台传值 同名参数接收 input private String name; private String password; private Integer age; private Boolean gender;*/ //后台--》前台传值 private String msg; //域模型 private User user; public String toLogin(){ System.out.println("跳转到登录页面"); return "login"; } public String toRegister(){ System.out.println("跳转到注册页面"); return "register"; } public String register(){ //接收参数 //User user=new User(null, name, age, gender, password); System.out.println(user); msg="注册成功"; return "login"; } public String toIndex(){ System.out.println("跳转到首页"); return "index"; } /* 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 Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Boolean getGender() { return gender; } public void setGender(Boolean gender) { this.gender = gender; }*/ public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }struts.xml:
<?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 name="struts.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="" value=""></constant> <package name="default" namespace="/" extends="struts-default"> <!--http://localhost:9999/Struts2/user_toLogin.action --> <!-- 1. --> <action name="user_*" class="com.xu.struts2.web.action.UserAction" method="{1}"> <result name="login">/WEB-INF/jsp/login.jsp</result> <result name="register">/WEB-INF/jsp/register.jsp</result> <result name="index">/WEB-INF/jsp/index.jsp</result> </action> <!-- 2. --> <action name="toLogin" class="com.xu.struts2.web.action.UserAction" method="toLogin"> <result name="login">/WEB-INF/jsp/login.jsp</result> </action> <!-- 3. --> <!-- user!toLogin.action --> <action name="user" class="com.xu.struts2.web.action.UserAction"> <result name="login">/WEB-INF/jsp/login.jsp</result> <result name="register">/WEB-INF/jsp/register.jsp</result> <result name="index">/WEB-INF/jsp/index.jsp</result> </action> </package> <!-- <include file="example.xml"/> --> </struts>register.jsp:
<%@ 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> <hr> <%-- <form action="user!register.action"> 用户名:<input type="text" name="name"/><br> 密码:<input type="password" name="password"/><br> 确认密码:<input type="password" name="repassword"/><br> 性别:<input type="radio" name="gender" value="true" checked="checked">男 <input type="radio" name="gender" value="false">女 <br> 年龄:<select name="age"> <option value="">请选择</option> <% for(int i=10;i<=30;i++){ %> <option value="<%=i %>"><%=i %>周岁</option> <% } %> </select> <br> <input type="submit" value="注册"> </form> --%> <form action="user!register.action"> 用户名:<input type="text" name="user.name"/><br> 密码:<input type="password" name="user.password"/><br> 确认密码:<input type="password" name="repassword"/><br> 性别:<input type="radio" name="user.gender" value="true" checked="checked">男 <input type="radio" name="user.gender" value="false">女 <br> 年龄:<select name="user.age"> <option value="">请选择</option> <% for(int i=10;i<=30;i++){ %> <option value="<%=i %>"><%=i %>周岁</option> <% } %> </select> <br> <input type="submit" value="注册"> </form> </body> </html>login.jsp:
<%@ 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> <h4 style="color: red">${msg }欢迎您:${user.name }</h4> <%-- <h4 style="color: red">${msg }</h4> --%> <h3>登录页面</h3> <hr> <form action="LoginServlet"> 用户名:<input type="text" name="name"/><br> 密码:<input type="password" name="password"/><br> <input type="submit" value="登录"> </form> </body> </html><好了,今天我学习的就这么多,有什么不懂的可以问我~
其实我也是渣渣~
标签:
原文地址:http://blog.csdn.net/qq_33642117/article/details/51883904