1,只用Struts2实现简单的登录,后台可以接收到值
1.1 Mavenx新建web项目
1.2 修改相关配置文件,如下:
pom.xml
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.20</version> </dependency>
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
在src/main/resources目录下新建: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> <package name="demo" namespace="/" extends="struts-default"> <action name="init" class="com.demo.action.UserAction" method="init"> <result name="success">/init.jsp</result> </action> <action name="login" class="com.demo.action.UserAction" method="login"> <result name="success">/success.jsp</result> </action> </package> </struts>
整个项目的结构如下:
1.3 UserAction.java
private String username; private String password; /** set.. get.. */ public String init(){ return SUCCESS; } public String login() { System.out.println("username = " + username + ",password = " + password); return SUCCESS; }
1.3 在webapp下新建init.jsp(初始化),success.jsp(登录成功)
index.jsp: <a href="init">Login</a> 登录初始化,init.jsp: <form action="login"> 姓名:<input name="username" value=""/> 密码:<input name="password" type="password" value=""/> <input type="submit" value="Login"/> </form> 登录成功页面,success.jsp <%@taglib prefix="s" uri="/struts-tags" %> <p>登录成功</p> 名字:<s:property value="%{username}"/><br> 密码:<s:property value="%{password}"/>
1.4 部署项目:http://localhost:9080/Demo/index.jsp
姓名:username
密码:password
登录成功页面