Struts2的初级应用

做一个登录注册

1、把Struts2框架的必须包导入到项目中(http://struts.apache.org/)

Struts2的初级应用

2、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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 配置struts2核心控制器 -->
<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>*.action</url-pattern>
</filter-mapping> </web-app>

3、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="test" namespace="/" extends="struts-default"> <action name="login" class="com.cn.action.Login">
<result name="welcome">/Welcome.jsp</result>
<result name="error">/Login.jsp</result>
</action> <action name="register" class="com.cn.action.Register">
<result name="welcome">/Welcome.jsp</result>
</action> </package> </struts>

4、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>
<title>登录</title>
</head> <body>
<form action="login.action">
${ msg }<br/>
账号:<input type="text" name="account" value="${account}"/><br/>
密码:<input type="password" name="password"/><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submit" value="登录" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href="Register.jsp">注册</a>
</form>
</body>
</html>

5、Register.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>
<title>注册</title>
</head> <body>
<form action="register.action">
账号:<input type="text" name="user.account"><br/>
密码:<input type="password" name="user.password"><br/>
性别:<input type="text" name="user.sex"><br/>
邮箱:<input type="text" name="user.email"><br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="submit" value="注册">
</form>
</body>
</html>

6、User类,UserDao,DBHelper略。。。。(看之前的)

7、Login.java

package com.cn.action;

import com.cn.dao.UserDao;

public class Login {

    private String account;
private String password;
private String msg; public String execute(){
UserDao userDao = new UserDao();
Boolean user = userDao.queryUser(account, password);
if(user){
return "welcome";
}else{
msg = "用户名或密码不正确";
return "error";
} } public String getAccount() {
return account;
} public void setAccount(String account) {
this.account = account;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} }

8、Register.java

package com.cn.action;

import com.cn.dao.UserDao;
import com.cn.pojo.User; public class Register { private User user; public String execute(){
UserDao userdao = new UserDao();
userdao.addUser(user);
return "welcome";
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} }
上一篇:ios 自定义NSError


下一篇:EasyUI Tabs + Yii2.0实现iframe方式打开页面(解决共用静态文件引入加载的问题)