欧美日韩国产一区,亚洲一区视频,色综合久久久久,私密按摩师舌头伸进去了,99re6这里只有精品,夜夜性日日交xxx性hd

微信小程序填坑之路之springmvc-注解版(三)

  • • 發(fā)表于 8年前
  • • 作者 toBeMN
  • • 4987 人瀏覽
  • • 1 條評論
  • • 最后編輯時間 8年前
  • • 來自 [技 術(shù)]

原創(chuàng)聲明:本文為作者原創(chuàng),未經(jīng)允許不得轉(zhuǎn)載,經(jīng)授權(quán)轉(zhuǎn)載需注明作者和出處

傳統(tǒng)的setter注入,都需要在springmvc配置文件中配置bean等操作,隨著項目的擴(kuò)展,整個配置文件就不敢看了,顯得紊亂且效率低下,我們在開發(fā)的時候還需要配置文件和action之間來回切換,很不方便。于是注解應(yīng)運而生,它充分的利用了Java的反射機(jī),減少配置工作,直接在action類中進(jìn)行設(shè)置,遵循了高內(nèi)聚低耦合的設(shè)計模式,讓代碼跟美觀,讓開發(fā)更高效。

現(xiàn)在重點解析的不在是配置文件,而是Action類,web.xml的配置與上一篇“xml版配置”中web.xml配置一樣。

UserAction類

package action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class UserAction{
    @RequestMapping(value="/login.mn")
    public String login(){
        return "login";
    }
    @RequestMapping(method=RequestMethod.POST,"/logout")
    public String logout(){
        return "logout";
    }
}

在類上方添加注解@Controller,對應(yīng)的方法上添加注解@RequestMapping,即可通過localhost:8080/springMVC(項目名)/login.mn訪問到對應(yīng)的方法中,上面的例子返回邏輯視圖路徑。
(value值和方法名沒有關(guān)聯(lián),可以不一樣)


光標(biāo)停留在@RequestMapping左括號后,按Alt+/可看出該注解可使用的參數(shù)有4個,并且都是數(shù)組。
就此,我們可以得知:
value={"/login.mn","","",""}可以讓多個請求訪問該方法
method={RequestMethod.POST,RequestMethod.GET,...}可以讓該方法接受不同的請求類型

@RequestMapping需要注意的有四點:
1.如果使用參數(shù)“只有”value,可以省略value=,直接寫上地址。
2.各個參數(shù)位置不固定
3.不寫method,默認(rèn)get,post方法都可訪問(建議寫上,因為get是通過地址后攜帶參數(shù)的方法傳值過來的,為防止有人提交惡意數(shù)據(jù),需要傳值的請求請寫上method=RequestMethod.POST)
4.value的值可以省略后綴.mn,但頁面上的請求路徑是不能省略的

這樣簡單的Action類就配置完了
然后在springmvc.xml稍稍配置一點東西即可

<?xml version="1.0" encoding="UTF-8"?>
<beans 
      xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/mvc
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- Action,讓springioc容器去掃描帶@Controller的類,base-package:掃描指定包
    需要添加 xmlns:context="http://www.springframework.org/schema/context"和
    http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    -->
    <context:component-scan base-package="action"/>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

在之前springmvc.xml配置文件的基礎(chǔ)上,在頭部信息中添加xmlns:context="http://www.springframework.org/schema/context"
及其對應(yīng)的約束文件http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd

之后配置<context:component-scan base-package="action"/>,讓springioc容器去掃描帶@Controller的類(UserAction.java),base-package是指定要掃描的包(我寫的UserAction.java在action包中)

login.jsp和logout.jsp文件只是簡單的寫上“登錄成功,退出成功”
配置結(jié)束,運行:


第二個運行結(jié)果失敗的原因是因為設(shè)置了只接受post請求,而在地址欄直接訪問默認(rèn)是get

如果在類方法上也添加了@RequestMapping,則請求地址就是 類級@RequestMapping+方法級@RequestMapping

package action;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserAction{
    @RequestMapping(value="/login.mn")
    public String login(){
        return "login";
    }
    @RequestMapping(value="/logout")
    public String logout(){
        return "logout";
    }
}

結(jié)果:

Action擴(kuò)展

表單與后臺的交互(普通參數(shù))

user.jsp

<body>
    <form action="${pageContext.request.contextPath }/user/login.mn" method="POST">
        姓名:<input type="text" name="username"/><br/>
        年齡:<input type="text" name="age" /><br/>
        <input type="submit" value="登錄"/>
    </form>
</body>

success.jsp頁面只有一局EL表達(dá)式用來輸出后臺傳過來的值

<body>
    ${msg }
</body>

UserAction.java

package action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/user")
public class UserAction{
    @RequestMapping("/login")
    public String login(Model model,String username,int age){
        model.addAttribute("msg","姓名:"+username+"年齡:"+age);
        return "success";
    }
}

在沒有學(xué)習(xí)更多注解的情況下,使用model(跟ModelAndView不是一個東西)傳值給前臺,之后的參數(shù)username,age必須與前臺表單中的name一致,才能夠正確的傳值

結(jié)果:

點擊登錄

表單與后臺的交互(數(shù)組參數(shù))

user.jsp

<form action="${pageContext.request.contextPath }/user/login.mn" method="POST">
    <input type="text" name="list" value="to"/><br/>
    <input type="text" name="list" value="Be"/><br/>
    <input type="text" name="list" value="M"/><br/>
    <input type="text" name="list" value="N"/><br/>
    <input type="submit" value="提交" />
</form>

name全都一樣

UserAction.java

@Controller
@RequestMapping("/user")
public class UserAction{
    @RequestMapping("/login")
    public String login(String[] list,Model model){
        String str="";
        for(int i=0;i<list.length;i++){
            str+=list[i];
        }
        model.addAttribute("msg",str);
        return "success";
    }
}

用數(shù)組接收

結(jié)果

表單與后臺的交互(對象參數(shù))

普通的參數(shù)缺陷很明顯,如果頁面?zhèn)鱽硎畮讉€參數(shù),那Action也得用十幾個參數(shù)來取值,所以使用對象封裝很有必要

需要添加實體類

package bean;
public class User {
    private String username;
    private int age;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

注意不要寫帶參數(shù)的構(gòu)造函數(shù),否則會報500錯
Could not instantiate bean class [bean.User]: No default constructor found

user.jsp

<form action="${pageContext.request.contextPath }/user/login.mn" method="POST">
    姓名:<input type="text" name="username"/><br/>
    年齡:<input type="text" name="age" /><br/>
    <input type="submit" value="登錄"/>
</form>

注意name值的改變!

UserAction.java

@Controller
@RequestMapping("/user")
public class UserAction{
    @RequestMapping("/login")
    public String login(Model model,User user){
        model.addAttribute("msg","姓名:"+user.getUsername()+"年齡:"+user.getAge());
        return "success";
    }
}

結(jié)果與上面例子一致

表單POST請求中文亂碼

上面的例子當(dāng)輸入中文時會出現(xiàn)亂碼

點擊登錄

這是為什么,看了看頁面編碼

沒有錯也,在看tomcat編碼

還是沒有錯,那是什么問題
原來排除頁面編碼,tomcat編碼的問題,還需要在web.xml文件中配置spring提供的針對于post請求的設(shè)置編碼格式的過濾器(注意是針對post,所以請求如果帶有參數(shù)請使用post)

<filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
 <filter-name>CharacterEncodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

我的編碼格式統(tǒng)一是utf-8
最后訪問結(jié)果:

請求轉(zhuǎn)發(fā)

(直接使用 數(shù)組參數(shù) 的例子)
修改Action類

@Controller
@RequestMapping("/user")
public class UserAction{
    @RequestMapping("/login")
    public String login(){
        System.out.println("我是第一個被請求的");
        return "forward:/user/newLogin.mn";
    }
    @RequestMapping("/newLogin")
    public String newLogin(String[] list,Model model){
        System.out.println("我是第二個被請求的");
        String str="";
        for(int i=0;i<list.length;i++){
            str+=list[i];
        }
        model.addAttribute("msg",str);
        return "success";
    }
}

使用forward:+新請求路徑即可轉(zhuǎn)移

結(jié)果:
頁面顯示與 數(shù)組參數(shù) 例子相同
查看控制臺

可知兩個方法都經(jīng)過了

下一帖子,將會講述小程序與本框架后臺之間的json數(shù)據(jù)交互,good good study,day day up

分享到:
1條評論
Ctrl+Enter
作者

toBeMN

toBeMN

APP:3 帖子:24 回復(fù):59 積分:3193

已加入社區(qū)[3111]天

夢想成為全棧的男人

作者詳情》
Top