結構 包與之前相同 <?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/java ...
結構
包與之前相同
<?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>springmvc01</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingfilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingfilter</filter-name> <url-pattern>*</url-pattern> </filter-mapping> </web-app>View Code
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 自動掃描載入註解的包 --> <context:component-scan base-package="com.ij34.bean"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/"></property> <property name="suffix" value=".jsp" ></property> </bean> </beans>View Code
web.xml 、spring-mvc.xml不變
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% response.sendRedirect("articles/list")
list.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> <table> <tr> <th colspan="2">文章列表</th> </tr> <tr> <td>1</td> <td><a href="${pageContext.request.contextPath }/articles/show/1" target="_blank">文章一</a></td> </tr> <tr> <td>2</td> <td><a href="${pageContext.request.contextPath }/articles/show/2" target="_blank">文章二</a></td> </tr> </table> </body> </html>
show.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> <p style="font-style: 16;font-weight:bold " >${articles.name}</p> <p>${articles.context }</p> </body> </html>
Article.java
package com.ij34.model; public class Article { private String id; private String name; private String context; public Article() { // TODO Auto-generated constructor stub } public Article(String name,String context) { this.name=name; this.context=context; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getcontext() { return context; } public void setcontext(String context) { this.context = context; } }
Testhello.java
package com.ij34.bean; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.ij34.model.Article; @Controller @RequestMapping("/articles") //外部請求的 包括update.jsp里${students.name } public class Testhello { @RequestMapping("/list") //@RequestMapping 請求映射 public String list(Model model) { return "articles/list"; } @RequestMapping("/show/{id}") public ModelAndView show(@PathVariable("id")int id){//@RequestParam 請求參數 ModelAndView mav=new ModelAndView(); if(id==1){ mav.addObject("articles", new Article("文章一","這是文章1的內容!!!!!!!!")); }else if(id==2){ mav.addObject("articles", new Article("文章二","這是文章2的內容!!!!!!!!")); } mav.setViewName("articles/show"); return mav; } }
結果