enmmm本该昨天发的博客硬生生被我推到今天了,没办法,那就多写一篇吧~~~
首先来说Struts2,其实Struts2的介绍我在之前的博客已经写过了,有兴趣的可以自己去看。废话不多说进入正题吧
1、新建一个Maven项目(Maven03)
2、打开pom.xml,写入依赖。
<!-- 属性配置 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<!-- struts2依赖包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.14</version>
</dependency>
</dependencies>
<build>
<finalName>struts2</finalName>
<plugins>
<!-- JDK配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- Tomcat配置 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
3、在Java Resources--->src/main/java中新建一个UserAction类
继承ActionSupport
package com.hp.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute() {
return SUCCESS;
}
public String login() {
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println("name->" + username + ",password->"+ password);
if ("admin".equals(username) && "123456".equals(password)) {
return SUCCESS;
} else {
return "login";
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
}
4、新建struts.xml 配置Struts2
配置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>
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.devMode" value="false" />
<include file="struts-default.xml" />
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.hp.action.UserAction" method="login">
<result name="success">index.jsp</result>
<result name="login">login.jsp</result>
</action>
</package>
</struts>
5、新建index.jsp和login.jsp(测试框架是否搭建成功)
index.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>
<h2>欢迎登录</h2>
山有木兮木有枝,心悦君兮君不知。<br/>
</body>
</html>
login.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="login" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /> </td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" name="password" /> </td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录" />
<input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
首先更新一下项目,然后编译,运行。正常情况下,应该是不会报错的
复制地址在谷歌浏览器打开
但是,总有那么几个会报错,总有那么几个bug不甘寂寞想出来玩会儿是吧。你也没有办法啊,你也很绝望啊。