struts2 helloworld用JavaBean的方式获取post值并传jsp页面获取

来源:互联网 发布:上瘾网络剧在线看 编辑:程序博客网 时间:2024/06/06 00:16

配置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">
  <display-name></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.ng.filter.StrutsPrepareAndExecuteFilter
  </filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>*.action</url-pattern>
  </filter-mapping>
  </web-app>

配置struts.xml进行映射

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="helloworld" extends="struts-default" namespace="/login">
<action name = "hello">
<result name="success">/hello.jsp</result>
</action>
<action name = "welcome" class="com.qiaozhq.outman.WelcomeAction">
<result name="success">/welcome.jsp</result>
<result name="fail">/error.jsp</result>
</action>
</package>
</struts>

WelcomeAction.java

package com.qiaozhq.outman;


public class WelcomeAction {
private String name;
private String password;
public String execute(){
System.out.println("WelcomeAction execute...");
System.out.println("name="+name+",password="+password);
if("".equals(name)){
return "fail";
}
return "success";
}
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;
}


}

hello.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>
<form action="welcome.action" method="post">
Please input your name:<input type="text" name="name" /><br />
        Please input your password<input type="password" name = "password" /><br />
<input type = "submit" value="Submit"  /> 
</form>
</body>
</html>

显示表单页面

welcome.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>
欢迎您 ${name}(${password})
</body>
</html>

运行结果:




0 0
原创粉丝点击