Struts(2)Struts快速入门案例和使用filter配合Struts,解决中文乱码问题

Struts登录系统搭建
本文介绍如何使用Struts框架搭建一个简单的登录系统,包括工程创建、配置文件编写、过滤器设置及数据库验证等步骤。

1 创建一个web工程

2 将Struts包引入web下的lib目录中

struts-1.3.10:http://pan.baidu.com/s/1c2IhRSs

3 创建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>
    <form action="/StrutsLoginJ/login.do" method="post"> 
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>
        <input type="submit" value="login">
    </form>
  </body>
</html>

4 创建ActionForm(用户表单)和Action

public class UserForm extends ActionForm {
    // 定义属性【属性名应该和jsp的控件名称一致,但是方法名(getUsername)必须与jsp控件名(username)保持一致】
    private String username;
    private String password;


    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        System.out.println("正在初始化username " + username);
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}
public class LoginAction extends Action {

    // 需要重新编写一个方法:execute会被自动调用,有点类似servlet中的service方法
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        System.out.println("hello world!");
        // 把ActionForm转换成对应的UserForm对象
        UserForm userform = (UserForm) form;

        System.out.println(userform.getUsername());

        if("123".equals(userform.getPassword())) {
            return mapping.findForward("ok");
        } else {
            return mapping.findForward("err");
        }
    }
}

5 创建Struts-config.xml文件,该文件用于配置ActionForm、action,对应关系,跳转关系,一般放在WEB-INF目录下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<!-- 配置表单 -->
    <form-beans>
    <!-- name 是表单名字,随意写,但是建议表单类名小写 
         type 用于指定表单类的全路径
    -->
        <form-bean name="userForm" type="com.test.forms.UserForm"></form-bean>
    </form-beans>   
    <!-- 配置action -->
    <action-mappings>
    <!-- 配置具体的一个action  path:表示将来访问该action的资源名称  http://localhost:8080/web应用/path?
        name: 用于关联某个表单
        type: 用于指定该action类的全路径
        scope 表示该action对应的表单对象的生命周期  request = request.setAttribute("formName", userForm);
        配置scope后,会将ActionForm对象放入request或session中
        scope 默认是session
    -->
        <action path="/login" name="userForm" scope="request" type="com.test.actions.LoginAction">

        <!-- 这里配置跳转关系 
            name 表示结果名称, path 转发到哪个文件
        -->
            <forward name="ok" path="/WEB-INF/wel.jsp"></forward>
            <forward name="err" path="/WEB-INF/err.jsp" />
        </action>
    </action-mappings>
</struts-config>

6 编写wel.jsp和err.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>My JSP 'wel.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">
  </head>
  <body>
    <h1>OK!</h1><br>
  </body>
</html>

7 在web.xml中配置ActionServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     <display-name>StrutsLoginJ</display-name>
     <welcome-file-list>
       <welcome-file>index.html</welcome-file>
       <welcome-file>index.htm</welcome-file>
       <welcome-file>index.jsp</welcome-file>
       <welcome-file>default.html</welcome-file>
       <welcome-file>default.htm</welcome-file>
       <welcome-file>default.jsp</welcome-file>
     </welcome-file-list>
     <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <!-- 配置Struts-config -->
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/Struts-config.xml</param-value>
    </init-param>
     </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
</web-app>

8 使用filter配合Struts,解决中文乱码问题

开发一个过滤器,实现了javax.servlet.Filter接口的Servlet类

public class MyFilter1 extends HttpServlet implements Filter {
    private String encoding;
    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        arg0.setCharacterEncoding(encoding); // 设置接收编码
        //arg1.setCharacterEncoding(encoding); // 设置接收编码
        arg2.doFilter(arg0, arg1); // 必须

        arg1.setContentType("text/html; charset=utf-8");
    }
    @Override
    public void init(FilterConfig arg0) throws ServletException {
        encoding = arg0.getInitParameter("encoding");
    }
}

web.xml中配置如下:

<filter>
    <filter-name>MyFilter1</filter-name>
    <filter-class>com.test.filter.MyFilter1</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>MyFilter1</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

9 同一个项目,配置多个Struts配置文件

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <!-- 配置Struts-config -->
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/Struts-config.xml, /WEB-INF/Struts-config-2.xml</param-value>
    </init-param>
</servlet>

10 到数据库中验证用户是否合法(MySQL)

①在mysql中增加数据表
② 与数据库普通使用一样,然后实现UserService

Users user = new Users(userform.getPassword(), userform.getUsername());
UserService us = new UserService();

System.out.println("out " + user.getUser_name() + " " + user.getUser_pwd());

if(us.checkUser(user)) {
    return mapping.findForward("ok");
} else {
    return mapping.findForward("err");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ADreamClusive

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值