struts2简单登录界面

最近小学生接触到了strusts,简单记录下struts的使用。
首先是配置。小学生使用的是eclipse。
配置好tomcat后,需要导入一些jar包,如图:
在这里插入图片描述
其次是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">
  <display-name>Struts1</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>

	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

注意,这里小学生,用的是2.5版本以上的.
然后,struts2的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
 <struts>
	<!-- Struts。xml的配置文件 -->
    <!-- 所有匹配*.action的请求都由struts2处理 -->
    <constant name="struts.action.extension" value="action" />
    <!-- 是否启用开发模式 -->
    <constant name="struts.devMode" value="true" />
    <!-- struts配置文件改动后,是否重新加载 -->
    <constant name="struts.configuration.xml.reload" value="true" />
    <!-- 请求参数的编码方式 -->
    <constant name="struts.i18n.encoding" value="utf-8" />
    <!-- 每次HTTP请求系统都重新加载资源文件 -->
    <constant name="struts.i18n.reload" value="true" />
    <!-- 让struts2支持动态方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>  <!--动态方法调用-->
	<!--新建一个package,name随意,extends自struts-default -->
    <package name="/" extends="struts-default">
    	<!-- 编写action,name为地址栏输入时用的名字,class为class文件的位置 -->
        <action name="index">
        	<!-- name 为刚才编写的action类中,execute返回的值       index.jsp的意思是返回到index.jsp页面 -->
        	<result name="success">/index.jsp</result>
        </action>
       
        <action name="login" class="com.shiyanyi.Login" method="login">
        <result name="success">/success.jsp</result>
        	
        </action> 
        
        
    </package>
</struts>

在这里插入图片描述在这里插入图片描述
jsp:表单:

<body>
	<form method="post" action="login.action">
		账 号:<input type="text" class="input" name="username" /><br>
		密 码:<input type="password" class="input" name="password" /><br> 
		<input type="submit" value="提交"> 
		<input type="reset" value="重置" />
	</form>
</body>

success:表单:

<body>
登陆成功!
</body>

login.java:

package com.shiyanyi;

import java.io.IOException;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import jdk.incubator.http.HttpRequest;


public class Login {

	private String username;
	
	private String password;

	
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}



	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}



	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}



	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}



	public String login() {
		HttpServletRequest req = ServletActionContext.getRequest();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		if(!password.equals("123")) {
			return "error";
		}
		return "success";

	}
	
	public String register(){
		HttpServletRequest req = ServletActionContext.getRequest();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		try {
			ServletActionContext.getResponse().getWriter().println("register success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "success";

	}
	
	
}

注意:
struts.xml中的 标签name="login"要与 index.jsp中 标签action=“login” 一致;

<package name="/" extends="struts-default">

struts中的name为访问中的前缀

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值