美文网首页
一个简单的Java Web实例教程

一个简单的Java Web实例教程

作者: PHPMyCMS | 来源:发表于2018-07-02 16:36 被阅读0次

1.首先项目结构目录


mulu.PNG

2.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
                                       http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xs</groupId>
  <artifactId>aaa</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>aaa Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.9.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.2.9.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.2.9.RELEASE</version>
    </dependency>   
  </dependencies>
  <build>
    <finalName>aaa</finalName>
    <plugins>  
        <plugin>  
            <artifactId>maven-compiler-plugin</artifactId>  
            <version>3.1</version>  
            <configuration>  
                <source>1.7</source>  
                <target>1.7</target>  
            </configuration>  
        </plugin>  
    </plugins>
  </build>
</project>

3.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>Archetype Created Web Application</display-name> 
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
        
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping> 
</web-app>  

4.serlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <context:component-scan base-package="com.xs" />

    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
</beans:beans>

5.HomeController.java

package com.xs.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/hello")
public class HomeController {
    //如果value="/",那么http://localhost:8080/aaa无法访问,而http://localhost:8080/aaa/可以访问
    //所以一般情况下,为了对客户友好,建议设为value=""或者简写为
    //@RequestMapping(method=RequestMethod.GET)。
    @RequestMapping(method=RequestMethod.GET)
    public String home(){
        return "home";
    }
    //@RequestMapping(method = RequestMethod.GET)
      // public String printHello(ModelMap model) {
        //  model.addAttribute("message", "Hello pring MVC Fram!");
          //return "index";
       //}
    
    @RequestMapping(value="/welcome",method=RequestMethod.GET)
    public String welcome(Model model){
        model.addAttribute("name","执行成功!");
        return "welcome";
    }
    
    @RequestMapping(value="/hiphop/second",method=RequestMethod.GET)
    public String hiphop(Model model){
        model.addAttribute("cost","Second!");
        return "cost";  
    }

}

6.index.jsp

<html>
<body>
<h2>Hello World!${message}</h2>

<a href="hello">home</a>
</body>
</html>

7.home.jsp

<html>
<body>
<h2>Hello World!${message}</h2>

<a href="hello/welcome">welcome</a>
<a href="hello/hiphop/second">hiphop</a>
</body>
</html>

8.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>Insert title here</title>
</head>
<body>
<h1>Welcome,${name}</h1>
</body>
</html>

9.cost.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>this a ${cost}</h1>

</body>
</html>

相关文章

  • 一个简单的Java Web实例教程

    1.首先项目结构目录 2.pom.xml 3.web.xml 4.serlet-context.xml 5.Hom...

  • React 入门的一些文章

    React 入门实例教程 Redux 入门教程(一):基本用法 Redux 的设计思想很简单 (1)Web 应用是...

  • String 总结

    Java String类 Java lang.String类用法实例教程。 简介java.lang.String类...

  • maven 系列 3-创建Web应用程序

    创建Web应用程序 要创建一个简单的java web应用程序,我们将使用Maven的原型 - web应用插件。 因...

  • Tomcat

    Tomcat官方站点 简单介绍:Tomcat是一个能够运行java页面(jsp)的java’web服务器程序;安装...

  • 吊打面试官之 JavaEE(框架对比)部分[4]

    1.请简单介绍一下你了解的Java领域中的Web Service框架都有哪些? 参考回答: Java领域的Web ...

  • Maven常用命令

    创建一个简单的Java工程 创建一个Java的web工程 编译项目 编译测试程序 运行测试 打包 清理(删除tar...

  • Java Web开发实战经典学习过程笔记

    Java Web开发实战经典学习简单笔记 第一章Java Web开发简介 1.胖客户端程序指的是,当一个程序运行时...

  • Java(JavaEE)实例

    Java实例教程(下) Java当前日期/时间Java将字符串转换为日期Java当前工作目录Java正则表达式Ja...

  • Java实例教程(下)

    Java实例教程(下) Java当前日期/时间Java将字符串转换为日期Java当前工作目录Java正则表达式Ja...

网友评论

      本文标题:一个简单的Java Web实例教程

      本文链接:https://www.haomeiwen.com/subject/mneluftx.html